Discussion:
UserControl, capture mousewheel without focus
(too old to reply)
Andreas
2008-03-14 07:44:26 UTC
Permalink
Hi,

I'm developing a usercontrol and I would like to be able to catch the
MouseWheel events in it even though it does not have focus (i.e without me
having to click it first). I would *only* like to capture the events if the
mouse pointer is over the control though. So what I want is some sort of
hover tracking + mouse wheel capturing.

I tried overriding the WndProc and listen on the WM_MouseWheel message, but
it's also only captured if the control has focus so it didn't work. Does
anyone know how to do this? I'm developing in C# but I have no problem with
example code in VB.NET

Thanks!
unknown
2010-04-24 02:34:45 UTC
Permalink
Here is what I did.

I need a mouse wheel event while the mouse pointer is over a panel. This panel is going to hold a Graphics Object and I want to be able to change custom cursors using the mouse wheel.

Don't ask me why, it is convoluted.

Anyways I created a boolean variable (boolMouseIn).

Then when the mouse entered the panel I caught it using a MouseEnter event. This set boolMouseIn = True.

When the mouse pointer left the panel then a MouseLeave event set boolMouseIn = False.

Then the myBase.MouseWheel first tested to see if boolMouseIn = False (Then Exit Sub).

Just for verification of the MouseWheel Capture I put a label in a StatusStrip to show the e.delta value.

Now the e.delta only increments or decrements while the mouse is inside the panel and panels can't get the focus.

Example code follows:

#Region " Module Variables "
Dim boolMouseIn as Boolean
#End Region

Private Sub MyBase_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseWheel

If boolMouseIn = False Then Exit Sub

Dim intMouseWheel As Integer = CInt(e.Delta / 120)

lblStatusStrip.Text = intMouseWheel
End Sub

Private Sub pnlTarget_MouseEnter1(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnlTarget.MouseEnter

boolMouseIn = True

End Sub

Private Sub pnlTarget_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnlTarget.MouseLeave

boolMouseIn = False

End Sub



Andreas wrote:

UserControl, capture mousewheel without focus
14-Mar-08

Hi,

I'm developing a usercontrol and I would like to be able to catch the
MouseWheel events in it even though it does not have focus (i.e without me
having to click it first). I would *only* like to capture the events if the
mouse pointer is over the control though. So what I want is some sort of
hover tracking + mouse wheel capturing.

I tried overriding the WndProc and listen on the WM_MouseWheel message, but
it's also only captured if the control has focus so it didn't work. Does
anyone know how to do this? I'm developing in C# but I have no problem with
example code in VB.NET

Thanks!

Previous Posts In This Thread:

On Friday, March 14, 2008 3:44 AM
Andreas wrote:

UserControl, capture mousewheel without focus
Hi,

I'm developing a usercontrol and I would like to be able to catch the
MouseWheel events in it even though it does not have focus (i.e without me
having to click it first). I would *only* like to capture the events if the
mouse pointer is over the control though. So what I want is some sort of
hover tracking + mouse wheel capturing.

I tried overriding the WndProc and listen on the WM_MouseWheel message, but
it's also only captured if the control has focus so it didn't work. Does
anyone know how to do this? I'm developing in C# but I have no problem with
example code in VB.NET

Thanks!


Submitted via EggHeadCafe - Software Developer Portal of Choice
Using VSTO Add-In To Automate Frequent Excel 2007 Tasks
http://www.eggheadcafe.com/tutorials/aspnet/ff2d1d4b-aedf-4d14-9e60-39a86ccab5d6/using-vsto-addin-to-auto.aspx
Loading...