绘制鼠标光标

编程入门 行业动态 更新时间:2024-10-25 20:21:14
本文介绍了绘制鼠标光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试以 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 00:40:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1126824.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   光标

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!