VB 如何获取一外部程序窗口内已知控件句柄的内容

如何用VB获取窗口内已知控件句柄的内容。

如:已知某窗口的句柄和窗口内Text的句柄,请问如何取得Text的内容。

内容为数字。

请给出源码!!分提问用完了。不好意思。

第1个回答  2010-05-19
我有个实例:打开记事本+获得记事本的句柄+向记事本中写入字符串+更改记事本的标题+获得所有运行的程序的标题。代码如下:
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 5325
ClientLeft = 60
ClientTop = 450
ClientWidth = 7560
LinkTopic = "Form1"
LockControls = -1 'True
ScaleHeight = 5325
ScaleWidth = 7560
StartUpPosition = 2 '屏幕中心
Begin VB.ListBox List1
Height = 4380
Left = 360
TabIndex = 4
Top = 480
Width = 4095
End
Begin VB.CommandButton Command4
Caption = "获得所有程序标题"
Height = 495
Left = 4800
TabIndex = 3
Top = 2400
Width = 2535
End
Begin VB.CommandButton Command3
Caption = "打开写入记事本+改标题"
Height = 495
Left = 4800
TabIndex = 2
Top = 1800
Width = 2535
End
Begin VB.CommandButton Command2
Caption = "运行记事本"
Height = 495
Left = 4800
TabIndex = 1
Top = 1200
Width = 2535
End
Begin VB.CommandButton Command1
Caption = "打开写入记事本"
Height = 495
Left = 4800
TabIndex = 0
Top = 600
Width = 2535
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'1
Private Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long
'==========================================================
'3
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 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 Const WM_PASTE = &H302
'===========================================================
'4
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2
'
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long

Private Sub Command1_Click()
'1 打开 写入 记事本
Dim AppID As Integer
AppID = Shell("C:\WINDOWS\notepad.exe", 1)

AppActivate AppID
SendKeys "-2.356e+012年后光荣感如果好了" '"SendKeys--字符有误,速度慢"

End Sub
Private Sub Command2_Click()
WinExec "notepad.exe", 5

End Sub
Private Sub Command3_Click()
'3 打开 写入 记事本+改标题
Dim TemphWnd As Long
AppID = Shell("C:\WINDOWS\notepad.exe", 1)
TemphWnd = FindWindow("Notepad", vbNullString)
'--------------------------------------------
SetWindowText TemphWnd, "Welcome to VB" '
'--------------------------------------------
TemphWnd = FindWindowEx(TemphWnd, 0, "Edit", vbNullString)
If TemphWnd Then
'Debug.Print TemphWnd
VB.Clipboard.SetText "-2.356e+012年后光荣感如果好了" '"SendMessage--字符正确,速度快"
SendMessage TemphWnd, WM_PASTE, 0, ByVal 0&

End If
End Sub

Private Sub Command4_Click()
'4 获得所有程序标题
Dim ret As Long
Dim hwnd As Long
Dim str As String * 256
ret = GetDesktopWindow()
hwnd = GetWindow(ret, GW_CHILD)
Do While hwnd <> 0
GetWindowText hwnd, str, Len(str)
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
If Left$(str, 1) <> vbNullChar Then
Form1.List1.AddItem str
End If
Loop
End Sub

参考资料:WinAPI

本回答被提问者采纳

VB 如何获取一外部程序窗口内已知控件句柄的内容
'1 打开 写入 记事本 Dim AppID As Integer AppID = Shell("C:\\WINDOWS\\notepad.exe", 1)AppActivate AppID

vb怎么获取控件句柄和内容
先找到窗口的句柄,再来查找按钮的句柄,如果按钮有文本内容,那就好办,如果没有,那建议你利用SPY++先来察看一下按钮的类型,以此类型为查找参数多次调用FindWindowEx来查找,直到找到的的句柄和Spy++相同,那么这个按钮就找到了。按钮类名同样,但是你去遍历的时候它的次序始终是固定的。通过id不可靠,有...

VB获取外部窗口控件句柄的文本
第一个想法就是使用GetWindowText函数,设定其内容的话就是SetWindowText函数,其实不对,这两个函数对于其他的控件都能够正常工作,唯独对于EDIT控件不行,我在这边困惑了好久,甚至想去逆向一下程序,看看到底有没有发送WM_GETTEXT命令和WM_SETTEXT命令了,幸好有google。省却一番功夫。贴一段不能成功的...

VB 如何获取外部程序文本框内容
'获取控件的内容 Option Explicit 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 Const WM_GETTEXT = &HD Private Const WM_GETTEXTLENGTH = &HEPublic Function Ge...

VB 获取其他程序控件的句柄
先用findwindow找到窗体,用GetWindow和GetClassName获得其窗体内各对象的句柄,从而可以获得文本框的句柄。最后用SendMessage函数想文本框发送文本。tWnd = FindWindow(vbNullString, "窗口名")可以获得外部程序窗口的句柄twnd bwnd = GetWindow(tWnd, GW_CHILD)可以获得该窗口子对象的集合句柄bwnd Do ...

VB 已知外部程序上的一个文本框句柄,如何获得这个文本框内的内容?或...
SendMessage(Handle, WM_GETTEXT, buffer_size, buffer)实现:Dim strBuffer As String SendMessage WindowHandle, &HD, 255, ByVal strBuffer

如何在VB中打开一个记事本窗口并获取窗口句柄,向其中发送富文本框中的...
1.新建三个命令按钮Command1命名“写字”,Command1命名“禁用”,Command1命名“关闭”;2.在窗体写下如下代码:Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function FindWindowEx Lib...

VB获取外部程序的窗体信息
E_ActionType_获取焦点 E_ActionType_填入组合框 E_ActionType_查找窗体 E_ActionType_手动暂停 E_ActionType_获取文本框 End Enum Public Enum E_CID E_CID_名称 = 1 E_CID_父窗体名称 = 2 E_CID_步骤类型 = 3 E_CID_控件ID = 4 E_CID_参数 = 5 End Enum Public NowWindow As ...

vb获得外部程序的combobox内容
利用vb操作一个外部程序的窗体上的按钮可以用到控制鼠标的API函数。Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long '而仅仅有这个函数是不够的,还需要定义一个type格式的自定义变量。定义为:Private Type POINTAPI X As Long Y As Long End...

VB 如何获取外部程序的窗口位置
1 利用findwindow函数,通过窗体的标题(caption)来获取窗体的句柄 2 再利用SetWindowPos函数,利用窗体句柄来设置一个窗体的位置。。。你可以把一个外部窗口移动摆放在任何位置。。。自然就知道它的位置了

相似回答