Winapi Lineto不会刷新线路

WinApi LineTo not refreshing the line

本文关键字:刷新 线路 Lineto Winapi      更新时间:2023-10-16

我只是开始学习C 。我要做的就是向指定的坐标绘制一条线,这是一种方法中的输入。我使用MoveToEx设置了每个循环中的起点(以不同的参数调用此功能),并给予我希望绘制泳道的坐标。

有什么想法如何使其在循环中工作?

我的代码类似于:

void Clock::drawSecondLine(float x,float y) {
    HWND console_handle = GetConsoleWindow();
    HDC device_context = GetDC(console_handle);
    HPEN pen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
    SelectObject(device_context, pen);
    MoveToEx(device_context, 0, 0, NULL);
    Ellipse(device_context, 400, 0, 0, 400);
    MoveToEx(device_context, 200, 200, NULL);
    LineTo(device_context, (int)x, (int)y);
    ReleaseDC(console_handle, device_context);
    cin.ignore();
}

和循环:

void Zegar::startClock() {
    while (true) {
        drawSecondLine(laneShowingSecond.getX(), laneShowingSecond.getY());
        laneShowingSecond.movePointByRadius(RADIUS_PER_SECOND);
        Sleep(1000);
        increaseSecond();
    }
}

这是一些示例代码(我在 vstudio 2k10 中运行的代码)。

注释

  • 尽管将其编译为 c (并使用某些 c 等功能,例如 iostream-意味着它不会编译为 c ),它仍然是旧的 c
  • 它有很多丑陋的东西和 no-no (例如全局var,lots定义,混合物 c c 代码,在控制台窗口上绘制,...)。目标是拥有a poc ;该代码稍后可以清洁。
  • 从边界矩形(RECT_*定义(400,0,0,400))我正在提取中心坐标,而在 x y 轴,使用一些简单的数学计算(由于矩形是正方形的事实,2个半径是相等的,因此我们击中了特殊情况,在这种情况下,椭圆形实际上是一个圆圈)。
  • nextPoint功能是从您的代码中替换laneShowingSecond.getX(), laneShowingSecond.getY()的功能。
  • 只有一次(在开始)将所有需要执行的所有操作都放在init函数中。请注意,如果初始化期间出现问题,它将使用 error < 0 )代码退出,因为它将无法绘制。
  • 类似地,末尾要执行的任何清理内容都放在cleanup函数中(在这里我不必费心检查返回代码,因为它正在退出)。
  • draw功能包含图形 loop 。在每个迭代中:
    • 角度通过INCERMENT_DEG递增(默认为30°)。
    • 使用一些简单的三角公式计算椭圆上的下一点。
    • 进行图纸。
    • ITERATION_SLEEP_TIME毫秒的等待(我将其设置为200,以避免等待每行绘制的一秒钟)。
    • 请注意:
      • 它在达到360°或 2 * pi Radians (圆将)之后停止,因为一遍又一遍地绘制相同的行是没有意义的。
      • 图形是逆时针(三角学中的正角)进行的; xoy origin( o(0,0))是屏幕的左上角。
  • 通过为它们分配各种值并查看图形如何变化,可以使用我标记的评论标记的定义。

main.cpp

#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <Windows.h>
#define RECT_LEFT 400  // Modify any of these 4 RECT_* values to get different ellipse shapes.
#define RECT_TOP 0
#define RECT_RIGHT 0
#define RECT_BOT 400
#define ITERATION_SLEEP_TIME 200  // Sleep time while in loop.
#define INCERMENT_DEG 30  // 30 degrees per step; a full circle has 360 (2 * PI  RAD).
#define M_PI_180 M_PI / 180
using std::cout;
using std::endl;
typedef enum {DRAW_RADII, DRAW_POLY} DrawMethod;
const int radiusX = abs(RECT_RIGHT - RECT_LEFT) / 2;
const int radiusY = abs(RECT_BOT - RECT_TOP) / 2;
const int centerX = (RECT_RIGHT + RECT_LEFT) / 2;
const int centerY = (RECT_BOT + RECT_TOP) / 2;
HWND hwnd = NULL;
HDC hdc = NULL;
HPEN hpen = NULL;
DrawMethod meth = DRAW_RADII;  // Modify this to DRAW_POLY to draw a polygon instead of the "bike wheel".
int deg = 0;
double x = 0, y = 0;

void nextPoint(int degree, double *x, double *y) {
    *x = centerX + radiusX * cos(M_PI_180 * degree );
    *y = centerY - radiusY * sin(M_PI_180 * degree);
}
int init() {
    if ((hwnd = GetConsoleWindow()) == NULL) {
        cout << "GetConsoleWindow error: " << GetLastError() << endl;
        return -1;
    }
    if ((hdc = GetDC(hwnd)) == NULL) {
        cout << "GetDC error: " << GetLastError() << endl;
        return -2;
    }
    if ((hpen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0))) == NULL) {
        cout << "CreatePen error: " << GetLastError() << endl;
        return -3;
    }
    SelectObject(hdc, hpen);
    Ellipse(hdc, RECT_LEFT, RECT_TOP, RECT_RIGHT, RECT_BOT);
    nextPoint(deg, &x, &y);
    if (meth == DRAW_RADII) {
        MoveToEx(hdc, centerX, centerY, NULL);
        LineTo(hdc, (int)x, (int)y);
    } else if (meth == DRAW_POLY) {
        MoveToEx(hdc, (int)x, (int)y, NULL);
    }
    return 0;
}
void draw() {
    while (deg < 360) {
        deg += INCERMENT_DEG;
        nextPoint(deg, &x, &y);
        if (meth == DRAW_RADII) {
            MoveToEx(hdc, centerX, centerY, NULL);
            LineTo(hdc, (int)x, (int)y);
        } else if (meth == DRAW_POLY) {
            LineTo(hdc, (int)x, (int)y);
        } else
            break;
        Sleep(ITERATION_SLEEP_TIME);
    }
}
void cleanup() {
    if (hpen) {
        DeleteObject(hpen);
    }
    if (hwnd && hdc) {
        ReleaseDC(hwnd, hdc);
    }
}
int main() {
    if (!init())
        draw();
    cleanup();
    return 0;
}