WINDOWS事件函数声明

 

Public Enum NextWindowType
    NextWindow = 2
    FirstWindow = 0
    LastWindow = 1
    PreWindow = 3
End Enum

Public Structure RECT
    Dim Left As Integer
    Dim Top As Integer
    Dim Right As Integer
    Dim Bottom As Integer
End Structure





Public Class _Windows

    Public Const WM_CLOSE = &H10
    Public Const GWL_STYLE = (-16)
    Public Const WS_VISIBLE = &H10000000


    Private Declare Function _GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer) As Integer
    Private Declare Function _SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
    Private Declare Function _MoveWindow Lib "user32" Alias "MoveWindow" (ByVal hwnd As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Integer) As Integer
    Private Declare Function _SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer


    Private Declare Function _GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Int32
    Private Declare Function _SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Int32) As Int32
    Private Declare Function _GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Int32, ByRef lpRect As RECT) As Boolean  'BYREF BOOLEAN否则错误
    Private Declare Function _FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
    Private Declare Function _GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Int32, ByVal lpString As String, ByVal cch As Int32) As Int32
    Private Declare Function _GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Int32, ByVal wFlag As Int32) As Int32
    Private Declare Function _ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Int32, ByVal nCmdShow As Int32) As Int32
    Private Declare Function _WindowFromPoint Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Int32, ByVal yPoint As Int32) As Int32
    Private Declare Function _SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, lParam As Int32) As Int32
    Private Declare Function _PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    Private Declare Function _GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Int32, ByVal lpClassName As String, ByVal nMaxCount As Int32) As Int32





    Public Sub MoveWindow(ByVal hwnd As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Integer)
        _MoveWindow(hwnd, x, y, nWidth, nHeight, bRepaint)
    End Sub

    ' Public Declare Function GetTopWindow Lib "user32" Alias "GetTopWindow" (ByVal hwnd As Int32) As Int32

    Public Function FindWindowWithName(windowname As String) As IntPtr
        Return _FindWindow(vbNullString, windowname)
    End Function

    Public Function FindWindowWithClass(windowclass As String) As IntPtr
        Return _FindWindow(windowclass, vbNullString)
    End Function

    Public Function GetFrantWindow() As IntPtr
        Return _GetForegroundWindow()
    End Function

    Public Function GetWindowText(windowHandle As IntPtr) As String
        Dim s As String = Space(30)
        _GetWindowText(windowHandle, s, 30)
        Return s
    End Function


    Public Function GetNextWindow(windowHandle As IntPtr, nextWindowType As NextWindowType) As IntPtr
        Return _GetNextWindow(windowHandle, nextWindowType)
    End Function

    Public Function SetWindowFrant(windowHandle As IntPtr)
        _SetForegroundWindow(windowHandle)
    End Function

    Public Function GetWindowClassName(windowHandle As IntPtr) As String
        Dim s As String = Space(30)
        _GetClassName(windowHandle, s, 30)
        Return s

    End Function

    Public Function GetWindowRegion(windowHandle As IntPtr) As RECT
        Dim rect As RECT
        _GetWindowRect(windowHandle, rect)
        Return rect
    End Function




End Class
Imports System.Runtime.InteropServices
Imports System.Text

Module Module1

    <DllImport("user32.dll", SetLastError:=True)>
    Public Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
    End Function

    '根据类名和窗口标题查找句柄




    <DllImport("user32.dll", SetLastError:=True)>
    Public Function FindWindowEx(parentHandle As IntPtr, childAfter As IntPtr, className As String, windowTitle As String) As IntPtr
    End Function

    '根据父句柄,前一个句柄,类名和窗口标题查找句柄,这几个信息可以通过VS自带的spy++查询。




    <DllImport("user32.dll", EntryPoint:="GetDesktopWindow", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Function GetDesktopWindow() As IntPtr
    End Function

    '返回桌面窗口句柄,被我用来当前一个函数的父句柄。




    <DllImport("user32.dll")>
    Public Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As Integer) As Integer
    End Function

    '获取指定窗口的标题,WinForm里的控件都是window,但在我们讨论的情况下window就只是窗口了。需要结合StringBuider类使用,不熟悉的可以去预习下。


    <DllImport("User32.dll")>
    Public Function GetClassName(hWnd As Integer, lpClassName As StringBuilder, nMaxCount As Integer) As Integer
    End Function

    '获取指定窗口的类名,也是用到StringBuider类。


    <DllImport("USER32.DLL")>
    Public Function SetForegroundWindow(hWnd As IntPtr) As Boolean
    End Function

    '将一个窗口显示到最前端。




    <DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)>
    Public Function GetForegroundWindow() As IntPtr
    End Function

    '返回最前端窗口的句柄。





    <DllImport("user32.dll", EntryPoint:="PostMessage")>
    Public Function PostMessage(hwnd As IntPtr, msg As UInteger, wparam As Integer, lparam As Integer) As IntPtr
    End Function



    '发送文本
    Public Sub sendText(hwnd As IntPtr, strText As [String])
        Dim charCByte As Byte() = UnicodeEncoding.GetEncoding("GBK").GetBytes(strText)
        For i As Integer = 0 To charCByte.Length - 1
            SendMessage(hwnd, WM_CHAR, CInt(charCByte(i)), 0)
        Next

    End Sub




    '获取窗口位置
    <DllImport("user32.dll")>
    Public Function GetWindowRect(hWnd As IntPtr, ByRef lpRect As RECT) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function





    <StructLayout(LayoutKind.Sequential)>
    Public Structure RECT
        Public Left As Integer
        '最左坐标
        Public Top As Integer
        '最上坐标
        Public Right As Integer
        '最右坐标
        Public Bottom As Integer
        '最下坐标
    End Structure


    '按钮事件,wParam代表键值,具体可以查;lParam代表点击的点击次数、组合键等信息,msdn上有张表介绍各个位的作用,不过我没用过。

    '发送字符,每次发送一个char,wParam代表char的值转换成int类型就行,lParam为0。

    '鼠标点击事件,wParam代表组合键,没有的话为0;lParam代表点击的位置,低字为x坐标,高字为y坐标,即x+(y<<16),这个坐标是相对于屏幕而言的。

    '对于鼠标滚轮事件,wParam高字代表滚动距离,向上为正,向下为负,低字代表组合键,没有的话为0;lParam代表点击的位置,低字为x坐标,高字为y坐标,即x+(y<<16),这个坐标是相对于窗口而言的。

    '由于只有窗口句柄,使用滚轮事件,发送字符和按钮事件时,需要获取相应区域的焦点,我是用鼠标点击事件做的。

    '按下按钮

    Public Const WM_KEYDOWN As Integer = &H100

    '放开按钮

    Public Const WM_KEYUP As Integer = &H101

    '发送字符

    Public Const WM_CHAR As Integer = &H102

    '应用程序发送此消息来设置一个窗口的文本   

    Public Const WM_SETTEXT As Integer = &HC

    '当一个窗口或应用程序要关闭时发送一个信号   

    Public Const WM_CLOSE As Integer = &H10

    '当用户选择结束对话框或程序自己调用ExitWindows函数   

    Public Const WM_QUERYENDSESSION As Integer = &H11

    '用来结束程序运行,会关闭窗口所属的整个程序

    Public Const WM_QUIT As Integer = &H12

    '按下鼠标左键   

    Public Const WM_LBUTTONDOWN As Integer = &H201

    '释放鼠标左键   

    Public Const WM_LBUTTONUP As Integer = &H202

    '双击鼠标左键   

    Public Const WM_LBUTTONDBLCLK As Integer = &H203

    '使用鼠标滚轮

    Public Const WM_MOUSEWHEEL As Integer = &H20A


    <DllImport("user32.dll", EntryPoint:="SendMessage")>
    Public Function SendMessage(hWnd As IntPtr, Msg As UInteger, wParam As Integer, lParam As Integer) As Integer
    End Function

End Module

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值