2012-05-07 103 views
4

我在我的機器上安裝了桌面應用程序。當我開始一個程序時,某種窗口會打開。讓我們說,這樣的事情(例如只):如何捕獲窗口中的數據

enter image description here

所以,我想C#編寫的應用程序,會發現這個窗口,並從中獲取一些數據。

我應該看什麼工具?我想走一條阻力最小的路。

我需要捕獲圖像,文本框中的文本,還可以通過文本查找控件並單擊它們。

回答

1

你試圖捕獲什麼樣的數據?

您可以嘗試收聽Windows消息或閱讀內存。

+0

TExtboxes \ pictures。另外我需要點擊一個按鈕。我應該如何聽聽Windows消息? – user194076

+0

看看這篇文章:http://stackoverflow.com/questions/9665579/setting-up-hook-on-windows-messages並嘗試玩間諜++(http://technet.microsoft.com/en- us/library/dd460725.aspx) – animaonline

2

你應該開始枚舉所有窗口的句柄這個過程:

https://stackoverflow.com/a/2584672/351383

然後,每處理獲取有關文本和位置信息,與位置信息來源,你可以採取的桌面截圖上那個位置獲得圖像AFAIK沒有其他方式從正在運行的應用程序窗口獲取圖像。

當你得到了控制的屏幕位置,然後從下面的鏈接使用模擬鼠標左鍵點擊,搜索窗口的一些文字,然後點擊內部控制的一些點,這裏是會點擊一個點的方法:

https://stackoverflow.com/a/10355905/351383

我把toghether快​​速類收集過程數據:

public static class ProcessSpy 
    { 
    public static List<ProcessSpyData> GetDataForProcess(string processName) 
    { 
     var result = new List<ProcessSpyData>(); 
     Process myProc = Process.GetProcessesByName(processName).FirstOrDefault(); 
     if (myProc != null) 
     { 
     var myHandles = EnumerateProcessWindowHandles(myProc); 
     foreach (IntPtr wndHandle in myHandles) 
     { 
      result.Add(new ProcessSpyData(wndHandle)); 
     } 
     } 
     return result; 
    } 

    delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam); 

    [DllImport("user32.dll")] 
    static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam); 

    static IEnumerable<IntPtr> EnumerateProcessWindowHandles(Process prc) 
    { 
     var handles = new List<IntPtr>(); 

     foreach (ProcessThread thread in prc.Threads) 
     EnumThreadWindows(thread.Id, (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero); 

     return handles; 
    } 
    } 

    public class ProcessSpyData 
    { 
    private const uint WM_GETTEXT = 0x000D; 

    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam); 

    [DllImport("user32.dll")] 
    private static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect); 

    [StructLayout(LayoutKind.Sequential)] 
    private struct RECT 
    { 
     int left, top, right, bottom; 

     public Rectangle ToRectangle() 
     { 
     return new Rectangle(left, top, right - left, bottom - top); 
     } 
    } 

    [DllImport("user32.dll")] 
    static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint); 

    public IntPtr WindowHandle { get; private set; } 
    public string WindowText { get; private set; } 
    public Rectangle ClientRect { get; private set; } 
    public Rectangle ScreenPos { get; private set; } 

    public ProcessSpyData(IntPtr windowHandle) 
    { 
     this.WindowHandle = windowHandle; 
     GetWindowText(); 
     GetWindowSize(); 
    } 

    private void GetWindowText() 
    { 
     StringBuilder message = new StringBuilder(1024); 
     SendMessage(this.WindowHandle, WM_GETTEXT, message.Capacity, message); 
     this.WindowText = message.ToString(); 
    } 

    private void GetWindowSize() 
    { 
     var nativeRect = new RECT(); 
     GetClientRect(this.WindowHandle, out nativeRect); 
     this.ClientRect = nativeRect.ToRectangle(); 

     Point loc = this.ClientRect.Location; 
     ClientToScreen(this.WindowHandle, ref loc); 
     this.ScreenPos = new Rectangle(loc, this.ClientRect.Size); 
    } 
    } 

這應該讓你開始,但你必須要知道,如果應用程序是使用非標準控件則沒有用這種方法從文本中獲取文本的方式,對於圖像來說,查看可執行資源可能會獲得更好的結果。

UPDATE

葛亭控制文本的各種控制類型(MFC,的WinForms,德爾福VCL等)將是非常艱鉅的任務,但對於的WinForms看外觀極好Managed Windows API,他們甚至在某種間諜應用tools,看看那個。

+0

謝謝你一個不錯的答案。我從頂部開始......你知道什麼是可以讓我通過文本控制位置的函數的名稱(比方說按鈕)。任何內部user32.dll?我到了我的窗戶。現在我需要找到控制。 – user194076

+0

窗口是控制:)在Windows API沒有控件,控件通常是從很多窗口構造的。國際海事組織幾乎不可能創建通用的方法來獲取控件內容,獲取Winforms控件的內容請參閱http://mwinapi.sourceforge.net/,我會更新我的答案 –

6

我建議你使用涼爽,但鮮爲人知的UI Automation API這項工作。

爲此,首先要測試的是啓動關聯的UISpy工具。它將在屏幕上顯示所有可訪問窗口的樹狀圖。它還能夠執行一些操作,如按菜單,選擇項目等。這就是所謂的UI Automation Control Patterns,它提供了一種獨立於控件類型或控件外觀的分類和公開控件功能的方法。因此,如果您可以使用UI Spy自動執行此應用程序,那麼您也可以使用.NET代碼完成同樣的事情(UISpy本身只是使用底層API)。

以下是有關UI自動化編程一個有趣的教程文章:The Microsoft UI Automation Library

0

有沒有其他辦法,而不是注入要檢查應用程序。這是UISpy實際運行的方式。這也是爲什麼UISpy應該使用管理憑證運行的原因。