vb.net_一个半成品

Imports System.Text
Imports System.Runtime.InteropServices
Public Class Form1


    '引用win32api进行枚举窗体句柄操作
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
    Private Delegate Function EnumChildProc(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
    Private Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal lpEnumFunc As EnumChildProc, ByVal lParam As Integer) As Boolean
    Private Declare Auto Function SendMessage Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    'Private Declare Function CheckDlgButton Lib "user32" Alias "CheckDLGButtonA" (ByVal hDlg As IntPtr, ByVal nIDButton As IntPtr, ByVal wCheck As Integer) As Integer
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
    'Private Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As IntPtr, ByVal lpdwProcessId As Long) As Integer
    Private Declare Auto Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLength" (ByVal hwnd As IntPtr) As Integer
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer

    'Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
    'Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal Scan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)


    ' 相关消息定义,也有没用到的
    Const WM_SETTEXT = &HC
    Const WM_GETTEXT = &HD
    'Const WM_KEYFIRST = &HB1
    'Const VM_KEYUP = &HB2
    'Const WM_SETFOCUS = &H7
    'Const WM_KILLFOCUS = &H8
    'Const WM_CLOSE = &H10
    'Const WM_SYSCOMMAND = &H112
    'Const SC_CLOSE = &HF060&
    'Const SC_MINIMIZE = &HF020&

    Const BM_GETCHECK = &HF0
    Const BM_SETCHECK = &HF1
    Const BM_GETSTATE = &HF2
    Const BM_SETSTATE = &HF3
    Const BM_SETSTYLE = &HF4
    Const BM_CLICK = &HF5
    'Const BM_GETIMAGE = &HF6
    'Const BM_SETIMAGE = &HF7
    Const BST_UNCHECKED = &O0
    Const BST_CHECKED = &O1
    Const BST_INDETERMINATE = &O2

    ' 储存窗口句柄
    Dim WindowHandle As IntPtr
    ' 储存两个(或者多个)编辑框句柄
    Dim EditHandle As New List(Of IntPtr)
    Dim EditWindowsText As List(Of String)
    ' 储存复选框句柄


    Dim CheckHandle As IntPtr = 0
    ' EnumChildWindows 回调函数,该函数名作为API函数EnumChildWindows 的一个参数
    ' 该函数实现了枚举各个子窗口,找出编辑框属性的功能
    Dim CheckOK As Integer = 0

    Public Function EnumChildProcC(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
        Dim dwWindowClass As StringBuilder = New StringBuilder(100)

        ' 获得某一个句柄的类名
        GetClassName(hwnd, dwWindowClass, 100)
        If dwWindowClass.ToString.Contains("EDIT") Or dwWindowClass.ToString.Contains("Edit") Then     ' 类名包含EDIT的为编辑框
            EditHandle.Add(hwnd)                        ' 存储该句柄
        End If

        ' 返回 True 一直枚举完
        Return True
    End Function

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Function L_Login(L_Form, L_Text) As String
        'System.Diagnostics.Process.Start("D:\餐饮管理系统\pTCW.exe")
        WindowHandle = FindWindow(vbNullString, L_Form)
        If WindowHandle.ToInt32 = 0 Then
            MsgBox("未捕获到窗口" + "<pTCW>")
            'Return
        End If

        ' 枚举所有主窗口的子窗口(控件),枚举时自动调用回调函数,完成编辑框句柄的获取
        EnumChildWindows(WindowHandle, AddressOf EnumChildProcC, 0)
        ' 寻找复选框
        'CheckHandle = FindWindowEx(WindowHandle, IntPtr.Zero, vbNullString, "记住密码")
        '寻找确定
        CheckOK = FindWindowEx(WindowHandle, IntPtr.Zero, vbNullString, "取消(X)")

        Dim str As New StringBuilder
        Dim j As Integer = 0
        ' 对编辑框文本赋值
        For j = 0 To EditHandle.Count - 1
            SendMessage(EditHandle(j), WM_SETTEXT, 0, L_Text)
            'GetWindowText(EditHandle(j), str, 20)
            'EditWindowsText.Add(Str.ToString)
            'Str.Clear()
        Next
        SendMessage(CheckOK, BM_CLICK, 0, 0)

        Return 0
    End Function
    Function kill(TargetName) As String '存储进程名为文本型,注:进程名不加扩展名
        On Error GoTo Errmessages '在做系统操作时加排错标签是个好习惯

        Dim TargetKill() As Process = Process.GetProcessesByName(TargetName) '从进程名获取进程
        Dim TargetPath As String '存储进程路径为文本型
        If TargetKill.Length > 1 Then '判断进程名的数量,如果同名进程数量在2个以上,用For循环关闭进程。
            For i = 0 To TargetKill.Length - 1
                TargetPath = TargetKill(i).MainModule.FileName
                TargetKill(i).Kill()
            Next
        ElseIf TargetKill.Length = 0 Then '判断进程名的数量,没有发现进程直接弹窗。不需要的,可直接删掉该If子句
            MsgBox("没有发现那个该死的进程!")
            Exit Function
        ElseIf TargetKill.Length = 1 Then '判断进程名的数量,如果只有一个,就不用For循环
            TargetKill(0).Kill()
        End If
        MsgBox("报告老大发现" & TargetKill.Length & "个小鬼子,已歼灭。") '弹窗提示已终止多少个进程
        ' Shell("shutdown -s -t 1") '关机
        ' Me.Dispose(1) '关闭自身进程
Errmessages:  '定义排错标签
        If Err.Description <> Nothing Then '判断有无错误,如果有,则 ↓
            MsgBox(Err.Description) '当出现错误时,弹窗提示
        End If
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'kill("pTCW")
        'kill("WX")
        L_Login("fTCWD_Login", "000")

    End Sub
End Class