c++ - WinApi LineTo 不刷新行

标签 c++ winapi console

我刚刚开始学习 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(比如全局变量、大量定义、CC++ 代码的混合,在控制台窗口上绘图,...)。目标是拥有一个PoC;代码可以稍后清理。
  • 从边界矩形(RECT_* 定义 (400, 0, 0, 400))我正在提取中心坐标和 上的半径XY 轴,使用一些简单的数学计算(由于矩形是正方形,2 个半径相等,所以我们遇到了椭圆的特殊情况实际上是一个圆圈)。
  • nextPoint 函数是替换您代码中的 laneShowingSecond.getX()、laneShowingSecond.getY()
  • 所有需要只执行一次(在开始时)的东西都放在 init 函数中。请注意,如果在初始化过程中出现问题,它将以错误(<0)代码退出,因为它将无法绘制。
  • 类似地,任何要在最后执行的清理工作都放在 cleanup 函数中(在这里我没有费心去检查返回代码,因为它无论如何都会退出)。
  • draw 函数包含绘图循环。在每次迭代中:
    • 角度增加 INCERMENT_DEG(默认为 30°)。
    • 椭圆上的下一个点是使用一些简单的三角公式计算的。
    • 完成绘图。
    • 等待 ITERATION_SLEEP_TIME 毫秒(我将其设置为 200 以避免为每条绘制的线等待一秒钟)。
    • 请注意:
      • 它在达到 360° 或 2 * PI 弧度(全天候)后停止,因为一遍又一遍地绘制相同的线没有意义。
      • 绘图是逆时针方向进行的(三角学中的正角); xOy 原点 (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;
}

关于c++ - WinApi LineTo 不刷新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42517628/

相关文章:

c++ - 终止递归调用

c++ - C++ 在线预处理器

c# - 为什么 Windows 只允许一个应用程序访问网络摄像头?

python - 如何在 Python 中的单独线程中启动 win32 应用程序

c# - 显示Windows窗体时,控制台应用程序不接受输入

c++ - qSort 的问题

c++ - "hello"和 {"hello"} 有什么区别?

检查文件是否正在使用 Win32 API 或 C/C++ 写入。我自己没有写权限

java - 如何从控制台设置亲和性运行java

php - 从 PHP 向 TCP/IP 控制台执行命令的最佳方式