Reputation: 2541
I am writing a Windows application in C.
I am hiding the mouse cursor in the client area of the window by handling the WM_SETCURSOR
message:
case WM_SETCURSOR:
{
static BOOL HideCursor = FALSE;
if ((LOWORD(LParam) == HTCLIENT) && !HideCursor)
{
HideCursor = TRUE;
ShowCursor(FALSE);
}
else if ((LOWORD(LParam) != HTCLIENT) && HideCursor)
{
HideCursor = FALSE;
ShowCursor(TRUE);
}
Result = DefWindowProc(Window, Message, WParam, LParam);
break;
}
This works fine, but it is a little awkward, because the mouse cursor disappears instantly as soon as it crosses into the client area of the window. The user can easily lose track of where the mouse cursor "should be" when he or she is trying to move the cursor toward the toolbar buttons, or manually resize the window, for example.
How can I add a second or two of delay in there so that the user can move the mouse over the client area of the window without the mouse instantly disappearing, but if the mouse cursor hovers in the client area for more than a second or two, it disappears?
Upvotes: 3
Views: 467
Reputation: 5664
When you are notified that the mouse cursor entered the window, you could use SetTimer(hWnd, ID_MOUSE_TIMER, 2000, NULL)
where hWnd
is your window handle and ID_MOUSE_TIMER
is an arbitrary identifier for a timer, to create a timer that will fire after 2000 milliseconds.
You can then respond to the WM_TIMER
message and hide the cursor just as you did before, but also use KillTimer(hWnd, ID_MOUSE_TIMER)
to prevent further calls.
When the mouse cursor leaves your window, you should also destroy the timer, and also restore visibility of the mouse cursor if it was hidden just as you did before.
To read up on timers, check the corresponding section in the MSDN.
Upvotes: 5