这是个画直线程序的窗口过程,我想实现当鼠标离开窗口时能立即画线,鼠标往窗口右边和下边拖的时候都没问题,
但是当鼠标拖到窗口左边缘的时候往左的直线突然连通窗口右边了,向上也一样,各种不懂。求解释啊
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static vector<POINT> vec;
static POINT spt;
static BOOL fPaint;
//static int count = 0;
int wmId, wmEvent;
POINT pt;
PAINTSTRUCT ps;
HDC hdc; switch (message)
{
case WM_COMMAND:
wmId    = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_LBUTTONDOWN:
SetCapture(hWnd);
SetCursor(LoadCursor(NULL, IDC_CROSS));
spt.x = LOWORD(lParam);
spt.y = HIWORD(lParam);
vec.push_back(spt);
fPaint = TRUE;
return 0;
case WM_MOUSEMOVE:
if(fPaint)
{
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
if(vec.back().x != spt.x || vec.back().y != spt.y)
{
vec.pop_back();
}
vec.push_back(pt);
InvalidateRect(hWnd, NULL, TRUE);
}
return 0;
case WM_LBUTTONUP:
SetCursor(LoadCursor(NULL, IDC_ARROW));
fPaint = FALSE;
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);

if(pt.x != spt.x || pt.y != spt.y)
{
vec.pop_back();
vec.push_back(pt);
}
else
{
vec.pop_back();
}
InvalidateRect(hWnd, NULL, TRUE);
return 0;
case WM_MOUSELEAVE:
if(fPaint)
{
GetCursorPos(&pt);
SendMessage(hWnd, WM_LBUTTONUP, 0, pt.y << 16 + pt.x);
ReleaseCapture();
}
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
for(vector<POINT>::iterator iter = vec.begin(); iter != vec.end();)
{
MoveToEx(hdc, iter->x, iter->y, NULL);
iter++;
LineTo(hdc, iter->x, iter->y);
iter++;
}

EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE; case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}

解决方案 »

  1.   


    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    static vector<POINT> vec;
    static POINT spt;
    static BOOL fPaint;
    //static int count = 0;
    int wmId, wmEvent;
    POINT pt;
    PAINTSTRUCT ps;
    HDC hdc;switch (message)
    {
    case WM_COMMAND:
    wmId = LOWORD(wParam);
    wmEvent = HIWORD(wParam);
    // Parse the menu selections:
    switch (wmId)
    {
    case IDM_ABOUT:
    DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
    break;
    case IDM_EXIT:
    DestroyWindow(hWnd);
    break;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
    case WM_LBUTTONDOWN:
    SetCapture(hWnd);
    SetCursor(LoadCursor(NULL, IDC_CROSS));
    spt.x = LOWORD(lParam);
    spt.y = HIWORD(lParam);
    vec.push_back(spt);
    fPaint = TRUE;
    return 0;
    case WM_MOUSEMOVE:
    if(fPaint)
    {
    pt.x = LOWORD(lParam);
    pt.y = HIWORD(lParam);
    if(vec.back().x != spt.x || vec.back().y != spt.y)
    {
    vec.pop_back();
    }
    vec.push_back(pt);
    InvalidateRect(hWnd, NULL, TRUE);
    }
    return 0;
    case WM_LBUTTONUP:
    SetCursor(LoadCursor(NULL, IDC_ARROW));
    fPaint = FALSE;
    pt.x = LOWORD(lParam);
    pt.y = HIWORD(lParam);if(pt.x != spt.x || pt.y != spt.y)
    {
    vec.pop_back();
    vec.push_back(pt);
    }
    else
    {
    vec.pop_back();
    }
    InvalidateRect(hWnd, NULL, TRUE);
    return 0;
    case WM_MOUSELEAVE:
    if(fPaint)
    {
    GetCursorPos(&pt);
    SendMessage(hWnd, WM_LBUTTONUP, 0, pt.y << 16 + pt.x);
    ReleaseCapture();
    }
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    for(vector<POINT>::iterator iter = vec.begin(); iter != vec.end();)
    {
    MoveToEx(hdc, iter->x, iter->y, NULL);
    iter++;
    LineTo(hdc, iter->x, iter->y);
    iter++;
    }EndPaint(hWnd, &ps);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
    }// Message handler for about box.
    INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
    return (INT_PTR)TRUE;case WM_COMMAND:
    if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    {
    EndDialog(hDlg, LOWORD(wParam));
    return (INT_PTR)TRUE;
    }
    break;
    }
    return (INT_PTR)FALSE;
    }