2015-10-25 80 views
2

我需要在应用程序启动后立即隐藏鼠标光标。我为此使用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毫秒是最小超时值在此,光标稳定消失。

+0

当我在没有定时器黑客的情况下尝试时,工作得很好。做什么都行。 –

+0

可能你在双击可执行文件时略微移动了鼠标?另外两个人在停止移动鼠标时也能够在其他计算机上重现这一点。 – Mogod

+0

明显的问题:为什么你注册一个窗口类,它有一个非null的游标?你为什么不处理[WM_SETCURSOR](https://msdn.microsoft.com/en-us/library/windows/desktop/ms648382.aspx)? – IInspectable

回答

1

根据the documentation

如果bShow是TRUE,则显示计数递增一。如果bShow为FALSE,则显示计数减1。

所以隐藏光标,你想是这样的:

while(ShowCursor(FALSE) >= 0); 
+0

已经尝试过,没有帮助。光标保持在屏幕上,直到鼠标移动,即使是负显示计数器。我认为这个游标属于资源管理器,而不是我的应用程序。 – Mogod

+0

我会处理'WM_SETCURSOR'然后作为@IInspectable暗示。也许你没有别的选择。 – ProXicT

+0

我已经试过处理WM_SETCURSOR,它也没有帮助。 – Mogod

2

CreateWindow函数返回时,仍然没有显示在屏幕上的窗口。你应该在早期窗口创建时间之后立即调用ShowCursor函数。恕我直言,最好的地方是当处理WM_CREATE消息。代码将变为:

#include <Windows.h> 

LRESULT __stdcall WndProcFull(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
    if (uMsg==WM_DESTROY) 
    { 
     PostQuitMessage(0); 
     return 0; 
    } 
    else if (uMsg == WM_CREATE) { 
     ShowCursor(FALSE); 
    } 
    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; 
} 

这样一旦窗口被创建并实现,您就真的隐藏了光标。

+0

在WM_CREATE和WM_SHOWWINDOW处理程序中尝试ShowCursor(FALSE),也没有帮助。出于某种原因,我无法以编程方式隐藏此光标而没有150-200毫秒的延迟。 – Mogod

+0

[WM_CREATE](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632619.aspx)在**'CreateWindow [Ex]'返回之前发送到窗口过程**。 – IInspectable

相关问题