2011-03-29 68 views
2

通过使用此代码,我可以得到活动窗口的标题..如何获取活动窗口的类名?

[DllImport("user32.dll")] 
static extern IntPtr GetForegroundWindow(); 

[DllImport("user32.dll")] 
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); 

private string GetActiveWindowTitle() 
{ 
    const int nChars = 256; 
    IntPtr handle = IntPtr.Zero; 
    StringBuilder Buff = new StringBuilder(nChars); 
    handle = GetForegroundWindow(); 

    if (GetWindowText(handle, Buff, nChars) > 0) 
    { 
     return Buff.ToString(); 
    } 
    return null; 

但我应该怎么做才能获得活动窗口的类名?

+0

User32的[GetClassNameA](http://www.pinvoke.net/default.aspx/user32.getclassname)调用? - *另请参阅[This support.microsoft Doc](http://support.microsoft.com/kb/112649)* – 2011-03-29 17:06:01

+0

您是指以Win32术语表示的窗口类,还是您的意思是C#类的名称在窗户后面? – dthorpe 2011-03-29 17:22:34

回答

4

只需pinvoke GetClassName()。这会返回一个窗口的类名,它不会与C#类有关。获取另一个进程中的窗口的C#类名是不可能的。如果这是Winforms应用程序,请查看Managed Spy++ tool以了解可能的黑客行为。