We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Is there a way to capture the mouse events of objects on a form

Hello, How do I handle the mouse events of controls on a form similar to how the keyboard events are processed then the form's keypreview property is set to true? Thanks, Ryan

3 Replies

PR Praveen Ramesh April 8, 2004 01:24 PM UTC

You can listen to the control''s mouse messages through their MouseUp, MouseDown, etc. events as you probably know already. But, those events are fired after they get processed by the control itself. To "preview" these events, you could set up a filter in your Form by implementing the IMessageFilter interface. See .net class ref for that interface. -Praveen

RM Ryan Milligan April 8, 2004 04:22 PM UTC

Yes, this is what I am looking for. I have a form with an axwebbrowser control which does not have a click event. I need to be able to recognize when the xbuttons have been clicked so I can call the back() and forward() methods of the browser. Could anyone please provide an example of how this can be accomplished.

AR Anthon Ryan Milligan April 14, 2004 01:53 PM UTC

I figured it out using the information you provided. It works great. Below is my code: Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim filter As New MyFilter() filter.frm = Me Application.AddMessageFilter(filter) End Sub Public Class MyFilter Implements IMessageFilter 'gets the left mouse button messages Public frm As frmMain Public Const WM_XMOUSEDOWN As Integer = 523 Public Const WM_XMOUSEUP As Integer = 524 Public Const WM_XMOUSEDBL As Integer = 525 Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage If m.Msg = WM_XMOUSEDOWN Or m.Msg = WM_XMOUSEUP Then frm.frmMain_MouseUp(frm.ActiveBrowser, New System.Windows.Forms.MouseEventArgs(frm.MouseButtons, 1, frm.MousePosition.X, frm.MousePosition.Y, 0)) Return (True) End If Return (False) End Function End Class

Live Chat Icon For mobile
Up arrow icon