win api sendkeys不适用于特定的程序

时间:2016-03-01 00:18:53

标签: vb.net

我想向一个(特定的)程序发送一个leftbutton-mouseclick,但它不起作用。当我在其他几个应用程序上尝试我的代码时,它没有任何重大改变。

这是我的宣言:

Public Class Win32
#Region "Methods And Functions Declaration"
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal IpClassName As String, ByVal IpWindowName As String) As Int32
Public Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Int32) As Int32
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As Int32
Public Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As Int32, ByVal lpEnumFunc As Int32, ByVal lParam As Int32) As Int32
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByRef lParam As Int32) As Int32
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByRef lParam As String) As Int32

#End Region


#Region "Messages"
Public Const BM_CLICK As Int32 = &HF5
Public Const WM_NCLBUTTONDOWN As Int32 = &HA1
Public Const WM_NCHITTEST As Int32 = &H84
Public Const WM_NCLBUTTONDBLCLK As Int32 = &HA3
Public Const WM_NCLBUTTONUP As Int32 = &HA2
Public Const WM_SETFOCUS As Long = &H7
Public Const WM_SETTEXT As Long = &HC
Public Const WM_NCRBUTTONDOWN As Int32 = &HA4


#End Region

实际代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Druck Eingeben
    Dim Hwnd As Integer = Win32.FindWindow(Nothing, "V1.22 BMTS Prüfstand 2")

    If (Not Hwnd = 0) Then
        Dim HwndInfo As Integer = Win32.FindWindowEx(Hwnd, Nothing, "TGroupBox", "Info")
        If Not HwndInfo = 0 Then
            Dim ccHwnd As Integer = Win32.FindWindowEx(HwndInfo, Nothing, "TPanel", Nothing)
            If Not ccHwnd = 0 Then
                Dim cccHwnd As Integer = Win32.FindWindowEx(ccHwnd, Nothing, "Tedit", Nothing)
                If Not cccHwnd = 0 Then
                    Win32.SetForegroundWindow(cccHwnd)
                    SendKeys.Send("990")
                Else
                    MessageBox.Show("Bitte Starte erst die Programme")
                End If
            Else
                MessageBox.Show("Bitte Starte erst die Programme")
            End If
        Else
            MessageBox.Show("Bitte Starte erst die Programme")

        End If
        Win32.SetForegroundWindow(HwndInfo)
        SendKeys.Send("999")
    Else
        MessageBox.Show("Bitte Starte erst die Programme")
    End If
    'Startbutton Klicken

    Dim HwndFunktion As Integer = Win32.FindWindowEx(Hwnd, Nothing, "TGroupBox", "Funktion")
    If Not HwndFunktion = 0 Then
        Dim cHwnd As Integer = Win32.FindWindowEx(HwndFunktion, Nothing, "TButton", "S T A R T")
        If Not cHwnd = 0 Then
            Win32.SetForegroundWindow(cHwnd)
            Win32.SendMessage(cHwnd, Win32.WM_SETFOCUS, 0, 0)
            Win32.SendMessage(cHwnd, Win32.WM_NCLBUTTONDBLCLK, 0, 0)
            Win32.SendMessage(cHwnd, Win32.WM_NCLBUTTONDBLCLK, 0, 0)
            Win32.SendMessage(cHwnd, Win32.WM_NCLBUTTONDOWN, 0, 0)
            Win32.SendMessage(cHwnd, Win32.WM_SETTEXT, 0, "6")
        Else
            MessageBox.Show("Bitte Starte erst die Programme")
        End If
    Else
        MessageBox.Show("Bitte Starte erst die Programme")
    End If

End Sub

对于程序本身:它在一个旧的测试台上运行,在第7场胜利。 我认为这取决于.net 3.5,但我没有关于它的详细信息。 作为测试,我用我的代码填充了一个文本框,甚至更改了按钮的标签。只是点击功能不起作用。

首先我尝试使用鼠标定位,现在直接使用手柄。 它无论如何都无法发挥作用。

请记住,代码在一些经过测试的程序上运行正常(除了我需要的程序)。

0 个答案:

没有答案