十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这是一个vb用findwindow等api找到其它窗口,并且更改其它窗口的文本的源码:
为淇县等地区用户提供了全套网页设计制作服务,及淇县网站建设行业解决方案。主营业务为成都网站建设、网站制作、淇县网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
里面也有源码下载的
如果你到它的文章列表中,会找到许多其它相关的文章,都是api操作其它窗体的例子
首先声明一下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
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
'这是定义获得窗口句柄的API函数.
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 '这句是定义向获得窗口句柄发送按键消息的API函数.
例子:你先建一个工程
form1.caption="12345
画一个command1 名字为 "确定1"
事件
Private Sub Command1_Click()
msgbox Me.Hwnd '显示确定按钮的句柄
End Sub
生成12345.exe
关闭VB6
打开VB6
再建一个工程 代码如下:
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 PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) 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_LBUTTONDOWN = H201
Private Const WM_LBUTTONUP = H202
Private Const MK_LBUTTON = H1
Dim hpwnd As Long, hcwnd As Long
Dim iResult As Long
Private Sub Command1_Click()
hpwnd = FindWindow(vbNullString, "12345")
hcwnd = FindWindowEx(hpwnd, 0, vbNullString, "确定")
SetForegroundWindow hcwnd
iResult = SendMessage(hcwnd, WM_LBUTTONDOWN, 0, 0)
iResult = SendMessage(hcwnd, WM_LBUTTONUP, 0, 0)
End Sub
然后先运行 事先生成好的12345.exe
再运行后建的那个
你单击确定 就会使12345.exe的确定按钮被单击 返回它的hwnd
我的方法是先找到12345.exe的主窗口 然后根据主窗口的句柄获得其确定按钮的句柄 再使用sendmessage 发送模拟鼠标左键单击
VB控件的句柄 一般可用me.hwnd 方法获得
可以说下你要干哈吗?
我看了一下这个框框,里面只有一个类名为edit的,也就是你要获取的框框,应该比较简单的,只要你能找到他的爸爸就可以了。
找窗体的句柄得用到API了,最常用的是:FindWindow(一般只找父窗口句柄),FindWindowEx(可找子窗口的句柄),给个例子看看,只用到一个command控件,希望可以帮到你,不了解可以再咨询:Option Explicit
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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
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,改成你要的窗体名,就可以了
FindWindowEx
VB声明
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
参数表
参数
类型及说明
hWnd1
Long,在其中查找子的父窗口。如设为零,表示使用桌面窗口(通常说的顶级窗口都被认为是桌面的子窗口,所以也会对它们进行查找)
hWnd2
Long,从这个窗口后开始查找。这样便可利用对FindWindowEx的多次调用找到符合条件的所有子窗口。如设为零,表示从第一个子窗口开始搜索
lpsz1
String,欲搜索的类名。零表示忽略
lpsz2
String,欲搜索的类名。零表示忽略