2011-03-07 81 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

相关问题