2011-12-30 203 views
2

我試過它與findwindowprocess但它沒有工作,我怎麼能找到一個特定的按鈕?如何從另一個程序讀取另一個窗口

例如,我有按鈕類AfxWnd90uinstance 21。我想檢查這個按鈕是否可見。我用這段代碼試過了,但是我找不到按鈕。我認爲我在這個實例中犯了一個錯誤。

之間我沒有使用findwindow在這裏,因爲我嘗試了一下。

//////IMPORTANT///////////// 
System.Diagnostics.Process[] move = System.Diagnostics.Process.GetProcessesByName("PartyGaming"); 
ArrayList save = new ArrayList(); 
RECT rct = new RECT(); 
listBox1.Items.Add(move.Length); 
List<System.Diagnostics.Process> process = new List<System.Diagnostics.Process>(); 

// use only the process with the button AfxWnd90u21 
for (int i = 0; i < move.Length;++i) 
{ 
    IntPtr hCheck = FindWindowEx(move[i].MainWindowHandle, IntPtr.Zero, "AfxWnd90u21", null); 
    //if button is visible 
    if (hCheck != IntPtr.Zero) 
     process.Add(move[i]); 

    //////IMPORTANT///////////// 
} 
+3

+1偏移隨機-1。 – 2011-12-30 23:39:00

+2

這聽起來像你需要通常所說的'窗戶間諜'。這是一個可能有所幫助的代碼項目項目:http://www.codeproject.com/KB/dotnet/wfspy.aspx – 2011-12-30 23:51:07

+1

我對樣本進行了一些編輯以適應多個窗口。最後我還加了一個排序。 – sarme 2011-12-31 23:09:32

回答

7

我相信FindWindow和SendMessage Windows API函數的結合將會給你想要的。棘手的部分將發現窗口類的名稱,但像WinSpy ++可以幫助你。

下面是如何使用API​​的示例。打開Notepad.exe幾次,鍵入一些文本,然後運行此示例。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<WinText> windows = new List<WinText>(); 

      //find the "first" window 
      IntPtr hWnd = FindWindow("notepad", null); 

      while (hWnd != IntPtr.Zero) 
      { 
       //find the control window that has the text 
       IntPtr hEdit = FindWindowEx(hWnd, IntPtr.Zero, "edit", null); 

       //initialize the buffer. using a StringBuilder here 
       System.Text.StringBuilder sb = new System.Text.StringBuilder(255); // or length from call with GETTEXTLENGTH 

       //get the text from the child control 
       int RetVal = SendMessage(hEdit, WM_GETTEXT, sb.Capacity, sb); 

       windows.Add(new WinText() { hWnd = hWnd, Text = sb.ToString() }); 

       //find the next window 
       hWnd = FindWindowEx(IntPtr.Zero, hWnd, "notepad", null); 
      } 

      //do something clever 
      windows.OrderBy(x => x.Text).ToList().ForEach(y => Console.Write("{0} = {1}\n", y.hWnd, y.Text)); 

      Console.Write("\n\nFound {0} window(s).", windows.Count); 
      Console.ReadKey(); 
     } 

     private struct WinText 
     { 
      public IntPtr hWnd; 
      public string Text; 
     } 

     const int WM_GETTEXT = 0x0D; 
     const int WM_GETTEXTLENGTH = 0x0E; 

     [DllImport("user32.dll", SetLastError = true)] 
     public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 

     [DllImport("user32.dll", SetLastError = true)] 
     public static extern int SendMessage(IntPtr hWnd, int msg, int Param, System.Text.StringBuilder text); 

     [DllImport("user32.dll", SetLastError = true)] 
     public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 

    } 
} 
+0

哇..好吧,這太棒了。我從來沒有想過迭代,直到找到一個空指針。非常感謝你 – 2011-12-31 23:51:44

+1

沒問題。樂意效勞。不要忘記接受答案。 – sarme 2012-01-01 18:05:01

+0

我有最後一個問題。我編輯過我的帖子。 – 2012-01-01 21:42:31

相關問題