VB VBA遍歷指定窗體所有控件
- 2017-09-20 08:00:00
- 硬軟兼得的博客 轉(zhuǎn)貼
- 13008
用VB 遍歷 指定程序窗口下 所有控件的句柄及控件中的文字
就是讀取另一個(gè)程序里標(biāo)簽顯示的文字代碼...
當(dāng)前前提是知道窗口句柄以及控件句柄才能讀取到
1. 標(biāo)簽有句柄,是由 SetWindowText 實(shí)現(xiàn)的(其實(shí)從底層一點(diǎn)看,還是發(fā)送了 WM_SETTEXT 消息),
例如VC、Masm 32
的程序。這種情況好解決,GetWindowText 或 發(fā)送WM_GETTEXT消息就OK了;
2. TextOut 畫(huà)上去的(例如 VB 就是這樣的)。這種情況要 hook TextOut;
GetWindowText()或SendMessage()都無(wú)法取得vb程序的label的文字,因?yàn)関b的label沒(méi)有handle, 但大家發(fā)現(xiàn) KingSoft CIBA 可以取得vb的label值,這是因?yàn)?KingSoft CIBA 攔下了Win32API中的textOut函數(shù)
用VB調(diào)用API函數(shù) 遍歷窗口下所有控件及取它的文本內(nèi)容
Private Const GW_CHILD = 5 Private Const GW_HWNDFIRST = 0 Private Const GW_HWNDNEXT = 2 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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Private Declare Function IsWindow Lib "user32" (ByVal hwnd 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 Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Sub FillChild(hWndParent As Long) Dim hWndChild As Long Dim szCaption As String Dim buffer As String Dim i As Long hWndChild = GetWindow(hWndParent, GW_CHILD) If (hWndChild = 0) Then Exit Sub hWndChild = GetWindow(hWndChild, GW_HWNDFIRST) If hWndChild = 0 Then Exit Sub While (hWndChild <> 0) szCaption = String$(255, 0) GetClassName hWndChild, szCaption, 250 szCaption = Left$(szCaption, InStr(szCaption, String$(1, 0)) - 1) buffer = CStr(hWndChild) & "--" & szCaption i = GetWindowTextLength(hWndChild) szCaption = String$(255, 0) GetWindowText hWndChild, szCaption, 250 szCaption = Left$(szCaption, i) buffer = buffer & "--" & szCaption List1.AddItem buffer FillChild hWndChild hWndChild = GetWindow(hWndChild, GW_HWNDNEXT) Wend End Sub Private Sub GetChildWindow(hwnd As Long) Dim szCaption As String Dim buffer As String Dim i As Long List1.Clear szCaption = String$(255, 0) GetClassName hwnd, szCaption, 250 szCaption = Left$(szCaption, InStr(szCaption, String$(1, 0)) - 1) buffer = CStr(hwnd) buffer = buffer & "--" & szCaption i = GetWindowTextLength(hwnd) szCaption = String$(255, 0) GetWindowText hwnd, szCaption, 250 szCaption = Left$(szCaption, i) buffer = buffer & "--" & szCaption List1.AddItem buffer FillChild hwnd End Sub 調(diào)用方法 GetChildWindow hwnd'hwnd是指定的窗口句柄 結(jié)果以 窗體句柄--窗體類(lèi)名稱(chēng)--窗體Text 形式列在列表框List1中,沒(méi)時(shí)間放到TreeView里了 只能獲取有句柄項(xiàng)的內(nèi)容,VB的Label是獲取不到的,這個(gè)只能做攔截 VC的STATIC上內(nèi)容可以被獲取 Text的內(nèi)容也無(wú)法獲取,這個(gè)要靠 WM_GETTEXT來(lái)獲取 Button的內(nèi)容可獲取 === 添加一個(gè)按鈕,一個(gè)listview,代碼如下,保證好用 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 GetParent Lib "user32" (ByVal hwnd 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 Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Type mType '自定義數(shù)據(jù)類(lèi)型 fhwnd As Long '窗口句柄 fText As String * 255 '窗口標(biāo)題 fRect As RECT '窗口矩形 pHwnd As Long '父窗句柄 pText As String * 255 '父窗標(biāo)題 End Type Private Sub mGetAllWindow(m_Type() As mType) '獲取控件信息,寫(xiě)成SUB了,其實(shí)用FUNCTION返回值也可以,只是函數(shù)里面定義就多了,總的來(lái)看需要2個(gè)mType數(shù)組,這樣做只需要一個(gè),占用空間小了 Dim Wndback As Long '上一個(gè)被查找的目標(biāo)句柄 Dim i As Long '數(shù)組控制 Do ReDim Preserve m_Type(i) DoEvents m_Type(i).fhwnd = FindWindowEx(0, Wndback, vbNullString, vbNullString) '獲取hwnd,第一個(gè)參數(shù)指定為0,查找桌面子窗口,第2個(gè)參數(shù)是開(kāi)始查找的窗口,第34個(gè)參數(shù)使函數(shù)查找所有窗口 If m_Type(i).fhwnd = 0 Then '=0時(shí)已經(jīng)查找一遍了,退出 Exit Sub Else '否則獲取控件相關(guān)消息 GetWindowText m_Type(i).fhwnd, m_Type(i).fText, 255 '獲取標(biāo)題 GetWindowRect m_Type(i).fhwnd, m_Type(i).fRect '獲取RECT m_Type(i).pHwnd = GetParent(m_Type(i).fhwnd) '獲取父HWND GetWindowText m_Type(i).pHwnd, m_Type(i).pText, 255 '獲取父標(biāo)題 End If Wndback = m_Type(i).fhwnd '保存上一個(gè)查的句柄 i = i + 1 Loop End Sub Private Sub Command1_Click() Dim cType() As mType mGetAllWindow cType() Dim i As Long ListView1.ListItems.Clear For i = LBound(cType) To UBound(cType) ListView1.ListItems.Add , "a" & i, cType(i).fhwnd ListView1.ListItems("a" & i).SubItems(1) = cType(i).fText ListView1.ListItems("a" & i).SubItems(2) = cType(i).fRect.Left ListView1.ListItems("a" & i).SubItems(3) = cType(i).fRect.Bottom ListView1.ListItems("a" & i).SubItems(4) = cType(i).fRect.Top ListView1.ListItems("a" & i).SubItems(5) = cType(i).fRect.Right ListView1.ListItems("a" & i).SubItems(6) = cType(i).pHwnd ListView1.ListItems("a" & i).SubItems(7) = cType(i).pText Next End Sub Private Sub Form_Load() ListView1.ColumnHeaders.Add , , "句柄", 1200 ListView1.ColumnHeaders.Add , , "標(biāo)題", 2800 ListView1.ColumnHeaders.Add , , "Rect.Left", 800 ListView1.ColumnHeaders.Add , , "Rect.Bottom", 800 ListView1.ColumnHeaders.Add , , "Rect.Top", 800 ListView1.ColumnHeaders.Add , , "Rect.Right", 800 ListView1.ColumnHeaders.Add , , "父窗句柄", 1200 ListView1.ColumnHeaders.Add , , "父窗標(biāo)題", 2800 ListView1.View = lvwReport ListView1.FullRowSelect = True Command1.Caption = "刷新" End Sub EnumWindows getwindowtext
分享
Access數(shù)據(jù)庫(kù)自身
- office課程播放地址及課程明細(xì)
- Excel Word PPT Access VBA等Office技巧學(xué)習(xí)平臺(tái)
- 將( .accdb) 文件格式數(shù)據(jù)庫(kù)轉(zhuǎn)換為早期版本(.mdb)的文件格式
- 將早期的數(shù)據(jù)庫(kù)文件格式(.mdb)轉(zhuǎn)換為 (.accdb) 文件格式
- KB5002984:配置 Jet Red Database Engine 數(shù)據(jù)庫(kù)引擎和訪問(wèn)連接引擎以阻止對(duì)遠(yuǎn)程數(shù)據(jù)庫(kù)的訪問(wèn)(remote table)
- Access 365 /Access 2019 數(shù)據(jù)庫(kù)中哪些函數(shù)功能和屬性被沙箱模式阻止(如未啟動(dòng)宏時(shí))
- Access Runtime(運(yùn)行時(shí))最全的下載(2007 2010 2013 2016 2019 Access 365)
Access查詢(xún)技巧
Access VBA函數(shù)模塊
- access vba代碼太長(zhǎng),換行,分行的寫(xiě)法
- VB6 VBA Access真正可用并且完美支持中英文的 URLEncode 與 URLDecode 函數(shù)源碼
- 自定義VB中的urlencode函數(shù),將URL中特殊部分進(jìn)行編碼
- Access 函數(shù)簡(jiǎn)化串接sql字符串,減少符號(hào)導(dǎo)致的書(shū)寫(xiě)錯(cuò)誤
- vba完全關(guān)閉IE瀏覽器及調(diào)用IE瀏覽器的簡(jiǎn)單應(yīng)用
- 利用FollowHyperlink方法打開(kāi)超鏈接提示“無(wú)法下載您要求的信息”的解決方案
- 在access中用代碼打開(kāi)文本框中超鏈接地址
Access Activex第三方控件
- Activex控件或Dll 在某些電腦無(wú)法正常注冊(cè)的解決辦法(regsvr32注冊(cè)時(shí)卡住)
- office使用部分控件時(shí)提示“您沒(méi)有使用該ActiveX控件許可的問(wèn)題”的解決方法
- RTF文件(富文本格式)的一些解析
- Access樹(shù)控件(treeview) 64位Office下出現(xiàn)橫向滾動(dòng)條不會(huì)自動(dòng)定位的解決辦法
- Access中國(guó)樹(shù)控件 在win10電腦 節(jié)點(diǎn)行間距太小的解決辦法
- EXCEL 2019 64位版(Office 2019 64位)早就支持64位Treeview 樹(shù)控件 ListView列表等64位MSCOMMCTL.OCX控件下載
- VBA或VB6調(diào)用WebService(直接Post方式)并解析返回的XML
Access ADP Sql Server等
- 早期PB程序連接Sqlserver出現(xiàn)錯(cuò)誤
- MMC 不能打開(kāi)文件C:/Program Files/Microsoft SQL Server/80/Tools/Binn/SQL Server Enterprise Manager.MSC 可能是由于文件不存在,不是一個(gè)MMC控制臺(tái),或者用后來(lái)的MMC版
- sql server連接不了的解決辦法
- localhost與127.0.0.1區(qū)別
- Roych的淺談數(shù)據(jù)庫(kù)開(kāi)發(fā)系列(Sql Server)
- sqlserver 自動(dòng)備份對(duì)備份目錄沒(méi)有存取權(quán)限的解決辦法
- 安裝Sql server 2005 express 和SQLServer2005 Express版企業(yè)管理器 SQLServer2005_SSMSEE
Access 行業(yè)應(yīng)用開(kāi)發(fā)
- 金蝶KIS旗艦版 登錄時(shí)“類(lèi)型不匹配”
- access行業(yè)交流QQ群-部分行業(yè)交流群(倉(cāng)庫(kù) 人事 工資 考勤 CRM HRM MRP ERP 等)
- access垃圾分類(lèi)數(shù)據(jù)庫(kù)
- Office提高企業(yè)辦公管理效率
- Access交流網(wǎng)Acccess通用開(kāi)發(fā)平臺(tái)樹(shù)導(dǎo)航出錯(cuò)的解決辦法
- Access交流網(wǎng)Access通用開(kāi)發(fā)平臺(tái)的使用幫助教程及FAQ
- Access采購(gòu)倉(cāng)庫(kù)系統(tǒng)作品源代碼
文章分類(lèi)
聯(lián)系我們
聯(lián)系人: | 王先生 |
---|---|
Email: | 18449932@qq.com |
QQ: | 18449932 |
微博: | officecn01 |