gpt4 book ai didi

vb.net - 是否可以在 MessageFilter 函数中捕获按钮名称?

转载 作者:行者123 更新时间:2023-12-05 03:08:55 25 4
gpt4 key购买 nike

我有以下代码,我需要知道按钮的名称,因为该按钮是唯一能够执行任务的按钮。

Class MessageFilter
Implements IMessageFilter
Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
If Form1.SavingData Then
Const WM_KEYDOWN As Integer = &H100
'Const WM_MOUSELEAVE As Integer = &H2A3
Const WM_MOUSE_LEFT_CLICK As Integer = &H201

Select Case m.Msg
Case WM_KEYDOWN, WM_MOUSE_LEFT_CLICK
' Do something to indicate the user is still active.
Form1.SavingData = False

Exit Select
End Select

' Returning true means that this message should stop here,
' we aren't actually filtering messages, so we need to return false.
End If

Return False
End Function
End Class

最佳答案

我建议您不要使用默认的 Form1 实例,而是将表单引用作为参数传递给消息过滤器的构造函数。添加到 VB 的默认表单实例有助于将 VB6 代码转换为 VB.Net。

如果你这样声明你的过滤器类:

Class MessageFilter
Implements IMessageFilter
Private frm As Form1
Private targetButton As Button
Public Sub New(frm As Form1, targetbutton As Button)
Me.frm = frm
Me.targetButton = targetbutton
End Sub

Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
Const WM_KEYDOWN As Integer = &H100
Const WM_MOUSE_LEFT_CLICK As Integer = &H201

If Me.frm.SavingData AndAlso
m.HWnd = Me.targetButton.Handle AndAlso
(m.Msg = WM_KEYDOWN OrElse m.Msg = WM_MOUSE_LEFT_CLICK) Then
Me.frm.SavingData = False
End If
' Returning true means that this message should stop here,
' we aren't actually filtering messages, so we need to return false.
Return False
End Function
End Class

您可以像这样应用过滤器:

Private filter As MessageFilter

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
filter = New MessageFilter(Me, Me.Button2)
Application.AddMessageFilter(filter)
End Sub

这允许您指定要使用的特定按钮。过滤器检查是否要使用其 Handle 属性而不是使用其 Name 属性将消息发送到该特定 Button,该属性将是唯一值。

关于vb.net - 是否可以在 MessageFilter 函数中捕获按钮名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44415644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com