在win32中绘制鼠标光标

dav*_*ave 3 mouse winapi gdi cursor

新手到GDI.

我正在尝试在Win32表单中模拟鼠标光标.在每个WM_MOUSEMOVE我都有

hCursor = LoadCursor(NULL, IDC_ARROW);
////Get device context
hDeviceContext = GetDC(hwnd);
hDCMem = CreateCompatibleDC(hDeviceContext);
hBitmap = CreateCompatibleBitmap(hDCMem, 50, 50);
hbmOld = SelectObject(hDCMem, hBitmap);
DrawIcon(hDCMem, x, y, hCursor);
SelectObject(hDCMem, hbmOld);
Run Code Online (Sandbox Code Playgroud)

但我没有看到任何被画出来的东西.但是,如果我直接画到DC:

    DrawIcon(hDeviceContext , x, y, hCursor);
Run Code Online (Sandbox Code Playgroud)

我确实看到了光标,但是当我移动光标时它不会擦除图像,留下一条长尾巴.

Jou*_*usi 5

不要画画WM_MOUSEMOVE,这WM_PAINT是为了什么.基本上,您需要处理三条消息:

    case WM_CREATE:
        hCursor = LoadCursor(NULL, IDC_ARROW);
        cWidth  = GetSystemMetrics(SM_CXCURSOR); // saving the cursor dimensions
        cHeight = GetSystemMetrics(SM_CYCURSOR);
    break;

    case WM_MOUSEMOVE:
        rcOld = rcNew;
        rcNew.left   = GET_X_LPARAM(lParam);     // saving the mouse coordinates
        rcNew.top    = GET_Y_LPARAM(lParam);
        rcNew.right  = rcNew.left + cWidth;
        rcNew.bottom = rcNew.top + cHeight;
        InvalidateRect(hwnd, &rcOld, TRUE);      // asking to redraw the rectangles
        InvalidateRect(hwnd, &rcNew, TRUE);
        UpdateWindow(hwnd);
    break;

    case WM_PAINT:
        hDC = BeginPaint(hwnd, &ps);
        DrawIcon(hDC, rcNew.left, rcNew.top, hCursor);
        EndPaint(hwnd, &ps);
    break;
Run Code Online (Sandbox Code Playgroud)

注意:我不确定"模拟鼠标光标"是什么意思,但可能有更好的方法来做你想要的.请检查功能SetCursor()SetWindowLongPtr() with GCL_HCURSOR.