views:

467

answers:

4

... Is it possible to create hot-spots in C# so that when the mouse is over a certain area an event gets triggered?

Thank you :o)

+3  A: 

Create a transparent Panel (truly transparent - by setting the WS_EX_TRANSPARENT bit in its extended window style - here's how), put it in the position you want on top of other controls, and handle MouseMove on it.

Pavel Minaev
There's no such thing as a truly transparent panel in WinForms. If you create a panel as you describe and overlap it with other controls / other panels, you will actually see the form background and not the controls "behind" your panel.
Eric J.
@Eric, please read my answer once again, attentively - particularly the bit about "truly transparent" and WS_EX_TRANSPARENT. Then follow the link in the answer and read that. I'm specifically _not_ talking about setting `Background` to `Transparent`, and I was careful to make this clear in the answer.
Pavel Minaev
+1  A: 

Add a MouseHover event handler for the control(s) that you want your hotspot over.

Eric J.
Thank you, but I don't have any controls where I want the hot spot. The hot spot will be on the form's background somewhere...
baeltazor
See Sequ3L's comment, you can handle that event on your form.
Eric J.
baeltazor: The Form is a Control too.
Henk Holterman
+3  A: 
Mr. Smith
This is fine in theory, but usually one wants the hot spots to overlap with some controls. And then you've got to handle `MouseMove` for _all_ such controls, and convert mouse coordinates accordingly. And, unlike WPF, there's no way to get "preview" mouse events in WinForms...
Pavel Minaev
Thanks for this, very helpful!
baeltazor
A: 

You can use WndProc to capture windows messages, or you could use GetCursorPos to get the cursor position on the screen.

Bogdan Maxim
And what, exactly, does this give over just handling `MouseMove`? Why involve Win32 API here at all (note that the question is about C# / WinForms)?
Pavel Minaev
WinForms doesn't have "direct" support for this kind of action. The first problem with the panel is that you can't have a non-rectangular region for capturing events.The second problem is that if you need multiple "regions", you might have to add more panel transparent panel controls, thus increasing form clutter. Also, some controls don't support receiving events if there is another control over, even though it is transparent.
Bogdan Maxim
If you want to have something light, you might want to consider using Win32 API.
Bogdan Maxim