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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏JadePeng的技术博客

Java LinqCollection 仿Linq的list常用函数

目前支持find,findAll,sort,select,remove等,java不支持lamda函数,因此用接口代替 public interface Fun...

1.1K4
来自专栏流柯技术学院

Java Selenium封装--RemoteWebDriver

3111
来自专栏曾大稳的博客

Android ClassLoader流程解读并简单方式实现热更新

ClassLoader在启动Activity的时候会调用loadClass方法,我们就从这里入手:

3032
来自专栏码匠的流水账

聊聊rocketmq的PullConsumerImpl

io/openmessaging/rocketmq/consumer/PullConsumerImpl.java

2391
来自专栏用户画像

微博订阅评论

参考:http://open.weibo.com/wiki/%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81

1311
来自专栏葡萄城控件技术团队

枚举的多语言显示

关于枚举类型的多语言显示,其实就是Globalization的问题。解决方案当然不止一种,这里介绍一种可用性和扩展性的比较好的通用方法。 显然这里自己去实现自定...

2925
来自专栏码匠的流水账

聊聊storm supervisor的启动

storm-core-1.2.2-sources.jar!/org/apache/storm/daemon/supervisor/Supervisor.java

1903
来自专栏机器学习与自然语言处理

03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历

03-树3. Tree Traversals Again (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-...

3419
来自专栏小樱的经验随笔

Codeforces 768B Code For 1

B. Code For 1 time limit per test:2 seconds memory limit per test:256 megabytes ...

3768
来自专栏yl 成长笔记

web 上读取图片,并转化为指定格式

921

扫码关注云+社区

领取腾讯云代金券