0

Is there a way to disable the mouse hover effect for all controls of a winforms application? I know you can do it by looping over all controls, but I rather do it on application level.

The background is, that I want to disable the input for the whole application (which I did via IMessageFilter). The user should still be able to move the mouse but not be able to click it. This works perfectly fine, but the hover effect over controls remains.


Edit:

For example if you move your mouse over a button, it indicates that you can click it once the mouse is over that control

The application consists of multiple possible open form, which each can start a backgroundworker (thread) from which I need to control the applications behaviour. So I really like to avoid messing with GUI thread issues.

DerApe
  • 3,097
  • 2
  • 35
  • 55

2 Answers2

0

You could start with creating your own button class and overwrite the MouseEnter event:

 protected override void OnMouseEnter(EventArgs e)
 {
     if (shouldBeEnabled)
     {
         base.OnMouseEnter(e);
     }
 }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MeTitus
  • 3,390
  • 2
  • 25
  • 49
  • I dont want that for buttons but for all controls which are able to handle mouse over events – DerApe Apr 02 '13 at 10:13
  • OK. I presume you don't want to create all classes yourself, so here is the easiest way. On the OnLoad method of the form, loop through all its child controls, and remove the OnMouseEnter event and add your custom one. This or overriding that handler for all UI controls you're using. – MeTitus Apr 02 '13 at 10:27
  • well, thus a good idea, but want to disable and enable it on demand during runtime of the application... – DerApe Apr 02 '13 at 10:31
  • Add the custom event like I say before and add a boolean condition around the base method. Check the update. – MeTitus Apr 02 '13 at 10:35
  • But what if the form is loaded already and I want to disable it while it is open? The method you are describing is only called when loading the form right? – DerApe Apr 02 '13 at 12:57
  • You can try to use reflection and do this guys say here:http://stackoverflow.com/questions/6828054/how-would-that-be-possible-to-remove-all-event-handlers-of-the-click-event-of-a. I tried and it did not work, but have a go. – MeTitus Apr 02 '13 at 16:29
0

It seems to me that you simply want to disable the top level window. This will solve all your problems and remove the need for your IMessageFilter code. You end up with a minuscule amount of code.

You need to p/invoke the EnableWindow API call from user32:

[DllImport("user32.dll")]
static extern bool EnableWindow(IntPtr hWnd, bool bEnable);

You can then control the availability of your form like this:

EnableWindow(MyForm.Handle, false); // disable form
EnableWindow(MyForm.Handle, true);  // enable form
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks, I also played with that a little bit, but the problem is that my application handles multiple forms and also multiple threads from which I need the en/disableing. So I would need to keep track of all forms and also do an `Invoke` to get all form handles, which kind of scares me. If possible I dont want to get into the GUI thread issues – DerApe Apr 02 '13 at 13:16
  • Those issues are separate to this. Since you need to apply an application wide change, you'll need to deal with those issues no matter what. – David Heffernan Apr 02 '13 at 13:24
  • Well as for the mouse/keyboard disabling, I do not have to (at least not from the developers point of view). I just have to add instance of an implementation of `IMessageFilter` to my applications message filter `Application.AddMessageFilter()`. I was hoping there is an solution like this for the "hover" effect too...somehow manipulate the cursor or so... – DerApe Apr 02 '13 at 13:33
  • Fine. So you just find a way to do the rest of it. Good luck! – David Heffernan Apr 02 '13 at 13:33