2011-03-07 25 views

回答

1

official documentation其中的窗口出現在任務欄中。

無論如何,像這樣的東西應該通過一般的想法。您現在可以自行排列詳細信息,以瞭解您的目標。

using System; 
using System.Runtime.InteropServices; 
using System.Text; 

public delegate bool CallBack(IntPtr hWnd, IntPtr lParam); 

public class EnumTopLevelWindows { 

    [DllImport("user32", SetLastError=true)] 
    private static extern int EnumWindows(CallBack x, IntPtr y); 

    [DllImport("user32.dll", SetLastError = true)] 
    static extern IntPtr GetParent(IntPtr hWnd); 

    [DllImport("user32.dll", EntryPoint = "GetWindowLong", SetLastError = true)] 
    private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex); 

    [DllImport("user32.dll", SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool IsWindowVisible(IntPtr hWnd); 

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 
    static extern int GetWindowTextLength(IntPtr hWnd); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); 

    public static string GetText(IntPtr hWnd) 
    { 
     // Allocate correct string length first 
     int length = GetWindowTextLength(hWnd); 
     StringBuilder sb = new StringBuilder(length + 1); 
     GetWindowText(hWnd, sb, sb.Capacity); 
     return sb.ToString(); 
    } 

    private const int GWL_STYLE = -16; 
    private const int WS_EX_APPWINDOW = 0x00040000; 

    public static void Main() 
    { 
     CallBack myCallBack = new CallBack(EnumTopLevelWindows.Report); 
     EnumWindows(myCallBack, IntPtr.Zero); 
    } 

    public static bool Report(IntPtr hWnd, IntPtr lParam) 
    { 
     if (GetParent(hWnd) == IntPtr.Zero) 
     { 
      //window is a non-owned top level window 
      if (IsWindowVisible(hWnd)) 
      { 
       //window is visible 
       int style = GetWindowLongPtr(hWnd, GWL_STYLE).ToInt32(); 
       if ((style & WS_EX_APPWINDOW) == WS_EX_APPWINDOW) 
       { 
        //window has WS_EX_APPWINDOW style 
        Console.WriteLine(GetText(hWnd)); 
       } 
      } 
     } 
     return true; 
    } 
} 
+0

很酷,我得看看你的代碼有點......正在研究enumtoplevelwindows ....那麼今天希望我會在所有事情發生之後再發布更新。 我實際上正在努力讓班級與所有凌亂的WMI一起工作。 它在我眼中只是沒有美麗。 :D謝謝,當我看透這個時,我會給你一個提醒。 – 2011-03-08 05:28:39

+0

謝謝,你的代碼讓我提前瞭解了這一點,但更多的是一個問題。 if(GetWindow(hWnd,GW_OWNER)== IntPtr.Zero)做什麼?有點難以理解它 – 2011-03-08 17:58:50

+0

嗯,是IntPtr.Zero代表桌面/所有者,該聲明意味着如果當前的應用程序所有者是桌面,然後繼續? – 2011-03-08 18:06:06

0

您可以使用管理System.Diagnostic.Processes類:

Process[] running = Process.GetProcesses(); 

foreach(Process p in running) 
    Console.WriteLine(p.ProcessName); 
+0

是的,我知道,它是運行的進程而不是應用程序的問題。最好的例子是打開任務管理器,查看應用程序和進程選項卡的區別。我只需要應用程序。 – 2011-03-07 22:44:27

+0

然後你可以使用EnumWindows API – 2011-03-08 08:55:40

相關問題