我需要在应用程序启动后立即隐藏鼠标光标。我为此使用ShowCursor(FALSE)。但通常在ShowCursor(FALSE)后,光标保持在屏幕上,直到鼠标移动。我和其他人在不同的电脑上用XP从10到10的Windows重现了这一点。为了重现这一点,只需双击可执行文件即可从Windows资源管理器启动该应用程序,绝对不要在双击之后移动鼠标。如果应用程序以任何其他方式启动,则按预期方式将光标隐藏起来。看起来,当它停留在屏幕上时,光标属于Windows资源管理器,有时除了屏幕上的光标仍然是资源管理器工具提示。在应用程序启动后立即使用ShowCursor(FALSE)隐藏鼠标光标
这并不总是工作代码:
#include <Windows.h>
LRESULT __stdcall WndProcFull(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg==WM_DESTROY)
{
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int __stdcall wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pszCmdLine, int iCmdShow)
{
MSG msg;
HWND hWnd;
PCWSTR pszFullName=L"FullWindow";
WNDCLASSEX wc={sizeof (WNDCLASSEX), 0, WndProcFull, 0, 0, hInstance, 0, LoadCursor(0, IDC_ARROW), (HBRUSH)GetStockObject(BLACK_BRUSH), 0, pszFullName, 0};
RegisterClassEx(&wc);
hWnd=CreateWindow(pszFullName, L"HideCursor", WS_POPUP | WS_MAXIMIZE | WS_VISIBLE, 0, 0, 0, 0, 0, 0, hInstance, 0);
UpdateWindow(hWnd);
ShowCursor(FALSE);
while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg);
return 0;
}
的源代码在任何VS.编译
目前唯一有帮助的是设置计时器,并在计时器间隔到期后,以编程方式移动鼠标光标,然后隐藏。但我宁愿找到更合适和稳定的解决方案。
代码与替代方法:
#include <Windows.h>
#define HIDECURSOR_TIMER_ID 1
LRESULT __stdcall WndProcFull(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_TIMER:
POINT pt;
GetCursorPos(&pt);
SetCursorPos(pt.x+1, pt.y);
KillTimer(hWnd, HIDECURSOR_TIMER_ID);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int __stdcall wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pszCmdLine, int iCmdShow)
{
MSG msg;
HWND hWnd;
PCWSTR pszFullName=L"FullWindow";
WNDCLASSEX wc={sizeof (WNDCLASSEX), 0, WndProcFull, 0, 0, hInstance, 0, LoadCursor(0, IDC_ARROW), (HBRUSH)GetStockObject(BLACK_BRUSH), 0, pszFullName, 0};
RegisterClassEx(&wc);
hWnd=CreateWindow(pszFullName, L"HideCursor", WS_POPUP | WS_MAXIMIZE | WS_VISIBLE, 0, 0, 0, 0, 0, 0, hInstance, 0);
UpdateWindow(hWnd);
ShowCursor(FALSE);
SetTimer(hWnd, HIDECURSOR_TIMER_ID, 200, 0);
while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg);
return 0;
}
200毫秒是最小超时值在此,光标稳定消失。
当我在没有定时器黑客的情况下尝试时,工作得很好。做什么都行。 –
可能你在双击可执行文件时略微移动了鼠标?另外两个人在停止移动鼠标时也能够在其他计算机上重现这一点。 – Mogod
明显的问题:为什么你注册一个窗口类,它有一个非null的游标?你为什么不处理[WM_SETCURSOR](https://msdn.microsoft.com/en-us/library/windows/desktop/ms648382.aspx)? – IInspectable