绘制鼠标光标 [英] Draw mouse cursor

查看:53
本文介绍了绘制鼠标光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以 Win32 形式模拟鼠标光标.在我拥有的每个 WM_MOUSEMOVE

I'm trying to simulate a mouse cursor in Win32 forms. On every WM_MOUSEMOVE I have

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

但是我没有看到任何东西被绘制出来.但是,如果我直接在 DC 上绘图:

But I don't see anything being drawn. However if I drew directly on the DC:

DrawIcon(hDeviceContext, x, y, hCursor);

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

I do see the cursor but it does not erase the image as I move the cursor, leaving a long tail behind.

推荐答案

不要在 WM_MOUSEMOVE 中绘画,这就是 WM_PAINT 的用途.基本上,您需要处理三个消息:

Don't paint in WM_MOUSEMOVE, that's what WM_PAINT is for. Basically, you need to handle three messages:

    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;

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

Note: I'm not sure what do you mean by "simulating a mouse cursor", but there could be a better way of doing what you probably want. Please check functions SetCursor() and SetWindowLongPtr() with GCL_HCURSOR.

这篇关于绘制鼠标光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆