精华内容
下载资源
问答
  • VB获取窗口句柄

    2018-03-12 01:27:05
    VB获取窗口句柄最容易看明白的代码。先看明白取窗口句柄后再学习取控件句柄。
  • vb 获取窗口句柄 并发信息

    万次阅读 2011-11-29 08:23:22
    (本文中部分使用繁体字,如果在简体系统下请将第三部实例...Public Declare Function ShowWindow Lib"user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long (根据句柄控制某一窗体显示状态) Public Decla

    (本文中部分使用繁体字,如果在简体系统下请将第三部实例程序记事本标题换成相对应的简体名称)

    第一部分:

    常用的与窗体相关的API函数

    Public Declare Function ShowWindow Lib"user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long (根据句柄控制某一窗体显示状态)

    Public Declare Function EnableWindow Lib"user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long(使能窗口事件)

    Public Declare Function FindWindow Lib"user32" Alias "FindWindowA" (ByVal lpClassName As String,ByVal lpWindowName As String) As Long(根据标题找主窗口句柄)

    Public Declare Function EnumWindows Lib"user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long(找到窗口组)

    Public Declare Function FindWindowEx Lib"user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByValhWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long(找子窗口部件)

     

     

    Public Declare Function GetParent Lib"user32" (ByVal hwnd As Long) As Long

    Public Declare Function GetWindow Lib"user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

    Public Declare Function GetWindowText Lib"user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVallpString As String, ByVal cch As Long) As Long(获取窗口标题)

    Public Declare Function GetClassName Lib"user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVallpClassName As String, ByVal nMaxCount As Long) As Long(获取窗口类)

     

    Public Declare Function SendMessage Lib"user32" Alias "SendMessageA" (ByVal hwnd As Long, ByValwMsg As Long, ByVal wParam As Long, lParam As Any) As Long(发送信息)

    Private Declare Function PostMessage Lib"user32" Alias "PostMessageA" (ByVal hwnd As Long, ByValwMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long(发送信息)

     

    Public Declare Sub Sleep Lib "kernel32"(ByVal dwMilliseconds As Long) (延时用)

    Public Declare Sub keybd_event Lib"user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags AsLong, ByVal dwExtraInfo As Long)(键盘事件)

    第二部分

    按键编码

    Public Const VK_LBUTTON = &H1

    Public Const VK_RBUTTON = &H2

    Public Const VK_CANCEL = &H3

    Public Const VK_MBUTTON = &H4

    Public Const VK_BACK = &H8

    Public Const VK_TAB = &H9

    Public Const VK_CLEAR = &HC

    Public Const VK_RETURN = &HD

    Public Const VK_SHIFT = &H10

    Public Const VK_CONTROL = &H11

    Public Const VK_MENU = &H12

    Public Const VK_PAUSE = &H13

    Public Const VK_CAPITAL = &H14

    Public Const VK_ESCAPE = &H1B

    Public Const VK_SPACE = &H20

    Public Const VK_PRIOR = &H21

    Public Const VK_NEXT = &H22

    Public Const VK_END = &H23

    Public Const VK_HOME = &H24

    Public Const VK_LEFT = &H25

    Public Const VK_UP = &H26

    Public Const VK_RIGHT = &H27

    Public Const VK_DOWN = &H28

    Public Const VK_Select = &H29

    Public Const VK_PRINT = &H2A

    Public Const VK_EXECUTE = &H2B

    Public Const VK_SNAPSHOT = &H2C

    Public Const VK_Insert = &H2D

    Public Const VK_Delete = &H2E

    Public Const VK_HELP = &H2F

    Public Const VK_0 = &H30

    Public Const VK_1 = &H31

    Public Const VK_2 = &H32

    Public Const VK_3 = &H33

    Public Const VK_4 = &H34

    Public Const VK_5 = &H35

    Public Const VK_6 = &H36

    Public Const VK_7 = &H37

    Public Const VK_8 = &H38

    Public Const VK_9 = &H39

    Public Const VK_A = &H41

    Public Const VK_B = &H42

    Public Const VK_C = &H43

    Public Const VK_D = &H44

    Public Const VK_E = &H45

    Public Const VK_F = &H46

    Public Const VK_G = &H47

    Public Const VK_H = &H48

    Public Const VK_I = &H49

    Public Const VK_J = &H4A

    Public Const VK_K = &H4B

    Public Const VK_L = &H4C

    Public Const VK_M = &H4D

    Public Const VK_N = &H4E

    Public Const VK_O = &H4F

    Public Const VK_P = &H50

    Public Const VK_Q = &H51

    Public Const VK_R = &H52

    Public Const VK_S = &H53

    Public Const VK_T = &H54

    Public Const VK_U = &H55

    Public Const VK_V = &H56

    Public Const VK_W = &H57

    Public Const VK_X = &H58

    Public Const VK_Y = &H59

    Public Const VK_Z = &H5A

    Public Const VK_STARTKEY = &H5B

    Public Const VK_CONTEXTKEY = &H5D

    Public Const VK_NUMPAD0 = &H60

    Public Const VK_NUMPAD1 = &H61

    Public Const VK_NUMPAD2 = &H62

    Public Const VK_NUMPAD3 = &H63

    Public Const VK_NUMPAD4 = &H64

    Public Const VK_NUMPAD5 = &H65

    Public Const VK_NUMPAD6 = &H66

    Public Const VK_NUMPAD7 = &H67

    Public Const VK_NUMPAD8 = &H68

    Public Const VK_NUMPAD9 = &H69

    Public Const VK_MULTIPLY = &H6A

    Public Const VK_ADD = &H6B

    Public Const VK_SEPARATOR = &H6C

    Public Const VK_SUBTRACT = &H6D

    Public Const VK_DECIMAL = &H6E

    Public Const VK_DIVIDE = &H6F

    Public Const VK_F1 = &H70

    Public Const VK_F2 = &H71

    Public Const VK_F3 = &H72

    Public Const VK_F4 = &H73

    Public Const VK_F5 = &H74

    Public Const VK_F6 = &H75

    Public Const VK_F7 = &H76

    Public Const VK_F8 = &H77

    Public Const VK_F9 = &H78

    Public Const VK_F10 = &H79

    Public Const VK_F11 = &H7A

    Public Const VK_F12 = &H7B

    Public Const VK_F13 = &H7C

    Public Const VK_F14 = &H7D

    Public Const VK_F15 = &H7E

    Public Const VK_F16 = &H7F

    Public Const VK_F17 = &H80

    Public Const VK_F18 = &H81

    Public Const VK_F19 = &H82

    Public Const VK_F20 = &H83

    Public Const VK_F21 = &H84

    Public Const VK_F22 = &H85

    Public Const VK_F23 = &H86

    Public Const VK_F24 = &H87

    Public Const VK_NUMLOCK = &H90

    Public Const VK_OEM_SCROLL = &H91

    Public Const VK_OEM_1 = &HBA

    Public Const VK_OEM_PLUS = &HBB

    Public Const VK_OEM_COMMA = &HBC

    Public Const VK_OEM_MINUS = &HBD

    Public Const VK_OEM_PERIOD = &HBE

    Public Const VK_OEM_2 = &HBF

    Public Const VK_OEM_3 = &HC0

    Public Const VK_OEM_4 = &HDB

    Public Const VK_OEM_5 = &HDC

    Public Const VK_OEM_6 = &HDD

    Public Const VK_OEM_7 = &HDE

    Public Const VK_OEM_8 = &HDF

    Public Const VK_ICO_F17 = &HE0

    Public Const VK_ICO_F18 = &HE1

    Public Const VK_OEM102 = &HE2

    Public Const VK_ICO_HELP = &HE3

    Public Const VK_ICO_00 = &HE4

    Public Const VK_ICO_CLEAR = &HE6

    Public Const VK_OEM_RESET = &HE9

    Public Const VK_OEM_JUMP = &HEA

    Public Const VK_OEM_PA1 = &HEB

    Public Const VK_OEM_PA2 = &HEC

    Public Const VK_OEM_PA3 = &HED

    Public Const VK_OEM_WSCTRL = &HEE

    Public Const VK_OEM_CUSEL = &HEF

    Public Const VK_OEM_ATTN = &HF0

    Public Const VK_OEM_FINNISH = &HF1

    Public Const VK_OEM_COPY = &HF2

    Public Const VK_OEM_AUTO = &HF3

    Public Const VK_OEM_ENLW = &HF4

    Public Const VK_OEM_BACKTAB = &HF5

    Public Const VK_ATTN = &HF6

    Public Const VK_CRSEL = &HF7

    Public Const VK_EXSEL = &HF8

    Public Const VK_EREOF = &HF9

    Public Const VK_PLAY = &HFA

    Public Const VK_ZOOM = &HFB

    Public Const VK_NONAME = &HFC

    Public Const VK_PA1 = &HFD

    Public Const VK_OEM_CLEAR = &HFE

    第三部分:

    实例打开记事本---并发送信息

    (上面的声明放在一个模块中即可)

     

    Option Explicit

    Private StartTime As Long

     

    Private Declare Function SendMessage& Lib"user32" Alias "SendMessageA" (ByVal hwnd As Long, ByValwMsg As Long, ByVal wParam As Long, lParam As Any)

    Private Declare Function PostMessage Lib"user32" Alias "PostMessageA" (ByVal hwnd As Long, ByValwMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Private Declare Function FindWindow Lib"user32" Alias "FindWindowA" (ByVal lpClassName As String,ByVal lpWindowName As String) As Long

    Private Const WM_KEYDOWN = &H100

    Private Const VK_F1 = &H70

    Private Const WM_KEYUP = &H101

     

    Public Sub Delay(mSec As Long)

        DimTStart As Single

        TStart =Timer

        While(Timer - TStart) < (mSec / 1000)

           DoEvents

        Wend

    End Sub

     

    Private Sub Command1_Click()

        DimReturnValue

       ReturnValue = Shell("notepad.exe", vbNormalFocus) '打开记事本

       AppActivate ReturnValue                            '激活窗口

        Dim hwndAs Long

       

        hwnd =FindWindow(vbNullString, "未命名-记事本")  '窗口标题

        SendKeys"= ", True

        SendKeys"I am ok", True

        SendKeys"{ENTER}", True

           SendKeys "{TAB}", True

       PostMessage hwnd, WM_KEYDOWN, VK_RETURN, 0

        Delay(1000)

       PostMessage hwnd, WM_KEYUP, VK_RETURN, 0

        Delay(1000)

           SendKeys "5", True

    End Sub

     

     

    Private Sub Command2_Click()

    End

    End Sub



     第四部分

    总结

    获取句柄的方法

    1主窗体(可参考上面的方法)

    如果是多个窗体要查找某一个

    '用 FindWindowEx 查找第一个,再用找到的窗口句柄作为参数再次调用 FindWindowEx,查找第二个,直到返回 0 为止
    '得到句柄后,用 GetWindowText 得到标题,分析它提取出你需要的窗口
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Sub Command1_Click()
    Dim dl As Long, winHwnd As Long, nStr As String, nSize As Long
    
    nSize = 128
    Do
       winHwnd = FindWindowEx(0, winHwnd, vbNullString, vbNullString) '得到一个窗口句柄
       If winHwnd = 0 Then Exit Do
       nStr = String(nSize, vbNullChar)
       dl = GetWindowText(winHwnd, nStr, nSize)       '得到窗口标题
       nStr = Left(nStr, InStr(nStr, vbNullChar) - 1) '去掉后面的无用字符
       If LCase(Right(nStr, 5)) = "1.exe" Then MsgBox winHwnd & vbCrLf & nStr '提取出符合条件的标题
    Loop


    2子窗体(部件),窗体组

    `控件需要知道类名可以用spy++查看

    private Sub Command1_Click()  
    Dim Phwnd As Long  
    Dim ChildHwnd As Long  
    Phwnd = FindWindow(vbNullString, "计算器")  
    If Phwnd <> 0 Then  
     ChildHwnd = FindWindowEx(Phwnd, 0, "Edit", vbNullString)  
     If ChildHwnd <> 0 Then  
     SendMessage ChildHwnd, WM_SETTEXT, 0, ByVal "我是计算器"  
     Else  
     MsgBox "找不到相关控件"  
     End If  
    Else  
     MsgBox "计算器程序没有运行"  
    End If  
    End Sub  


    发送信息的方法

    1--------------

    先获取窗口句柄(方法同上),然后用sendmessage发信息

    Private Const WM_SETTEXT = &HC
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long

    Dim s As String
    Dim FT_hWnd As Long
    Dim ID_Control_hWnd As Long
    s = "test" & vbCrLf & "找到你了"
    
    

     ID_Control_hWnd = GetDlgItem(你得到的句柄值, &HF)
    SendMessage ID_Control_hWnd, WM_SETTEXT, 0, ByVal s


    2------------------------------

    用 SendKeys 语句
    AppActivate "接收键盘事件的窗口标题" ' 激活指定窗口
    SendKeys "A", True '发送字符 A

    '  SendKeys "I am ok", True
     '   SendKeys "{ENTER}", True
     '   PostMessage hwnd, WM_KEYDOWN, VK_RETURN, 0
     '   Delay (1000)
     '   PostMessage hwnd, WM_KEYUP, VK_RETURN, 0
     '   Delay (1000)

     


    展开全文
  • VB6.0拖动获取窗口句柄.zip
  • VB2010编写的一个 窗口句柄信息动态获取工具源码,新手可以参考学习,不知道的可以留言,里面还有一个vb6的也是源码.一起奉上.有可以学习的是图片鼠标的代替
  • vb获取句柄

    2018-07-04 10:22:42
    vb获取鼠标指定位置窗口句柄句柄内容关联 vb获取鼠标指定位置窗口句柄句柄内容关联 vb获取鼠标指定位置窗口句柄句柄内容关联 vb获取鼠标指定位置窗口句柄句柄内容关联
  • Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer Private Declare Function FindWindowEx Lib "user32" Alia
      Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    
        Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hwnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
    
        Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        Declare Function GetForegroundWindow Lib "user32" () As Integer
        Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
    
        Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Integer
    

    展开全文
  • 实现方式:主要是通过winapi实现窗体的捕捉,取句柄,发送信息等功能废话不多说直接上代码:Imports System.Runtime.InteropServicesImports System.TextPublic Class Form1 '获取窗口句柄1 EntryPoint:=...

    3f9bccc11a9f9f5f45fa762dafdc7f69.gif

    5c01781ee4e97ce5e8585ed12c43bd06.png

    实现方式:主要是通过winapi实现窗体的捕捉,取句柄,发送信息等功能

    废话不多说直接上代码:

    Imports System.Runtime.InteropServicesImports System.TextPublic Class Form1    '获取窗口句柄1    EntryPoint:="WindowFromPoint", SetLastError:=True, CharSet:=CharSet.Auto)>    Public Shared Function WindowFromPoint(pt As Point) As IntPtr    End Function    '获取窗口句柄2    EntryPoint:="GetForegroundWindow", SetLastError:=True, CharSet:=CharSet.Auto)>    Public Shared Function GetForegroundWindow() As IntPtr    End Function    '发送信息(同步)    EntryPoint:="SendMessage", SetLastError:=True, CharSet:=CharSet.Auto)>    Public Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As StringBuilder) As Integer    End Function    '发送消息(异步)    EntryPoint:="PostMessage", SetLastError:=True, CharSet:=CharSet.Auto)>    Public Shared Function PostMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As StringBuilder) As Integer    End Function    '获取窗体类名    EntryPoint:="GetClassName", SetLastError:=True, CharSet:=CharSet.Auto)>    Private Shared Function GetClassName(hWnd As IntPtr, lpClassName As StringBuilder, nMaxCount As Integer) As Integer    End Function    '获取窗体文本    EntryPoint:="GetWindowText", SetLastError:=True, CharSet:=CharSet.Auto)>    Public Shared Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As Integer) As Integer    End Function    '查找窗体句柄    EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)>    Public Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr    End Function    '调整窗体(大小位置)    EntryPoint:="MoveWindow", SetLastError:=True, CharSet:=CharSet.Auto)>    Public Shared Function MoveWindow(hWnd As IntPtr, x As Integer, y As Integer, nWidth As Integer, nHeight As Integer, BRePaint As Boolean) As Integer    End Function    '窗体显示隐藏    EntryPoint:="ShowWindow", SetLastError:=True, CharSet:=CharSet.Auto)>    Public Shared Function ShowWindow(hwnd As IntPtr, nCmdShow As Integer) As Integer    End Function    '获取窗口大小    EntryPoint:="GetWindowRect", SetLastError:=True, CharSet:=CharSet.Auto)>    Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef lpRect As RECT) As  Boolean    End Function        Public Structure RECT        Public Left As Integer '最左坐标        Public Top As Integer '最上坐标        Public Right As Integer '最右坐标        Public Bottom As Integer '最下坐标    End Structure    '查找子窗体句柄    EntryPoint:="FindWindowEx", SetLastError:=True, CharSet:=CharSet.Auto)>    Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr    End Function    '======================================    Dim Hwd As IntPtr = IntPtr.Zero    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load        MaximizeBox = False        MinimizeBox = False    End Sub    Private Sub Pic_Wnd_MouseDown(sender As Object, e As MouseEventArgs) Handles Pic_Wnd.MouseDown        If e.Button = MouseButtons.Left Then            TextBox1.Text = ""            Cursor.Current = New Cursor(Application.StartupPath & "\szj.cur")            Pic_Wnd.Capture = True        End If    End Sub    Private Sub Pic_Wnd_MouseUp(sender As Object, e As MouseEventArgs) Handles Pic_Wnd.MouseUp        Pic_Wnd.Capture = False    End Sub    Private Sub Pic_Wnd_MouseMove(sender As Object, e As MouseEventArgs) Handles Pic_Wnd.MouseMove        If Not Bounds.Contains(Cursor.Position) Then            '获取窗口句柄            Hwd = WindowFromPoint(Cursor.Position)            Hwd = Hwd.ToString("D").PadLeft(8, "0"c)            TextBox1.Text = Hwd.ToString            '------------------------            '获取窗口类名            Dim Classname As New StringBuilder(256)            GetClassName(Hwd, Classname, 256)            TextBox2.Text = Classname.ToString            ''------------------------            '获取窗口标题            Dim title As New StringBuilder(256)            Dim titlea As New StringBuilder(256)            SendMessage(Hwd, &HD&, 256, title)            GetWindowText(Hwd, titlea, 256)            If titlea.ToString <> "" Then                TextBox3.Text = title.ToString            Else                TextBox3.Text = titlea.ToString            End If            ''-------------------------            ''获取窗口大小            Dim rc As New RECT            GetWindowRect(Hwd, rc)            TextBox4.Text = (rc.Right - rc.Left) & " X " & rc.Bottom - rc.Top        End If    End Sub    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        SendMessage(Hwd, &HC&, 256, New StringBuilder(TextBox5.Text))    End Sub    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click        ShowWindow(Hwd, 1)    End Sub    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click        ShowWindow(Hwd, 0)    End SubEnd Class

    示例下载:

    https://vbee.lanzous.com/i4EOogged4d

    更多精彩内容请继续关注我们的动态哦!

    53ca7b1ee29064b51fed168afe8c2e0c.png

    展开全文
  • 请各位朋友帮忙,有重谢!!!,知道了一个窗口句柄,怎么获得IHTMLDOCUMENT接口, 进而可以获得网页的内容?
  • 本文通过已知窗口的进程ID获取窗口句柄的实例来浅谈EnumWindows函数使用、委托函数的定义及其意义、ByVal和ByRef的区别。

      有时候我们需要通过窗口的进程ID获取窗口的句柄,在windows中又没有直接的函数可用,这时候我们只能曲线救国,通过EnumWindows函数枚举所有顶级窗口,然后用GetWindowThreadProcessId函数来获取已知进程ID的窗口句柄。网上有很多C和C++EnumWindows函数的使用实例和详细解释,但是VB.NET中EnumWindows函数的详细解释和实例资源非常稀少,我也是费了很长时间才把C++语言改写为VB.NET语言的,并在Windows7和vs2019中实测通过。希望能对有此困惑的人有所帮助!
      顺便说一下,本人是外科医生,编程实属业余爱好,不务正业之举,编程方面菜鸟一枚,有不对的望各位大佬指正,批评!!!
      废话不多说,开始正题。
      本文涉及以下几方面的内容:
      1、API函数EnumWindows 及GetWindowThreadProcessId在VB.NET中的使用
      2、函数中ByVal和ByRef的使用注意事项

      一、EnumWindows 函数的运行流程

      EnumWindows 函数(枚举顶级窗口)和EnumChildWindows函数(枚举指定父窗口的子窗口)运行流程都差不多,会了一个就会另外一个。
      EnumWindows 枚举窗口,将窗口句柄传递给其回调函数,回调函数根据这个句柄进行相关的判断和处理事件,并返回一个布尔值给EnumWindows,如果返回true则EnumWindows 函数继续枚举,返回false则停止枚举。
      EnumWindows 和其回调函数用第二个参数传递程序需要的值。(这句话比较难懂,也是这个函数的难点,下面会有详细的解释)

      二、函数的声明

       两个函数在vb中的声明:

    Declare Sub EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    

      两个函数在vb.net中的声明:

    Private Declare Ansi Function EnumWindows Lib "user32" Alias "EnumWindows"(ByVal lpEnumFunc As EnumWindowsProc, ByRef lParam As EnumWindowsArg) As Integer  '声明API函数EnumWindows
    Private Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer '第二个参数一定要用ByRef类型。
    

      vb.net中声明的几点说明:
       1、Function 后面的EnumWindows 只是函数的名称,你可以随便改成自己喜欢的名字,但是Alias 后面的名称不能改,那是windows函数库中的标准名称,改了程序就找不到库中的函数了。GetWindowThreadProcessId
    类同。
       2、Ansi的意义:EnumWindows 声明时在Function 前要不要加Ansi ,网上查的资料说需要加,我不加测试也通过了。还请明白声明时Ansi的意义的大佬指点一下;
       3、EnumWindows 函数第二个参数lParam 的意义:
       原文是这样写的:在枚举期间,传递给dwcbkd32.ocx定制控件之EnumWindows事件的值。这个值的含义是由程序员规定的。
       关于这个参数的含义和用法查了很多资料,都是c语言的,而且也没有表达清楚到底怎么传递参数的值。vb.net的相关资料几乎没有。经过我的不断摸索,终于搞清楚这个参数的用法,在此分享一下:
       EnumWindows 函数第二个参数 “lParam” 只是变量名,你可以随便改 ,变量前面的ByVal 和ByRef 使用要小心,ByVal 的意思是只传递变量的值,ByRef 只传递变量的地址。EnumWindows函数使用第二个参数和其回调函数某个参数来回传递变量的地址,也就是说回调函数某个参数的值发生改变,EnumWindows函数的第二个参数的值也随之而改变,这是函数的要求,所以你如果用第二个参数,必须用传递地址的方式使用第二个参数,当然你也可以不用第二个参数。还有一点,EnumWindows函数第二个参数 “lParam” 可以是你自己定义的变量类型,本例就是我自己定义的变量类型:EnumWindowsArg,具体定义如下:

    Structure EnumWindowsArg
            Dim HwndWindow As Integer
            Dim ProcessID As Integer
    End Structure
    

       此变量包含有窗口句柄和窗口进程ID两个属性。你也可以把第二个参数定义成你自己需要的数据类型。
       4、声明到此,还没完,你还需用Delegate声明一个委托函数,因为VB.NET中,EnumWindows函数不能直接调用你的回调函数,不声明为委托函数,你用AddressOf是无效的,无法获取回调函数的地址。
       什么是委托?
       简单的理解,委托就是对一个方法映射,把方法名当参数来传递,类似JS中的在某个function的参数中把另一个function的名称当参数来传递。也就是把函数(一种方法的定义)名当作另一个函数的参数使用。委托可以理解为一种接口,具体的方法实现这个接口,在开发阶段只需要关心委托的定义就可以调用,而不用关心它如何实现的或者在哪里实现的。
       vb.net中委托函数调用时要先实例化,然后通过AddressOf调用实例化的指针来执行委托函数的方法。
       是不是有点云山雾罩的感觉了,那就对了,看个实例就全都理解了。

       定义委托函数(即回调函数的方法,相当于类,class):

    Private Delegate Function EnumWindowsProc(ByVal Hwnd As Integer, ByRef ewa As EnumWindowsArg) As Boolean '声明一个委托函数
    

      三、具体代码

       1、回调函数代码:

     '自定义回调函数,EnumWindows函数接收其返回值,返回true则继续enum,返回false则停止enum。
        '参数Hwnd为EnumWindows函数枚举窗口的句柄
        '参数ewa为按址传值的自定义参数,其参数名称可以和EnumWindows函数里第二个参数不一样,但参数类型必须与EnumWindows函数第二个参数相同。
        '相当于和EnumWindows共用的一块地址,两个函数都可以更改和读取这个地址里的值。
        '注意:这个回调函数要与定义的委托函数的参数类型要兼容,最好相同。
        Private Function EnumWindowsProc_GetHwndByPID(ByVal Hwnd As Integer, ByRef ewa1 As EnumWindowsArg) As Boolean
            Dim pArg As EnumWindowsArg = ewa1
            Dim ProcessID As Integer
    
       '通过枚举的窗口句柄用windows api 函数 GetWindowThreadProcessId取得窗口进程ID
            '与要查找的窗口进程比较,相同,则找到了该进程的窗口句柄。
            Dim threadID = GetWindowThreadProcessId(Hwnd, ProcessID)
            If ProcessID = pArg.ProcessID Then
                pArg.HwndWindow = Hwnd
                '找到了返回FALSE
                ' Return False
            End If
            If threadID = pArg.ProcessID Then
                pArg.HwndWindow = Hwnd
                Return False
            End If
            '没找到,继续找,返回TRUE
            Return True
    
       End Function
    

       2、主函数

     Public Function GetWindowhandleByProcessID(ByVal ProcessID As Integer) As Integer '通过窗口的进程ID获取窗口句柄
            Dim Hwndrtn As Integer '返回窗口句柄的变量
            Dim ewa As EnumWindowsArg '自定义的变量,用来传递窗口进程和窗口句柄
            ewa.ProcessID = ProcessID '
            ewa.HwndWindow = 0
            Dim Prc As EnumWindowsProc = New EnumWindowsProc(AddressOf EnumWindowsProc_GetHwndByPID) '实例化一个回调函数
    
            'EnumWindows枚举窗口,prc为实例化的回调函数指针(地址),EnumWindows将枚举窗口的句柄传递给回调函数prc处理。 
            'ewa为向回调函数按址传递的一个参数, 就是可以向回调函数传值, 也可以接收从回调函数返回的值。
            EnumWindows(Prc, ewa)
            If ewa.HwndWindow <> 0 Then
                Hwndrtn = ewa.HwndWindow
            End If
            Return Hwndrtn
        End Function
    
    展开全文
  • 这样类似的功能,通常都是需要通过获取句柄的方式,然后发送Message来实现。以前我都是用VB6.0来实现,那是一个很老的编程环境,微软也不维护它了。还好,现在有了更为强大和满是甜糖的C#,于是怎样用C#来实现句柄...
  • vb仿SPY控件(拖动鼠标获取窗口句柄、类名、进程名、标题等参数) 从VB梦工厂淘到的一个vb仿SPY控件(拖动鼠标获取窗口句柄、类名、进程名、标题等参数),以后都不用API操作了!感谢原作者。 点击...
  • VB.NET通过窗口句柄获取webbrowser控件HTML内容 通过Sendmessage获取IE浏览器内容 webbrowser控件内容获取 Class Win32API  _  Public Shared Function FindWindow(ByVal lpClassName As String, ByVal ...
  • VB Spy获取进程句柄源码实例,列出出当前运行程序的窗口信息、子进程信息和鼠标坐标、句柄信息,程序可设置窗口总在最前端显示。
  • 使用FindWindow和FindWindowEx找出路径文本框和保存按钮的句柄 使用SendMessage和SendMessageEX将自定义路径字符串输入至上述的文本框,并后台模拟点击保存按钮。 基本的思路是没有问题的,如文本编辑器的另存为...
  • vb-EnumChildWindows所有窗口及子窗口句柄是一款vb枚举窗口枚举出所有父窗口和其对应的子窗口句柄信息获取查看工具,每个子窗口控件都具有一个在其兄弟中唯一的窗口句柄和ID值。对于句柄和ID这两者,知道其中的一个...
  • 操作一软件,所有信息都显示在文本框里的。我可以用VB获取该软件的文本框窗口句柄。 但我想把文本框里的数据取出来入打开的记事本。应该怎样做。用VB的
  • vb 获取任意程序窗体句柄

    热门讨论 2011-08-13 04:12:15
    可以获取正常情况下无法获取窗口句柄,某些网络游戏会有屏蔽正常获取游戏窗口句柄,用此代码可以正常
  • ' 功能: 通过窗口句柄得到模块名称 '******************************************************************************** ' Public Function GetModuleFileNameByHwnd(ByVal hWindow As Long) As String    ...
  • 关于如何根据IE窗口句柄hWnd获得IWebBrowser接口,网络上代码很多,大多是使用了MSAA里的一个函数GetObjectFromLResult进行获取,一方面,许多朋友尤其是VB爱好者对MSAA技术并不了解,另一方面,用此方法非常繁琐。...
  • 获取窗口标题和句柄

    2011-11-26 10:39:47
    通过vb写的代码,获取Windows 窗口正在运行的程序名称和句柄,非常适合学习句柄的人

空空如也

空空如也

1 2 3 4 5 6
收藏数 116
精华内容 46
关键字:

vb获取窗口句柄