Detecting mousewheel over non-focused window?

前提是你 提交于 2019-12-06 12:51:10

问题


My goal is to make a floating toolbar (as its own C# application), and when the user uses the scrollwheel over me I want to change the buttons that are visible. Sounds easy enough, should just be a matter of this one-liner:

MouseWheel += new MouseEventHandler(Form1_MouseWheel);

The problem I am having is that the mouse wheel handler is only invoked when my application has focus. That means the user has to first click, and then mousewheel. That won't do for what I'm trying to do.

I can hook the MouseHover event handler and call form.Activate() then, to get focus. That's suboptimal because if the user uses the scrollwheel immediately after mousing over my application (instead of waiting a little), the focus will still be on the previous app and it'll get the mousewheel event.

A natural thing to do would be to hook the MouseEnter event and call Activate() there, but then instead of my application coming to the front, its icon starts to blink on the task bar. I'm using Win7, but this problem is probably older than this.

Ideally, what I'd like to do would be to detect the mousewheel events without having to worry about whether my application has focus. It would really be better for the previous application to keep input focus, so for example if the user's in Notepad they can type, mouse over to my app, use the scroll wheel, look at what they see and decide to just resume typing in Notepad. Ideally I don't want them to have to click even once in this scenario.

I'll settle for a solution that switches focus to my application, though, if there's no other way.

What I have so far uses C# and Windows Forms, but I'd be open to using something different if that can solve my problems.

So: how can I see those mousewheel events without the user having to click to focus my application first?


回答1:


If you need to catch mouse events outside your application, you can use a global system hook. There's a good .NET implementation here



来源:https://stackoverflow.com/questions/1466702/detecting-mousewheel-over-non-focused-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!