为什么当我画画的时候,鼠标在窗口里移动,却不是在同一时间到处画画?(WinAPI)

Why when I paint, and the mouse is moving in the window, the painting is not done at the same time everywhere? (WinAPI)

本文关键字:同一时间 WinAPI 为什么 窗口 鼠标 移动      更新时间:2024-09-26
问了这个问题后,我更改了代码。它是有效的,但当WM_PAINT绘制窗口并且光标在其中移动时,绘制不会在任何地方同时进行。这里有一个视频可以更好地观看。这是我的WM_PAINT:
//TV is a struct with the dimensions of the window.
//BitmapBuffer is the bitmap containing the painting.
static int cont;
cont++;
HDC HDc;
PAINTSTRUCT ps;
HDc = BeginPaint(identifier, &ps);
Gdiplus::Graphics lienzo (HDc);
AlphaBlend(HDc, 0, 0, TV.width+4, TV.height+4, buffer, 0, 0, TV.width+4, TV.height+4, CopyInfo);
EndPaint(identifier, &ps);

由于问题出在移动鼠标时,可能WM_NCHITTEST消息与此有关:

case WM_NCHITTEST: 
POINTS point1 = MAKEPOINTS(lParam); 
POINT point2;
point2.x = point1.x;
point2.y = point1.y;

ScreenToClient(hwnd, &point2); 
if (PtInRegion(region, point2.x, point2.y)) 
{
if (inWindow == false) //inWindow is true when the cursor is in the window
{
inWindow = true;
TrackMouseEvent(&structure);
Repaint(); //this function repaint the buffer and after call invalidrect
}
return HTCLIENT; 
}
else
{
if (inWindow == true)
{
inWindow = false;
Repaint();
}
return HTTRANSPARENT; 
}
break;

有人知道为什么会发生这种事吗?

很难看出问题,因为您的代码没有定义Repaint。但是,您应该使用InvalidateRect来告诉Windows哪个区域正在更新
说。。。Windows很难使用双缓冲技术对out更新图像进行编程。这是指绘制到内存(位图(,然后将位图位图位图到屏幕上。

你发现这个答案很有用用GDI+和C++减少闪烁