开发者

Resizing Ellipse

I dont understand why this doesn't work. On message WM_LBUTTONDOWN, the coordinates of the pointer are stored. then on WM_MOUSEMOVE, if the left button is down, i want it to draw an ellipse with the original points and the new points that are where the mouse is now. But nothing happens when I debug. Here is my 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, 开发者_如何学Go&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);
 }


You haven't invalidated any area of the window, so BeginPaint isn't going to do anything. You should be storing the point on WM_MOUSEMOVE (in a similar structure to pnt), and calling InvalidateRect() at that time. Then, do your painting in WM_PAINT. See this link for more info.


Try code something like this:

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜