vb 启动外部程序并且模拟鼠标点击

Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.ComponentModel
'Imports System.Runtime.InteropServices

Public Class Form1

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        My.Settings.Save()
        If p.StartInfo.FileName <> "" Then
            If p.HasExited = False Then
                p.Kill()
            End If
        End If


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        On Error Resume Next
        Me.Show()
        With ListBox1.Items
            'TextBox1.Text = "kxyt:" & My.Settings.kxytks & "----" & My.Settings.kxytjs
            'MsgBox(My.Settings.qxytks)


            .Add("千橡鱼塘(kxyt.exe):" & My.Settings.qxytks & "----" & My.Settings.qxytjs & "  耗时: " & Math.Round((CDate(My.Settings.qxytjs).Ticks - CDate(My.Settings.qxytks).Ticks) / 10 ^ 7 / 60, 2) & "分")
            .Add("开心外挂:" & My.Settings.wgks & "----" & My.Settings.wgjs & "  耗时: " & Math.Round((CDate(My.Settings.wgjs).Ticks - CDate(My.Settings.wgks).Ticks) / 10 ^ 7 / 60, 2) & "分")
            .Add("开心鱼塘:" & My.Settings.kxytks & "----" & My.Settings.kxytjs & "  耗时: " & Math.Round((CDate(My.Settings.kxytjs).Ticks - CDate(My.Settings.kxytks).Ticks) / 10 ^ 7 / 60, 2) & "分")
            .Add("小号外挂:" & My.Settings.xhwgks & "----" & My.Settings.xhwgjs & "  耗时: " & Math.Round((CDate(My.Settings.xhwgjs).Ticks - CDate(My.Settings.xhwgks).Ticks) / 10 ^ 7 / 60, 2) & "分")

        End With

    End Sub

    Public Sub yxcx(ByVal cxname As String, ByVal cxpath As String, ByVal btname As String)
        'MsgBox(cxpath + "\log")
        'End
        cxks = Now.Ticks
        pq = IntPtr.Zero
        p.StartInfo.FileName = cxname
        p.StartInfo.WorkingDirectory = cxpath
        p.StartInfo.WindowStyle = FormWindowState.Normal
        p.Start()
        p.EnableRaisingEvents = True
        p.WaitForInputIdle()

        Do While p.MainWindowTitle = ""
            Threading.Thread.Sleep(2000)
            p.Refresh()
        Loop


        Dim tt As Int32 = 1
        Do While pq.ToInt32 < 1
            Dim title As New System.Text.StringBuilder(55)
            Dim MyCallBack As New funcCallBackParent(AddressOf EnumChildWindowsProc)
            EnumChildWindows(p.MainWindowHandle, MyCallBack, IntPtr.Zero)

            'With ListBox1.Items
            '.Add("mc:" & p.MainWindowTitle)
            For Each pq1 As IntPtr In lngHWND
                '  .Add(pq1.ToString)

                GetWindowText(pq1, title, title.Capacity + 1)
                ' .Add(title.ToString)
                'MsgBox(title.ToString)

                If InStr(title.ToString, btname) > 0 Then
                    'TextBox1.Text = pq1.ToString & title.ToString
                    TextBox1.Text = p.MainWindowTitle & "正在运行"
                    pq = pq1
                    Exit For
                End If

            Next
            'End With
            If pq.ToInt32 < 1 Then
                Threading.Thread.Sleep(2000)
                p.Refresh()
            End If

            tt = tt + 1
            If tt > 5 Then
                MsgBox("无法运行")
                Exit Sub

            End If
        Loop

        PostMessage(pq, WM_LBUTTONDOWN, IntPtr.Zero, 1)
        PostMessage(pq, WM_LBUTTONUP, IntPtr.Zero, 1)
        PostMessage(p.MainWindowHandle, WM_SYSCOMMAND, SC_MINIMIZE, 0)
        FileSystemWatcher1.Path = Trim(cxpath) + "\log"
        FileSystemWatcher1.Filter = "*.*"
        FileSystemWatcher1.EnableRaisingEvents = True

        'p.WaitForExit()
        Do
            'System.Threading.Thread.Sleep(2000)
            Application.DoEvents()
        Loop While p.HasExited = False
    End Sub

    Public Function EnumChildWindowsProc(ByVal hWndParent As IntPtr, ByVal lParam As IntPtr) As Boolean

        lngHWND.Add(hWndParent)
        EnumChildWindowsProc = True
    End Function



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        My.Settings.qxytks = Now.ToString
        kxytks = Now.Ticks
        ii.StartInfo.FileName = "d:\exe\kxyt.exe"
        ii.StartInfo.WindowStyle = FormWindowState.Minimized
        ii.Start()
        Timer1.Interval = 3000
        Timer1.Enabled = True

        My.Settings.Save()
        PostMessage(ii.MainWindowHandle, WM_SYSCOMMAND, SC_MINIMIZE, 0)
        'My.Settings.qxytjs = Now.ToString


        My.Settings.wgks = Now.ToString
        Call yxcx("F:\wg714\开心网花园农夫.exe", "F:\wg714", "开始工作")
        My.Settings.wgjs = Now.ToString

        My.Settings.kxytks = Now.ToString
        Call yxcx("F:\XXXXX.exe", "F:\dm\开心钓鱼1008", "开始")
        My.Settings.kxytjs = Now.ToString

        My.Settings.xhwgks = Now.ToString
        Call yxcx("F:\xhwg\开心网花园农夫.exe", "F:\xhwg", "开始工作")
        My.Settings.xhwgjs = Now.ToString
        My.Settings.Save()
    End Sub

    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

    End Sub

    Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
        My.Settings.Save()
        'PostMessage(pq, WM_CLOSE, IntPtr.Zero, IntPtr.Zero)
        Dim t1 As DateTime = Now


        'If p.StartInfo.FileName = "F:\xhwg\开心网花园农夫.exe" Then
        'MsgBox(e.Name)

        Dim title As New System.Text.StringBuilder(55)
        Dim MyCallBack As New funcCallBackParent(AddressOf EnumChildWindowsProc)
        Do
            EnumChildWindows(p.MainWindowHandle, MyCallBack, IntPtr.Zero)

            For Each pq1 As IntPtr In lngHWND


                GetWindowText(pq1, title, title.Capacity + 1)

                If InStr(title.ToString, "距下次") > 0 Then
                    'TextBox1.Text = pq1.ToString & title.ToString
                    Exit Do

                End If

            Next
            Application.DoEvents()
        Loop
        'End If

        Do While (Now.Ticks - t1.Ticks) < (10 * 10 ^ 7)
            Application.DoEvents()
        Loop
        'MsgBox(Now.Ticks - t1.Ticks)
        If p.HasExited = False Then
            'MsgBox(pq.ToString)

            ListBox1.Items.Add(p.StartInfo.FileName() & "  运行完毕  耗时:" & Math.Round((Now.Ticks - cxks) / 10 ^ 7 / 60, 2) & "分")
            p.Kill()
            'p.Close()
        End If
        My.Settings.Save()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If ii.HasExited = True Then
            My.Settings.qxytjs = Now.ToString
            ListBox1.Items.Add("d:\exe\kxyt.exe  运行完毕  耗时:" & Math.Round((Now.Ticks - kxytks) / 10 ^ 7 / 60, 2) & "分")
            Timer1.Enabled = False
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        kxytks = Now.Ticks
        ii.StartInfo.FileName = "d:\exe\kxyt.exe"
        ii.StartInfo.WindowStyle = FormWindowState.Minimized
        ii.Start()
        Timer1.Interval = 3000
        Timer1.Enabled = True
    End Sub
End Class


''''''''
Module Module1
    Public p As Process = New Process()
    Public ii As Process = New Process()
    Public pq As IntPtr
    Public cxks As UInt64
    Public kxytks As UInt64
    Public Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String)
    'Public Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    'Public Delegate Function EnumWindowsCallback( _
    '                   ByVal hWnd As IntPtr, _
    '                  ByVal lParam As IntPtr) As Boolean

    '<DllImport("user32.dll ")> _
    'Public Function EnumChildWindows( _
    '                   ByVal hwndParent As IntPtr, _
    '                  ByVal lpEnumFunc As EnumWindowsCallback, _
    '                 ByVal lParam As IntPtr) As Boolean
    'End Function
    'Friend Declare Function EnumChildWindows Lib "User32" (ByVal hWndParent As IntPtr, ByVal funcCallBack As funcCallBackParent, ByVal lParam As IntPtr) As Boolean
    Public Const WM_CHAR As Long = &H102
    Public Const WM_KEYDOWN = &H100
    Public Const WM_SYSKEYDOWN = &H104
    Public Const WM_CLOSE = &H10
    Public Const BM_CLICK = &HF5
    Public Const WM_LBUTTONDOWN = &H201
    Public Const WM_LBUTTONUP = &H202
    Public Const WM_SETTEXT = &HC
    Public Const WM_SYSCOMMAND = &H112
    Public Const SC_MINIMIZE = &HF020&

    Public Delegate Function funcCallBackParent(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
    Public Delegate Function funcCallBackChild(ByVal hWndParent As IntPtr, ByVal lpEnumFunc As Long, ByVal lParam As Integer) As Boolean

    ' --> EnumChildWindows matches the funcCallParent Delegate 
    Friend Declare Function EnumChildWindows Lib "User32" (ByVal hWndParent As IntPtr, ByVal funcCallBack As funcCallBackParent, ByVal lParam As IntPtr) As Boolean
    Friend Declare Function EnumWindows Lib "User32" (ByVal funcCallBack As funcCallBackParent, ByVal lParam As IntPtr) As IntPtr
    Public Declare Function GetDesktopWindow Lib "user32" () As IntPtr
    Public lngHWND As New ArrayList
    <Runtime.InteropServices.DllImport("user32.dll")> _
    Public Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpWindowText As System.Text.StringBuilder, _
    ByVal nMaxCount As Integer) As Integer
    End Function


    <Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
    Public Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
    End Function
End Module

你可能感兴趣的:(thread,工作,F#,vb)