Sendmessage 没有引发点击事件

问题描述 投票:0回答:1

我有这个 vb6 形式。

在后台桌面Winsta0\second_desktop中启动 这是其中的代码:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202

Private Sub Command1_Click()
    Dim X As Long, Y As Long, lParam As Long, hwnd
    X = 150
    Y = 50
    hwnd = Me.hwnd
    lParam = MakeLParam(X, Y)
    SendMessage hwnd, WM_LBUTTONDOWN, 1, lParam
    SendMessage hwnd, WM_LBUTTONUP, 0, lParam
End Sub

Private Function lshft(num As Long, shifts As Byte) As Long
    lshft = num * (2 ^ shifts)
End Function

Private Function MakeLParam(ByVal LoWord As Long, ByVal HiWord As Long) As Long
        MakeLParam = lshft(HiWord, 16) Or (LoWord And &HFFFF)
End Function

Private Sub Form_Click()
    MsgBox "Clicked"
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    MsgBox "mouseup"
End Sub

如果我启动它并单击按钮,只会引发 Form_MouseUp 事件,但不会引发 Form_Click。我也尝试使用 Form_MouseDown 并且它也有效。

我怎样才能提高 Form_Click? 也可以使用外部 C++ 进程的解决方案。

谢谢

c++ winapi vb6 sendmessage
1个回答
0
投票

我认为您正在寻找使用

PostMessage
功能而不是
SendMessage
PostMessage
函数向指定线程的消息队列中添加一条消息,并立即返回。这将允许系统处理消息队列并适当地引发点击事件。

这是一个如何使用

PostMessage
而不是
SendMessage
的例子:

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Sub Command1_Click()
    Dim X As Long, Y As Long, lParam As Long, hwnd
    X = 150
    Y = 50
    hwnd = Me.hwnd
    lParam = MakeLParam(X, Y)
    PostMessage hwnd, WM_LBUTTONDOWN, 1, lParam
    PostMessage hwnd, WM_LBUTTONUP, 0, lParam
End Sub

这应该会按预期触发

Form_Click
事件。

© www.soinside.com 2019 - 2024. All rights reserved.