我不明白为什么这不起作用。在消息WM_LBUTTONDOWN上,存储指针的坐标。然后在WM_MOUSEMOVE上,如果左键按下,我希望它用原始点和鼠标所在的新点绘制一个椭圆。但是当我调试时没有任何反应。这是我的WindowProc
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (uMsg)
{
case WM_DESTROY:
{
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
}
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
break;
}
case WM_LBUTTONDOWN:
{
pnt.x = GET_X_LPARAM(lParam);
pnt.y = GET_Y_LPARAM(lParam);
break;
}
case WM_MOUSEMOVE:
{
if(wParam == MK_LBUTTON)
{
hdc = BeginPaint(hwnd, &ps);
Ellipse(hdc, pnt.x, pnt.y, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)); // nothing happens
EndPaint(hwnd, &ps);
}
break;
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
答案 0 :(得分:2)
您尚未使窗口的任何区域无效,因此BeginPaint
不会执行任何操作。您应该将该点存储在WM_MOUSEMOVE上(与pnt
类似的结构),并在那时调用InvalidateRect()。然后,在WM_PAINT中进行绘画。有关详细信息,请参阅此link。
答案 1 :(得分:1)
尝试使用以下代码:
static POINT begin, end;
static BOOL drawing = false;
// ...
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
if (drawing)
Ellipse(hdc, begin.x, begin.y, end.x, end.y);
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONDOWN:
begin.x = GET_X_LPARAM(lParam);
begin.y = GET_Y_LPARAM(lParam);
SetCapture(hWnd);
drawing = true;
break;
case WM_LBUTTONUP:
ReleaseCapture();
drawing = false;
break;
case WM_MOUSEMOVE:
end.x = GET_X_LPARAM(lParam);
end.y = GET_Y_LPARAM(lParam);
{
RECT invalid = {begin.x, begin.y, end.x, end.y};
InvalidateRect(hWnd, &invalid, true);
}
break;