简体   繁体   English

如何在WPF / C#中检测鼠标是否在我的窗口范围内移动?

[英]How can i detect if the mouse is moving both within and out of the bounds of my window in WPF/C#?

I have a small video app that i would like to give vlc-like functionality. 我有一个小视频应用程序,我想提供类似vlc的功能。 By that, I mean having a certain element disappear and appear on mouse move or mouse not moving. 通过这个,我的意思是有一个元素消失,并出现在鼠标移动或鼠标不移动。 I have a rough concept of how to do this but I have no ideea how i coul detect if the mouse is moving or not. 我有一个如何做到这一点的粗略概念,但我不知道我如何检测鼠标是否移动。 I thought about using the GetPosition function but that will just give me the mouse's position and won't let me know if the cursor is moving or not. 我想过使用GetPosition函数,但这只会给我鼠标的位置,并且不会让我知道光标是否移动。 I would like to use a timer to count down 2-3 seconds after the mouse has stopped moving and then either fade out the control or just make it collapse without further ado. 我想在鼠标停止移动后使用计时器倒计时2-3秒,然后淡出控件或者让它崩溃而不再赘述。 I can't check the value of the position variable every milisecond. 我无法每毫秒检查位置变量的值。 Is there some other way to do this ? 还有其他方法可以做到这一点吗?

You can use a hook functionality so you will be notified when a mouse move, I use to use this free opensource library. 您可以使用钩子功能,以便在鼠标移动时通知您,我使用这个免费的开源库。

How to use it: 如何使用它:

using Gma.UserActivityMonitor.GlobalEventProvider;

GlobalEventProvider _globalEventProvider1 
                                 = new Gma.UserActivityMonitor.GlobalEventProvider();

this.globalEventProvider1.MouseMove += HookMouseMove;//to listen to mouse move

Capture the mouse. 捕获鼠标。 Release the capture if the user actually clicks anywhere else. 如果用户实际点击其他任何位置,则释放捕获。 Then you can use the standard WPF mouse move event. 然后您可以使用标准的WPF鼠标移动事件。

myElement.MouseMove += (my MouseMove handler)
Mouse.Capture(myElement);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM