Win32:测试鼠标是否悬停了一秒钟
Win32: Test if mouse has hovered for a second
我正在尝试创建一个函数,当鼠标在同一位置停留一段时间而不停止程序时,该函数仅 运行s(因此没有 Sleep(1000)
函数)。我尝试做的是在鼠标移动时让回调 运行 成为绘画中的一个函数。在该函数中,将有一个 while 循环,以确保鼠标自上一帧后没有移动,并在单击或移动时中断。
回调中:
case WM_MOUSEMOVE:
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
iPosX = pt.x;
iPosY = pt.y;
if (!mouseMoved) {
mouseMoved = true;
period = 0;
InvalidateRect(hWnd, 0, FALSE);
}
break;
在画图中:
if (mouseMoved && !MouseClicked) {
test.newBit(1, 100, 0, hdc); //Draws a picture as a test
oldx = iPosX;
oldy = iPosY;
period = 0;
while (period <= 1000000 && !MouseClicked) {
oldx = iPosX;
oldy = iPosY;
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
iPosX = pt.x;
iPosY = pt.y;
if (iPosX != oldx || iPosY != oldy) {
mouseMoved = false;
period = 0;
break;
}
period++;
}
if (period <= 1000000) {
period = 0;
test.newBit(0, 0, 0, hdc); //Draws a picture as a test
mouseMoved = false;
}
}
这会在您移动鼠标并单击时生成随机闪烁。对我做错了什么的任何见解将不胜感激。
系统已经为您实现了。您只需通过调用 TrackMouseEvent:
来请求鼠标悬停消息
TRACKMOUSEEVENT tme = { sizeof( tme ) };
tme.dwFlags = TME_HOVER;
tme.hwndTrack = hWnd;
tme.dwHoverTime = 1000;
::TrackMouseEvent( &tme );
当光标悬停在 window 的客户区上达到 TrackMouseEvent
调用中指定的时间段时,应用程序会收到 WM_MOUSEHOVER 消息。一旦生成 WM_MOUSEHOVER
消息,悬停跟踪就会停止。要接收更多 WM_MOUSEHOVER
消息,应用程序需要重新请求悬停跟踪(使用与上述相同的代码)。
应用程序可以调用 SystemParametersInfo 并使用 SPI_GETMOUSEHOVERTIME
来检索默认悬停超时。
请注意,这会将悬停矩形考虑在内(SPI_GETMOUSEHOVERWIDTH
和 SPI_GETMOUSEHOVERHEIGHT
)。这是鼠标光标周围的区域,允许鼠标在其中移动,无需重置悬停超时。
如果您需要一个只在鼠标根本没有移动时报告悬停的解决方案,您将不得不自己实现它。基于计时器的解决方案是一个干净的解决方案。
每当鼠标移动时,我们都需要重置计时器。如果鼠标之前在 window 的客户区外,我们也必须重新请求 WM_MOUSELEAVE
消息。这是必要的,以便在鼠标离开客户区时可以取消计时器:
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) {
enum TimerId { TimerId_MouseHover = 1 };
static const UINT HoverTimeoutInMs = 1000;
static int PrevX = INT_MIN;
static int PrevY = INT_MIN;
static bool IsMouseOutside = true;
static bool IsMouseHovered = false;
switch ( message ) {
case WM_MOUSEMOVE: {
// If mouse was previously outside, re-request WM_MOUSELEAVE messages
if ( IsMouseOutside ) {
TRACKMOUSEEVENT tme = { sizeof( tme ) };
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hWnd;
::TrackMouseEvent( &tme );
IsMouseOutside = false;
}
int CurrX = GET_X_LPARAM( lParam );
int CurrY = GET_Y_LPARAM( lParam );
if ( ( CurrX != PrevX ) || ( CurrY != PrevY ) ) {
// Mouse moved -> reset timer
::SetTimer( hWnd, TimerId_MouseHover, HoverTimeoutInMs, nullptr );
PrevX = CurrX;
PrevY = CurrY;
IsMouseHovered = false;
// For testing only:
::InvalidateRect( hWnd, nullptr, FALSE );
}
return 0;
}
只要鼠标离开客户区,我们就需要取消计时器。如果我们不取消计时器,即使鼠标移出 window 的客户区,它也会过期:
case WM_MOUSELEAVE:
::KillTimer( hWnd, TimerId_MouseHover );
IsMouseOutside = true;
PrevX = INT_MIN;
PrevY = INT_MIN;
return 0;
如果我们的计时器到期,我们将有一个悬停事件:
case WM_TIMER:
if ( wParam == TimerId_MouseHover ) {
// The mouse hasn't been moved for the specified timeout:
// This is a hover event
IsMouseHovered = true;
// For testing only:
::InvalidateRect( hWnd, nullptr, FALSE );
return 0;
}
else {
return ::DefWindowProc( hWnd, message, wParam, lParam );
}
作为测试,让我们在悬停超时到期时用黑色画笔填充客户区:
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint( hWnd, &ps );
::FillRect( hdc, &ps.rcPaint, GetStockBrush( IsMouseHovered ? BLACK_BRUSH :
WHITE_BRUSH ) );
EndPaint( hWnd, &ps );
return 0;
}
我正在尝试创建一个函数,当鼠标在同一位置停留一段时间而不停止程序时,该函数仅 运行s(因此没有 Sleep(1000)
函数)。我尝试做的是在鼠标移动时让回调 运行 成为绘画中的一个函数。在该函数中,将有一个 while 循环,以确保鼠标自上一帧后没有移动,并在单击或移动时中断。
回调中:
case WM_MOUSEMOVE:
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
iPosX = pt.x;
iPosY = pt.y;
if (!mouseMoved) {
mouseMoved = true;
period = 0;
InvalidateRect(hWnd, 0, FALSE);
}
break;
在画图中:
if (mouseMoved && !MouseClicked) {
test.newBit(1, 100, 0, hdc); //Draws a picture as a test
oldx = iPosX;
oldy = iPosY;
period = 0;
while (period <= 1000000 && !MouseClicked) {
oldx = iPosX;
oldy = iPosY;
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
iPosX = pt.x;
iPosY = pt.y;
if (iPosX != oldx || iPosY != oldy) {
mouseMoved = false;
period = 0;
break;
}
period++;
}
if (period <= 1000000) {
period = 0;
test.newBit(0, 0, 0, hdc); //Draws a picture as a test
mouseMoved = false;
}
}
这会在您移动鼠标并单击时生成随机闪烁。对我做错了什么的任何见解将不胜感激。
系统已经为您实现了。您只需通过调用 TrackMouseEvent:
来请求鼠标悬停消息TRACKMOUSEEVENT tme = { sizeof( tme ) };
tme.dwFlags = TME_HOVER;
tme.hwndTrack = hWnd;
tme.dwHoverTime = 1000;
::TrackMouseEvent( &tme );
当光标悬停在 window 的客户区上达到 TrackMouseEvent
调用中指定的时间段时,应用程序会收到 WM_MOUSEHOVER 消息。一旦生成 WM_MOUSEHOVER
消息,悬停跟踪就会停止。要接收更多 WM_MOUSEHOVER
消息,应用程序需要重新请求悬停跟踪(使用与上述相同的代码)。
应用程序可以调用 SystemParametersInfo 并使用 SPI_GETMOUSEHOVERTIME
来检索默认悬停超时。
请注意,这会将悬停矩形考虑在内(SPI_GETMOUSEHOVERWIDTH
和 SPI_GETMOUSEHOVERHEIGHT
)。这是鼠标光标周围的区域,允许鼠标在其中移动,无需重置悬停超时。
如果您需要一个只在鼠标根本没有移动时报告悬停的解决方案,您将不得不自己实现它。基于计时器的解决方案是一个干净的解决方案。
每当鼠标移动时,我们都需要重置计时器。如果鼠标之前在 window 的客户区外,我们也必须重新请求 WM_MOUSELEAVE
消息。这是必要的,以便在鼠标离开客户区时可以取消计时器:
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) {
enum TimerId { TimerId_MouseHover = 1 };
static const UINT HoverTimeoutInMs = 1000;
static int PrevX = INT_MIN;
static int PrevY = INT_MIN;
static bool IsMouseOutside = true;
static bool IsMouseHovered = false;
switch ( message ) {
case WM_MOUSEMOVE: {
// If mouse was previously outside, re-request WM_MOUSELEAVE messages
if ( IsMouseOutside ) {
TRACKMOUSEEVENT tme = { sizeof( tme ) };
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hWnd;
::TrackMouseEvent( &tme );
IsMouseOutside = false;
}
int CurrX = GET_X_LPARAM( lParam );
int CurrY = GET_Y_LPARAM( lParam );
if ( ( CurrX != PrevX ) || ( CurrY != PrevY ) ) {
// Mouse moved -> reset timer
::SetTimer( hWnd, TimerId_MouseHover, HoverTimeoutInMs, nullptr );
PrevX = CurrX;
PrevY = CurrY;
IsMouseHovered = false;
// For testing only:
::InvalidateRect( hWnd, nullptr, FALSE );
}
return 0;
}
只要鼠标离开客户区,我们就需要取消计时器。如果我们不取消计时器,即使鼠标移出 window 的客户区,它也会过期:
case WM_MOUSELEAVE:
::KillTimer( hWnd, TimerId_MouseHover );
IsMouseOutside = true;
PrevX = INT_MIN;
PrevY = INT_MIN;
return 0;
如果我们的计时器到期,我们将有一个悬停事件:
case WM_TIMER:
if ( wParam == TimerId_MouseHover ) {
// The mouse hasn't been moved for the specified timeout:
// This is a hover event
IsMouseHovered = true;
// For testing only:
::InvalidateRect( hWnd, nullptr, FALSE );
return 0;
}
else {
return ::DefWindowProc( hWnd, message, wParam, lParam );
}
作为测试,让我们在悬停超时到期时用黑色画笔填充客户区:
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint( hWnd, &ps );
::FillRect( hdc, &ps.rcPaint, GetStockBrush( IsMouseHovered ? BLACK_BRUSH :
WHITE_BRUSH ) );
EndPaint( hWnd, &ps );
return 0;
}