Use Application.AddMessageFilter mask message

xiaoxiao2021-03-06  85

It took a long time to pay attention to Application's AddMessageFilter, bitterly poor in your own basis, and did not test the results. Today, I saw the post, some people wanted to block the right-click message in the form of TextBox, so he represented this topic. Just still don't have any good results, you can only be realized.

Application.AddMessageFilter's main function is to add a message filter to monitor these messages when the Windows message is transmitted to the target.

(Hey, I originally want to say these meanings according to my own understanding, I am afraid that I am mistaken, and I have a lesson, I will take care of MSDN. Poor me, it is not a professional, I can only It is customary, and the 僧 不 不, the country is wearing a tie into the city, which is poor.)

Note: Adding a message filter to the application will reduce performance. (I am a warning, I generally use it, use it, manage it)

Using Application.addMessageFilter and the general method call different, its parameter message has such a request: To prevent scheduling a message, the value parameter instance that passes to the method must rewrite the PREFILTERMESSAGE method with the code to process the message. This method must return false.

Take the right-click message on the Movie FORM. Take the main practice below.

First, implement the iMessageFilter interface. as follows.

Why did you have a property that deliver FORM, as long as it is used to determine whether the current control is used. This effect is not ideal and needs to be improved. Originally, it is not necessary to introduce this attribute. Use the message.getlparam method to know the current message object is TEXTBOX. Pity I don't know how to write that Structure, if you give it out to correct, the little person is grateful.

Public Class TextBoxRbuttonMessageFilter

Implements system.windows.forms.imsSagefilter

Const WM_RBUTTONDOWN AS INTEGER = & H204

Const WM_RBUTTONUP AS INTEGER = & H205

Private M_Form As System.Windows.Forms.form

Public Writeonly Property [Form] () as system.windows.forms.form

Set (Byval Value As System.Windows.Forms.form)

M_Form = Value

End set

End Property

Public Function RButtonFilterMessage (byref m as system.windows.forms.Message)

As Boolean Implements System.windows.Forms.IMessageFilter.prefilterMessage

IF (M.MSG = WM_RBUTTONDOWN ORELSE M.MSG = WM_RBUTTONUP) ANDALSO M_FORM.ActiveControl.gettype.name.Equals ("TextBox") THEN

Return True

Else

Return False

END IF

END FUNCTION

END CLASS

Second, add a message filter to the application's message pump.

#Region "Windows Form Designer Generated Code"

Public Sub New ()

Mybase.new ()

'This call is required for the Windows Form Designer. InitializeComponent ()

'Add any initialization after INITIALIZECOMPONENT ()

mmsgfilter.form = me

System.windows.Forms.Application.addMessageFilter (mmsgfilter)

End Sub

......

#End region

Private MMSGFilter As New TextBoxRbuttonMessageFilter

It's that simple. The question is how to use Message.Getlparam, please advise.

Third, use Handle instead of ActiveControl

#Region "Windows Form Designer Generated Code"

Public Sub New ()

Mybase.new ()

'This call is required for the Windows Form Designer.

InitializeComponent ()

'Add any initialization after INITIALIZECOMPONENT ()

Msgfilterinit ()

End Sub

.......

#End region

Private MMSGFilter As New TextBoxRbuttonMessageFilter

Private sub msgfilterinit ()

DIM CTR as system.windows.forms.control

For Each Ctr in Me.Controls

IF Typeof Ctr is TextBox Then

MMSGFilter.SetfilterHandles (Ctr.Handle)

END IF

NEXT

System.windows.Forms.Application.addMessageFilter (mmsgfilter)

End Sub

END CLASS

Public Class TextBoxRbuttonMessageFilter

Implements system.windows.forms.imsSagefilter

Const WM_RBUTTONDOWN AS INTEGER = & H204

Const WM_RBUTTONUP AS INTEGER = & H205

Private m_hwnds as new hashtable

Public SUB SETFILTERHANDLES (BYVAL MHWND AS INTPTR)

m_hwnds.add (MHWND, NOTHING)

End Sub

Public Function RButtonFilterMessage (byref m as system.windows.forms.Message) _

As Boolean Implements System.windows.Forms.IMessageFilter.prefilterMessage

IF (M.MSG = WM_RBUTTONDOWN ORELSE M.MSG = WM_RBUTTONUP) Andalso M_HWNDS.CONTAINS (M.HWND) THEN

Return True

Else

Return False

END IF

END FUNCTION

END CLASS

转载请注明原文地址:https://www.9cbs.com/read-94944.html

New Post(0)