2010-11-30 49 views

回答

1

您可以從WindowFromPointChildWindowFromPointEx獲取窗口句柄,然後用GetWindowInfo的。我不認爲有識別從Word一個按鈕一個平凡的方式查詢窗口句柄,雖然。

+0

您建議的方式適用於我可以在屏幕上看到的任何應用程序嗎? – naokikun 2010-12-01 15:36:49

0

查看AutomationElement.FromPoint()這是UI自動化的一部分 - 這套API經常被自動化測試和輔助功能應用程序使用,這些應用程序想要獲取有關其他進程的UI的信息。對於支持它的應用程序(Windows中的大部分UI和大多數MS應用程序),您可以獲取有關UI元素的信息,而不僅僅是外部窗口。此示例應用程序輸出光標下項目的名稱和類型(例如「按鈕」)。

它不支持任何地方,可能無法在許多非MS應用程序中工作(雖然受Firefox支持);但至少可以讓你獲得比WindowFromPoint更好的結果。

// Compile using: csc ItemAtPoint.cs /r:UIAutomationClient.dll /r:WindowsBase.dll 

using System; 
using System.Windows.Automation; 
using System.Windows.Forms; 

class ItemAtPoint 
{ 
    public static void Main() 
    { 
     Console.WriteLine("Place pointer over item and hit return..."); 
     Console.ReadLine(); 

     // Get the AutomationElement that represents the window handle... 
     System.Windows.Point point = new System.Windows.Point(Cursor.Position.X, Cursor.Position.Y); 
     AutomationElement el = AutomationElement.FromPoint(point); 

     // Print out the type of the item and its name 
     Console.WriteLine("item is a \"{0}\" with name \"{1}\"", el.Current.LocalizedControlType, el.Current.Name); 
    } 
} 
相關問題