调整椭圆大小

| 我不明白为什么这行不通。在消息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);
 }
    
已邀请:
尝试编写如下代码:
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;
    
您尚未使窗口的任何区域无效,因此
BeginPaint
不会做任何事情。您应该将点存储在WM_MOUSEMOVE(与in3ѭ类似的结构)中,然后调用InvalidateRect()。然后,在WM_PAINT中进行绘画。有关更多信息,请参见此链接。     

要回复问题请先登录注册