vb.net句柄找窗口,vbnet获取当前窗口句柄
vb通过句柄获得窗口名称
获取窗体句柄通常有如下几个API函数:1、用于父窗口,findwindow FindWindowVB声明Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long说明寻找窗口列表中第一个符合指定条件的顶级窗口(在vb里使用:FindWindow最常见的一个用途是获得ThunderRTMain类的隐藏窗口的句柄;该类是所有运行中vb执行程序的一部分。获得句柄后,可用api函数GetWindowText取得这个窗口的名称;该名也是应用程序的标题)返回值Long,找到窗口的句柄。如未找到相符窗口,则返回零。会设置GetLastError参数表参数类型及说明lpClassNameString,指向包含了窗口类名的空中止(C语言)字串的指针;或设为零,表示接收任何类lpWindowNameString,指向包含了窗口文本(或标签)的空中止(C语言)字串的指针;或设为零,表示接收任何窗口标题注解很少要求同时按类与窗口名搜索。为向自己不准备参数传递一个零,最简便的办法是传递vbNullString常数示例Dim hw, cnt
创新互联公司专业为企业提供涪陵网站建设、涪陵做网站、涪陵网站设计、涪陵网站制作等企业网站建设、网页设计与制作、涪陵企业网站模板建站服务,10余年涪陵做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
Dim rttitle As String * 256
hw = FindWindow("ThunderRT5Main", vbNullString) ' ThunderRTMain under VB4
cnt = GetWindowText(hw, rttitle, 255)
MsgBox Left$(rttitle, cnt), 0, "RTMain title"2、用于子窗口:findwindowexFindWindowExVB声明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说明在窗口列表中寻找与指定条件相符的第一个子窗口返回值Long,找到的窗口的句柄。如未找到相符窗口,则返回零。会设置GetLastError参数表参数类型及说明hWnd1Long,在其中查找子的父窗口。如设为零,表示使用桌面窗口(通常说的顶级窗口都被认为是桌面的子窗口,所以也会对它们进行查找)hWnd2Long,从这个窗口后开始查找。这样便可利用对FindWindowEx的多次调用找到符合条件的所有子窗口。如设为零,表示从第一个子窗口开始搜索lpsz1String,欲搜索的类名。零表示忽略lpsz2String,欲搜索的类名。零表示忽略 3、获取窗体标题:GetwindowText GetWindowTextVB声明Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long说明取得一个窗体的标题(caption)文字,或者一个控件的内容(在vb里使用:使用vb窗体或控件的caption或text属性)返回值Long,复制到lpString的字串长度;不包括空中止字符。会设置GetLastError参数表参数类型及说明hwndLong,欲获取文字的那个窗口的句柄lpStringString,预定义的一个缓冲区,至少有cch+1个字符大小;随同窗口文字载入cchLong,lpString缓冲区的长度注解不能用它从另一个应用程序的编辑控件中获取文字 4、获取窗体的类名:GetclassName GetClassNameVB声明Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long说明为指定的窗口取得类名返回值Long,以字节数表示的类名长度;排除最后的空中止字符。零表示出错。会设置GetLastError参数表参数类型及说明hwndLong,欲获得类名的那个窗口的句柄lpClassNameString,随同类名载入的缓冲区。预先至少必须分配nMaxCount+1个字符nMaxCountLong,由lpClassName提供的缓冲区长度Top 给你举例如下:Private Const MaxControlUnit = 65535
Private Sub Command1_Click()
Dim hwnd As Long
Dim hWnd2 As Long
Dim Caption As String * 255
Dim ClassName As String * 255
Dim Output As String
Dim i As Long
hwnd = FindWindow(vbNullString, "Form1")
For i = 0 To MaxControlUnit
hWnd2 = FindWindowEx(hwnd, hWnd2, vbNullString, vbNullString)
If hWnd2 0 Then
GetClassName hWnd2, ClassName, Len(ClassName)
GetWindowText hWnd2, Caption, Len(Caption)
Output = "句柄是:" hWnd2 " " "类名为:" ClassName
MsgBox Output
Output = "标题是:" Caption
MsgBox Output
Else
Exit For
End If
Next
End Sub
'只要修改那个form1,改成你要的窗体名,就可以了
求vb.net句柄实例,实现操作其他程序窗口。如我给的例子
Imports System.Text
Imports System.Runtime.InteropServices
Public Class Form1
' 相关API函数声明,注释掉的这里没用到,但是也比较常用吧,这些函数的功能都能搜到。
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
' 相关消息定义,也有没用到的
Const WM_SETTEXT = HC
Const WM_GETTEXT = HD
'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
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1_Click(sender, e)
End Sub
' EnumChildWindows 回调函数,该函数名作为API函数EnumChildWindows 的一个参数
' 该函数实现了枚举各个子窗口,找出编辑框属性的功能
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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WindowHandle = FindWindow(vbNullString, "登陆")
If WindowHandle.ToInt32 = 0 Then
MsgBox("未捕获到窗口" + "登陆")
Return
End If
' 枚举所有主窗口的子窗口(控件),枚举时自动调用回调函数,完成编辑框句柄的获取
EnumChildWindows(WindowHandle, AddressOf EnumChildProcC, 0)
' 寻找复选框
CheckHandle = FindWindowEx(WindowHandle, IntPtr.Zero, vbNullString, "记住密码")
Dim str As New StringBuilder
Dim j As Integer = 0
' 对编辑框文本赋值
For j = 0 To EditHandle.Count - 1
SendMessage(EditHandle(j), WM_SETTEXT, 0, "Text")
'GetWindowText(EditHandle(j), str, 20)
'EditWindowsText.Add(Str.ToString)
'Str.Clear()
Next
If EditHandle.Count = 0 Then
MsgBox("未找到输入框!")
End If
If CheckHandle.ToInt32 0 Then
'CheckDlgButton(WindowHandle, id, 1)
' 对复选框进行鼠标单击操作
SendMessage(CheckHandle, BM_CLICK, 0, 0)
'SendMessage(CheckHandle, BM_SETCHECK, True, 0)
End If
End Sub
End Class
vb.net如何通过窗口句柄显示窗口
首先声明一下API
在你窗口最顶的位置上写上
Private Declare Function 取窗口句柄 Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'由于VB支持中文的 我就定义成 取窗口句柄 这个了
'在窗体中添加一个按钮里面放进下面代码就OK了
Private Sub Command1_Click()
Dim 窗口句柄 As Long '声明窗口句柄变量存储
窗口句柄 = 取窗口句柄(vbNullString, "新建 文本文档.txt - 记事本") '取窗口标题的句柄 把得到窗口标题句柄赋予给刚才 窗口句柄 这变量里
If 窗口句柄 = 0 Then '判断窗口句柄是否等于0 如果等于0表示无法找到窗口句柄
MsgBox "无法找到需要的窗口句柄"
Else
MsgBox "已找到窗口句柄 句柄数为:" 窗口句柄
End If
End Sub
vb.net如何查找打开的窗口
将NewForm设置为全局变量就可以了,如果新建窗口比较多,就设置为数组,例如:
Public Class Form1
Dim NewForm(10) As Form
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
NewForm(0) = New Form
NewForm(0).Show()
End Sub
End Class
当前题目:vb.net句柄找窗口,vbnet获取当前窗口句柄
标题网址:http://scyanting.com/article/hcppep.html