另一个窗口的HCURSOR可能吗?

时间:2014-08-20 12:09:07

标签: c++ windows cursor dwm

我在窗口寻找抽奖HCURSOR。光标必须是来自另一个窗口(HWND)的真实光标。

这是我的代码:

GetCursorPos(&pos);
ScreenToClient(hwnd, &pos);
DrawIcon(hdcMemDC, pos.x, pos.y,GetCursor());

它在我的窗口上画了一个光标,但它不是"真实的" Windows游标。例如,当我在另一个窗口上有一个手形鼠标图标时,我没有改变。

所以我想知道是否有可能处理"真实"来自指定窗口(HWND)的光标并绘制它。像GetCursorOf(hwnd, &myCursorInfo)之类的东西会很酷。

1 个答案:

答案 0 :(得分:2)

此代码将获取任何窗口的光标。然后它将创建一个线程,该线程将不断将光标绘制到我们的窗口上。

#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif

#include <tchar.h>
#include <windows.h>

DWORD WINAPI BltThreadProc(void *lpParam)
{
    while(true)
    {
        if (!IsIconic((HWND)lpParam)) //if our window isn't minimised, then we get the cursor and draw it.. Makes no sense drawing on a minimised window.
        {
            CURSORINFO Info = {0};
            Info.cbSize = sizeof(Info);
            GetCursorInfo(&Info);

            HDC hDC = GetDC((HWND)lpParam);
            DrawIconEx(hDC, 0, 0, Info.hCursor, 0, 0, 0, (HBRUSH)GetStockObject(COLOR_BACKGROUND), DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE);
            ReleaseDC((HWND)lpParam, hDC);
        }

        Sleep(1);
    }
    return 0;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_CREATE:
        {
            CreateThread(NULL, 0, BltThreadProc, hwnd, 0, 0);
        }
        break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = _T("CLS");
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof(WNDCLASSEX);
    wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH)GetStockObject(COLOR_BACKGROUND);

    if (!RegisterClassEx (&wincl))
        return 0;

    hwnd = CreateWindowEx(0, _T("CLS"), _T("Title"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL, hThisInstance, NULL);
    ShowWindow(hwnd, nCmdShow);
    while (GetMessage(&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}