Winapi Lineto不会刷新线路
WinApi LineTo not refreshing the line
我只是开始学习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;
}
相关文章:
- 是否可以配置提升日志刷新?
- 如何在qt中同步应用程序和显示器的刷新率?
- 在 3ds Max 中更新进度条后,环境和效果 UI 不刷新
- std::endl,在 Python 中是否有等价物?(返回 + 刷新)
- QProcess无法通过QPushButton执行终端线路命令
- 在 directx11 代码中更改全屏模式下的刷新率不起作用
- Qt C++括号匹配和突出显示当前线路功能无法协同工作
- 如何在 Windows 中立即刷新文件夹图标
- 线路抑制状态错误 C4703 可能未初始化的局部指针变量"back"已使用
- 刷新来自Qpid质子连接的所有消息而不是等待?
- 如何让 cout 缓冲区在 ubuntu 上刷新
- 显示DHT1306温度数据时如何正确刷新Adafruit 11 OLED?
- 为什么PNG图像的stdout有时会在printf中刷新图像的一半
- 使叮当声忽略对线路的评论
- 到达线路时程序崩溃
- QML:收到C 信号从C 传递时刷新问题
- 托管语言是否锁定本机库的刷新和重新加载变量?
- 记录器何时应刷新
- 在 Linux 上编译一个 c++ 应用程序,并在 TI TIVA TM4C123GH6PM上刷新它
- Winapi Lineto不会刷新线路