gpt4 book ai didi

c++ - 来自另一个窗口的 HCURSOR,可能吗?

转载 作者:可可西里 更新时间:2023-11-01 10:22:13 25 4
gpt4 key购买 nike

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

这是我的代码:

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

它在我的窗口上绘制了一个光标,但它不是“真正的”Windows 光标。例如,当我在另一个窗口上看到手形鼠标图标时,我的图标没有改变。

所以我想知道是否可以从指定窗口 (HWND) 处理“真实”光标并绘制它。像 GetCursorOf(hwnd, &myCursorInfo) 这样的东西会很酷。

最佳答案

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

#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;
}

关于c++ - 来自另一个窗口的 HCURSOR,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25404305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com