我想通过点击并从一点到另一点移动光标来绘制一条线,我刚刚复制了以下代码,而我的WindowProcedure看起来就是这样:
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
//PAINTSTRUCT ps;
HDC hdc;
bool draw = false;
int x = 0, y=0;
//InvalidateRect(hwnd, NULL, true);
switch (uMsg)
{
case WM_LBUTTONDOWN:
draw = true;
x = LOWORD(lParam);
y = HIWORD(lParam);
return 0;
case WM_LBUTTONUP:
if (draw)
{
hdc = GetDC(hwnd);
MoveToEx(hdc, x, y, NULL);
LineTo(hdc, LOWORD(lParam), HIWORD(lParam));
ReleaseDC(hwnd, hdc);
}
draw = FALSE;
return 0;
case WM_MOUSEMOVE:
if (draw)
{
hdc = GetDC(hwnd);
MoveToEx(hdc, x, y, NULL);
LineTo(hdc, x = LOWORD(lParam), y = HIWORD(lParam));
ReleaseDC(hwnd, hdc);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
但是当我点击没有任何反应时,就像只有一个案件可以处理,是否正确?当我评论前两种情况时它会绘制线条,所以它进入切换但不是我想要做的。有什么建议吗?
答案 0 :(得分:-2)
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
//PAINTSTRUCT ps;
HDC hdc;
static bool draw = false; // ←static
static int x = 0, y=0; // ←
这个怎么样?