這是一個(gè)vb用findwindow等api找到其它窗口,并且更改其它窗口的文本的源碼:
為淇縣等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及淇縣網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、網(wǎng)站制作、淇縣網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
里面也有源碼下載的
如果你到它的文章列表中,會(huì)找到許多其它相關(guān)的文章,都是api操作其它窗體的例子
首先聲明一下API
在你窗口最頂?shù)奈恢蒙蠈懮?/p>
Private Declare Function 取窗口句柄 Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'由于VB支持中文的 我就定義成 取窗口句柄 這個(gè)了
'在窗體中添加一個(gè)按鈕里面放進(jìn)下面代碼就OK了
Private Sub Command1_Click()
Dim 窗口句柄 As Long '聲明窗口句柄變量存儲(chǔ)
窗口句柄 = 取窗口句柄(vbNullString, "新建 文本文檔.txt - 記事本") '取窗口標(biāo)題的句柄 把得到窗口標(biāo)題句柄賦予給剛才 窗口句柄 這變量里
If 窗口句柄 = 0 Then '判斷窗口句柄是否等于0 如果等于0表示無(wú)法找到窗口句柄
MsgBox "無(wú)法找到需要的窗口句柄"
Else
MsgBox "已找到窗口句柄 句柄數(shù)為:" 窗口句柄
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函數(shù).
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 '這句是定義向獲得窗口句柄發(fā)送按鍵消息的API函數(shù).
例子:你先建一個(gè)工程
form1.caption="12345
畫一個(gè)command1 名字為 "確定1"
事件
Private Sub Command1_Click()
msgbox Me.Hwnd '顯示確定按鈕的句柄
End Sub
生成12345.exe
關(guān)閉VB6
打開VB6
再建一個(gè)工程 代碼如下:
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
然后先運(yùn)行 事先生成好的12345.exe
再運(yùn)行后建的那個(gè)
你單擊確定 就會(huì)使12345.exe的確定按鈕被單擊 返回它的hwnd
我的方法是先找到12345.exe的主窗口 然后根據(jù)主窗口的句柄獲得其確定按鈕的句柄 再使用sendmessage 發(fā)送模擬鼠標(biāo)左鍵單擊
VB控件的句柄 一般可用me.hwnd 方法獲得
可以說(shuō)下你要干哈嗎?
我看了一下這個(gè)框框,里面只有一個(gè)類名為edit的,也就是你要獲取的框框,應(yīng)該比較簡(jiǎn)單的,只要你能找到他的爸爸就可以了。
找窗體的句柄得用到API了,最常用的是:FindWindow(一般只找父窗口句柄),F(xiàn)indWindowEx(可找子窗口的句柄),給個(gè)例子看看,只用到一個(gè)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 = "標(biāo)題是:" Caption
MsgBox Output
Else
Exit For
End If
Next
End Sub
只要修改那個(gè)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
說(shuō)明
在窗口列表中尋找與指定條件相符的第一個(gè)子窗口
返回值
Long,找到的窗口的句柄。如未找到相符窗口,則返回零。會(huì)設(shè)置GetLastError
參數(shù)表
參數(shù)
類型及說(shuō)明
hWnd1
Long,在其中查找子的父窗口。如設(shè)為零,表示使用桌面窗口(通常說(shuō)的頂級(jí)窗口都被認(rèn)為是桌面的子窗口,所以也會(huì)對(duì)它們進(jìn)行查找)
hWnd2
Long,從這個(gè)窗口后開始查找。這樣便可利用對(duì)FindWindowEx的多次調(diào)用找到符合條件的所有子窗口。如設(shè)為零,表示從第一個(gè)子窗口開始搜索
lpsz1
String,欲搜索的類名。零表示忽略
lpsz2
String,欲搜索的類名。零表示忽略