简体   繁体   English

C#列出具有GUI的正在运行的应用程序

[英]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 EnumWindowsGetWindowThreadProcessId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取当前正在运行的应用程序列表,而不是进程 c# - Getting a list of current APPLICATIONS running, not processes c# 为 2 个不同的外部应用程序创建 2 套 .Net C# 插件,除了命名空间外,它们具有相同的 API - Creating 2 sets of .Net C# plugins for 2 diffferent external applications which have the same API except namespace Azure Functions - C# - 运行外部应用程序? - Azure Functions - C# - Running external applications? 在C#中运行应用程序之间共享变量 - sharing variables between running applications in C# 创建不依赖框架的C#应用​​程序? - Create C# applications which are not framework dependant? 使用python自动化c#gui应用程序控件的更好方法 - better way to automate c# gui applications controls using python 我对 C# 中的多平台 GUI 应用程序有哪些选择? - what are my options for multiplatform GUI applications in C#? 如何在Java Swing GUI应用程序中访问和使用C#dll - How to access and consume C# dlls in Java swings GUI applications 从C#运行Matlab GUI - Running Matlab GUI from C# 检查已订阅为Windows剪贴板侦听器的应用程序列表 - Check the list of applications which have subscribed as windows clipboard listeners
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM