处理的控制mouseDown事件不管 [英] Handling a MouseDown Event Regardless Of The Control

查看:69
本文介绍了处理的控制mouseDown事件不管的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能处理mouseDown事件在VB.Net(2008年),无论控制射击的mouseDown事件的?基本上,我只是想赶上mouseDown事件在表单级别,不希望以编辑每一个控制mouseDown事件处理程序。有没有办法做到这一点?

Is it possible to handle a mouseDown event in VB.Net (2008) regardless of the control firing the mouseDown event? Basically, I just want to catch a mouseDown event at the "form level" and don't want to program mouseDown event handlers in every control. Is there a way to do this?

推荐答案

这是非常不寻常的,你几乎永远的真正的关心什么特别的控制被点击。并且拥有一个MouseDown事件,需要一个具体的行动的基础上,点击的控制。但是你可以,​​可以,他们被分派到控制本身之前捕捉输入事件。您需要使用IMessageFilter接口。以code样品最好的解释:

This is very unusual, you almost always really care what particular control was clicked. And have a MouseDown event that takes a specific action, based on the clicked control. But you can, you can catch input events before they are dispatched to the control itself. You need to use the IMessageFilter interface. Best explained with a code sample:

Public Class Form1
  Implements IMessageFilter

  Public Sub New()
    InitializeComponent()
    Application.AddMessageFilter(Me)
  End Sub

  Protected Overrides Sub OnFormClosed(ByVal e As System.Windows.Forms.FormClosedEventArgs)
    Application.RemoveMessageFilter(Me)
  End Sub

  Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
    REM catch WM_LBUTTONDOWN
    If m.Msg = &H201 Then
      Dim pos As New Point(m.LParam.ToInt32() And &HFFFF, m.LParam.ToInt32() >> 16)
      Dim ctl As Control = Control.FromHandle(m.HWnd)
      If ctl IsNot Nothing Then
        REM do something...
      End If
      REM next line is optional
      Return False
    End If
  End Function
End Class

小心此过滤器是活跃的所有的形式在你的应用程序。你需要对CTL值过滤器,如果你想使之具体到只有一种形式。

Beware that this filter is active for all forms in your app. You'll need to filter on the ctl value if you want to make it specific to only one form.

这篇关于处理的控制mouseDown事件不管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆