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

如何用VB获取窗口内已知控件句柄的内容。如:已知某窗口的句柄和窗口内Text的句柄,请问如何取得Text的内容。内容为数字。请给出源码!!分提问用完了。不好意思。... 如何用VB获取窗口内已知控件句柄的内容。

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

内容为数字。

请给出源码!!分提问用完了。不好意思。
展开
 我来答
八零冷月藏刀
2010-05-19 · 超过10用户采纳过TA的回答
知道答主
回答量:38
采纳率:0%
帮助的人:21.4万
展开全部
我有个实例:打开记事本+获得记事本的句柄+向记事本中写入字符串+更改记事本的标题+获得所有运行的程序的标题。代码如下:
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

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式