画线不起作用,可能是什么问题?

时间:2019-03-24 10:45:00

标签: c++ winapi

我想通过在行应开始的第一个坐标和行应结束的第二个坐标上单击鼠标来绘制线。 当我运行我的项目时,什么也没有发生。 我找不到我的代码丢失了。

LONG WINAPI WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{

    HDC hdc;
    BOOL fDraw = FALSE;
    POINT ptPrevious = { 0 };
    HPEN Pen = CreatePen(PS_SOLID, 3, RGB(0, 0, 255));


    switch (Message) {
        case WM_LBUTTONDOWN: {
            fDraw = TRUE;
            ptPrevious.x = LOWORD(lParam);
            ptPrevious.y = HIWORD(lParam);
            break;
        }

        case WM_LBUTTONUP: {
            if (fDraw)
            {
                hdc = GetDC(hWnd);
                MoveToEx(hdc, ptPrevious.x, ptPrevious.y, NULL);
                LineTo(hdc, LOWORD(lParam), HIWORD(lParam));
                ReleaseDC(hWnd, hdc);
            }
            fDraw = FALSE;
            break;
        }


        case WM_MOUSEMOVE: {
            if (fDraw)
            {
                hdc = GetDC(hWnd);
                MoveToEx(hdc, ptPrevious.x, ptPrevious.y, NULL);
                LineTo(hdc, ptPrevious.x = LOWORD(lParam),
                    ptPrevious.y = HIWORD(lParam));
                ReleaseDC(hWnd, hdc);
            }
            break;
        }


        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hWnd, Message, wParam, lParam);
    }
    return 0;
}

3 个答案:

答案 0 :(得分:2)

从根本上讲,问题是因为您的数据都没有持久性。由于它们都是局部变量,因此WndProc返回时它们的内容就会消失。在每条消息上fDraw都设置为FALSE,并且对于WM_LBUTTONDOWN或WM_MOUSEMOVE消息永远不会为真。因此什么也没发生。

您将要创建某种类型的类以容纳fDrawptPrevious,并创建一种结构以容纳要绘制的线的坐标。在WM_MOUSEMOVE和WM_LBUTTONUP消息中使用InvalidateRect。然后仅响应WM_PAINT消息(使用绘画消息中提供的DC)绘制它们。

使用CWnd MFC类可以大大简化这些任务。

答案 1 :(得分:1)

不要获取DC三次,请获取一次,然后用鼠标将其释放。 另外,您的图形将最终得到验证,因此您需要记录鼠标的移动并在WM_PAINT处理程序中重复此操作。

答案 2 :(得分:1)

由于声明的变量是局部变量,因此可以使用静态声明变量。

   static BOOL fDraw = FALSE;
   static POINT ptPrevious = { 0 };

此外,您忘记使用SelectObject函数将笔选择为DC。

您还可以使用WM_PAINT重绘来改善代码:

示例:

   LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
   {
   HDC hdc;
   PAINTSTRUCT ps;
   static BOOL fDraw = FALSE;
   static BOOL fDraw_begin = FALSE;
   static POINT ptPrevious;
   static RECT rcClient; 
   static POINT pt;
   static HPEN Pen;

   switch (message)
   {
   case WM_CREATE:
   {
          Pen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
          break;
   }

   case WM_LBUTTONDOWN:
   {
          ptPrevious.x = (LONG)LOWORD(lParam);
          ptPrevious.y = (LONG)HIWORD(lParam);
          return 0;
   }

   case WM_LBUTTONUP:
   {
          if (fDraw = TRUE)
          {
                 fDraw = FALSE;
                 fDraw_begin = TRUE;
                 InvalidateRect(hwnd, &rcClient, TRUE);
                 UpdateWindow(hwnd);

          }
          return 0;
   }

   case WM_MOUSEMOVE: 
   {
          if (wParam && MK_LBUTTON)
          {
                 GetClientRect(hwnd, &rcClient);
                 hdc = GetDC(hwnd);
                 SetROP2(hdc, R2_NOTXORPEN);
                 if (!IsRectEmpty(&rcClient))      // Detecting whether the rectangular area is empty
                 {
                       MoveToEx(hdc, ptPrevious.x, ptPrevious.y, NULL);
                       LineTo(hdc, (LONG)LOWORD(lParam),
                              (LONG)HIWORD(lParam));
                 }
                 MoveToEx(hdc, ptPrevious.x, ptPrevious.y, NULL);
                 LineTo(hdc, (LONG)LOWORD(lParam),
                       (LONG)HIWORD(lParam));
                 pt.x = (LONG)LOWORD(lParam);
                 pt.y = (LONG)HIWORD(lParam);
                 fDraw = TRUE;

                 ReleaseDC(hwnd, hdc);
          }      
          return 0;
   }

   case WM_PAINT:
   {
          hdc = BeginPaint(hwnd, &ps);
          SelectObject(hdc, Pen);
          if (fDraw_begin)
          {
                 fDraw_begin = FALSE;
                 MoveToEx(hdc, ptPrevious.x, ptPrevious.y, NULL);
                 LineTo(hdc, pt.x,
                       pt.y);
          }
          EndPaint(hwnd, &ps);
          ReleaseDC(hwnd, hdc);
          return 0;
   }

   case WM_DESTROY:
   {
          DeleteObject(Pen);
          PostQuitMessage(0);
          return 0;
   }

   }
   return DefWindowProc(hwnd, message, wParam, lParam);
   }