[英]C# List running applications which have a GUI
How could I list all running applications which have a GUI in C#? 如何列出C#中具有GUI的所有正在运行的应用程序? I am trying to make some sort of a taskbar, so I have to know what processes are running.
我正在尝试制作某种任务栏,所以我必须知道正在运行什么进程。 I can't list all processes of course, because that would be a total disaster.
我当然不能列出所有过程,因为那将是一场彻底的灾难。 My idea is to only list processes with a gui, because that seems like what's happening in the "stock" taskbar.
我的想法是仅使用gui列出进程,因为这似乎在“ stock”任务栏中发生了什么。
This code will fill a dropdown and set get it on foreground by selection, you can do it like this 这段代码将填充一个下拉列表,并通过选择将其设置为在前台显示,您可以像这样进行操作
[DllImport("User32.dll")]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private void ListTask()
{
Process[] processes = Process.GetProcesses();
foreach (Process process in Process.GetProcesses().
Where(p => !string.IsNullOrEmpty(p.MainWindowTitle)).ToList())
listBox1.Items.Add(process.ProcessName);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Process p = new Process();
string name = listBox1.SelectedItem.ToString();
p = Process.GetProcessesByName(name)[0];
const uint SWP_SHOWWINDOW = 0x0001;
ShowWindow(p.MainWindowHandle, SWP_SHOWWINDOW);
SetForegroundWindow(p.MainWindowHandle);
p.Dispose();
}
You can list all processes that have a main window: 您可以列出具有主窗口的所有进程:
static IEnumerable<Process> WindowProcesses()
{
foreach(var proc in Process.GetProcesses())
{
if(proc.MainWindowHandle != IntPtr.Zero)
{
yield return proc;
}
}
}
This will enumerate all processes that have a main window, but may not necessarily list all processes that have any visible windows (eg services). 这将枚举具有主窗口的所有进程,但不一定列出具有任何可见窗口的所有进程(例如服务)。 For that, you need to P/Invoke EnumWindows and GetWindowThreadProcessId .
为此,您需要P / Invoke EnumWindows和GetWindowThreadProcessId 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.