在win32中绘制鼠标光标 [英] draw mouse cursor in win32
问题描述
GDI的新手.
我正在尝试模拟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 to 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()与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
.
这篇关于在win32中绘制鼠标光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!