Detecting mousewheel over non-focused window? - c#

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?

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

Related

C# forward mouse events to another window without losing focus

My app screencaptures another window that runs on a second monitor. Now I'd also like to forward mouse clicks made in my app to that window. I tried using SendMessage in user32.dll for this, but this also makes window focus switch, which causes some issues, like the two windows rapidly fighting for focus. Is there are way to place those mouse events without making the hidden window active and losing focus on the main app?
Is there are way to place those mouse events without making the hidden window active and losing focus on the main app?
No, there is not even a way to forward mouse input to another receiver. Messages are only part of the input processing. The system also does internal bookkeeping and you cannot replicate that.
The only reliable way to inject input is by calling SendInput. Doing so doesn't allow you to specify a receiver. Input goes to whichever thread is determined to be the receiver by the system.
Although, more often than not, this question is asked when the problem that needs to be solved is a different one altogether: How do you automate a UI? The answer to that question is UI Automation.

Perform left mouse click from Background

I have desktop app that is in background. I later want to try this a a service too.
How can I perform mouse click when certain time and date is reached.
I don't want to move user mouse or something. Just send left mouse click from background.
Controling an application via simulated Mouse or Keyboard input goes into the Area of Desktop Automation. There are many existing solutions for it. I advise agaisnt making your own custom hack.
That asumes you do not have control over the target application of course. If that was the case, using a Timer is the obvious way.
As for doing this from a Service: That is nigh impossible. Ever since Windows Vista, Service do not get interactive Sessions by default.This is a rather important part of the UAC security. While getting around it is possible it will propably raise some red flags.

Detect whenever click event is fired in fully transparent window

I have a transparent controls and window where I can click through as it would not even exist. My question is: Is it possible to detect whenever click event is fired no matter where on the screen I click, while the wpf applicaton is running?
The reason why I need this is because I'm making an agent that will collect all information about actions made by user. Any suggestions, hacks, tricks will do.
You would need to create an event hook so that you could snoop all system wide messages before they are dispatched to the target window. Check the following link for more information...

Is there a way to "copy" the MessageBox functionality in Windows Mobile 6.5?

is there a way to "copy" the functionality of a MessageBox or DialogBox in Windows Mobile 6.5?
I've developed a custom message box (with paint events and blending etc) to make the UI look better. However, I don't know how to duplicate the MessageBox functionality, where the program "freezes" and "waits" for a response from the user (usually pressing OK or Cancel button).
Right now, as a workaround, I disable the entire screen so that only the message box will receive any key presses. Then have a custom event called OnMessageBoxClosed.
I then put the actual code to be executed based on the choice made by the user (OK or Cancel) in the event handler for the event.
I would prefer if its possible to make this custom message box behave like a regular messagebox.
Thanks!

How To Keep Checking For Something Without a Timer?

I need to be able to check if the mouse is within a certain area on the form continuously. I want to be able to do this without the use of a timer, though. How would I go about doing this?
I'm using C# btw.
Have you tried attaching a handler to the MouseMove event, and checking on each movement?
If the area is a screen control, you can use MouseEnter, MouseLeave, MouseHover and MouseMove events.
I think the mouse events suggested by others is the best solution, but as another alternative to timers, you could write a small function to check the mouse and then keep invoking it on your main window dispatcher with an "application idle" priority. This will continuously run your check without freezing the UI.
Again, hooking into the mousemove event is still a cleaner solution, IMO.
If hooking the MouseMove event triggers too often - or if you want to avoid hooking that event on every form, consider hooking the Application.Idle event instead.
This event fires every time the application is about to go idle - all pending messages (including repaints) have been processed and there's nothing left to do. In most WinForms applications, this happens several times a second, providing a good way to do "just in time" processing.
You need to define events over that area.
Use OnMouseEnter and OnMouseLeave together to decide if mouse is in this area or not !
OnMouseEnter until OnMouseLeave means that mouse is still in that area.

Categories