基於IAccessible接口的QQ窗口信息獲取的實現

這個技術如今已經封裝成DLL免費開放給你們使用了,詳情請加羣221487171編程

能夠訪問官方網站下載 http://www.guihelper.com/app

主要技術(Microsoft Active Accessibility)講解:ide

如下是微軟對於此技術的說明函數

Microsoft Active Accessibility

Version 2.0

Purpose

Microsoft® Active Accessibility® 2.0 is a COM-based technology that improves the way accessibility aids work with applications running on Microsoft Windows®. It provides dynamic-link libraries that are incorporated into the operating system as well as a COM interface and application programming elements that provide reliable methods for exposing information about user interface elements.測試

For a copy of the Active Accessibility SDK documentation formatted in Microsoft Word or WinHelp, see the Accessibility home page on MSDN.網站

Where Applicable

By using Active Accessibility and following accessible design practices, developers can make applications running on Windows more accessible to many people with vision, hearing, or motion disabilities.ui

Developer Audience

Active Accessibility is designed primarily for C, C++, and Microsoft Visual Basic® developers. In general, developers need a moderate level of understanding about COM objects and interfaces as well as about Unicode.spa

Run-Time Requirements

Full support for Active Accessibility 2.0 is built into the Windows XP and Windows Server 2003 operating systems. Active Accessibility 2.0 also supports Microsoft Windows NT® 4.0 with Service Pack 6 and Windows 98.code

MSAA的全稱是Microsoft Active Accessibility。這是相似DCOM技術。技術模型是這樣的,UI程序能夠暴露出一個Interface,方便另外一個程序對其進行控制。 MSAA技術的初衷是爲了方便殘疾人使用Windows 程序。好比盲人看不到窗口,可是盲人能夠經過一個USB讀屏器鏈接到電腦上, 讀屏器經過UI程序暴露出來的這個Interface,就能夠獲取程序信息,經過盲文或者其它形式傳遞給盲人。MSAA提供瞭如此方便的功能, UI自動化測試天然能夠借用這項技術。MSAA暴露出來的Interface叫作IAccessible。測試程序和目標UI程序互操做流程以下:orm

  1. 測試程序調用Windows API: AccessibleObjectFromWindow,傳入目標UI程序HWND。

  2. AccessibleObjectFromWindow函數向UI程序發送WM_GETOBJECT消息。

  3. UI程序建立實現了IAccessible的內部類,而後經過LresultFromObject API把IAccessible 接口返回給測試程序。

  4. 測試程序拿到IAccessible接口,開始調用IAccessible接口函數操做測試目標。

  IAccessible接口裏面的幾個關鍵函數是:

  IAccessible.get_accChild/ IAccessible.get_accParent經過這兩個函數,調用者能夠瀏覽目標程序的窗口關係樹,定位到UI元素。

  IAccessible.accLocation/I Accessible.accHitTest讀取和分辨目標元素的屏幕位置。

  IAccessible.accName/ I Accessible.accSelect讀取元素的名字,對UI元素進行指定的操做,好比選取Listbox裏面的某一項等等。

  IAccessible.accValue 開發人員能夠自定義value屬性的實現。好比針對折線圖控件,開發人員能夠在accValue中返回折線的座標數列。

  MSAA的理念 相似於test hook。 經過主動讓UI程序暴露一個接口來讓調用者控制。 在具體使用中,測試人員每每是結合MSAA和Win32 API操做,取長補短。一方面對於UI元素豐富的屬性,好比style,鉤選狀態,是否最大化和模擬用戶輸入等,繼續採用Win32 API。 另外一方面用MSAA的優點來彌補Win32 API的一些不足,好比:

  因爲MSAA有本身的get_accChild方法,使其控件樹關係並不必定要和Win32 HWNDd關係對應一致。對於自繪窗口,雖說只有一個HWND, 可是開發人員能夠經過實現IAccessible接口來實現邏輯上的層次關係。好比Excel中就能夠經過IAccessible把多個cell的子 IAccessible接口暴露給調用者。

  IAccessible的實現是由開發者提供, 開發者能夠靈活地根據實際狀況決定方法的實現。好比前面提到了折線圖控件能夠返回座標數列。對於.NET WinForm, 微軟在Framework中就提供了IAccessible的默認實現,這樣在具體實現中,就能夠處理.NET動態維護HWND的細節。

可見這個接口主要是用於軟件測試和實現特殊的UI需求所用的 , 瞭解到這個技術以後我就想QQ會不會也使用了這個技術實現軟件的測試呢 ?

結果是確定的 , QQ的GUI也使用了這個接口 , 因此咱們能夠經過這個接口獲取QQ窗口內部的信息 , 並且信息很全面 , 好在.NET中已經封裝好了這一接口使得這一編程過程變得比較容易 , IAccessible接口在Accessibility命名空間下

實現窗口信息的獲取須要配合oleacc.dll中提供的API來進行操做 , 下面是使用到的主要的API函數名

 1 public static extern int AccessibleChildren(
 2             IAccessible paccContainer,
 3             int iChildStart,
 4             int cChildren,
 5             [Out()] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)] object[] rgvarChildren,
 6             ref int pcObtained);
 7 
 8 internal static extern int AccessibleObjectFromWindow(
 9             IntPtr hwnd,
10             uint id,
11             ref Guid iid,
12             [In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object ppvObject);
13 
14 public static extern uint WindowFromAccessibleObject(IAccessible pacc, ref IntPtr phwnd);

 轉載請註明出處 : 文章來自博客園SadlyCodes的博客 http://www.cnblogs.com/SadlyCodes

但願你們能夠經過這篇文章瞭解到更多的關於IAccessible接口的知識

最後附一張個人測試DEMO圖:

爲了防止盜圖我加上了我本身的水印  若是違規請通知我  我會當即更改  謝謝

相關文章
相關標籤/搜索