【问题标题】:Grouping separate processes in the Windows Taskbar在 Windows 任务栏中对单独的进程进行分组
【发布时间】:2012-12-17 23:55:25
【问题描述】:

我有许多逻辑相关的独立进程(但所有进程都是单独启动的 - 没有共同的“父”进程)。

是否可以让它们在 Windows 任务栏中显示为一组?

工作样本

这是一些受 Remy 回答启发的工作代码

using System;
using System.Runtime.InteropServices;
using System.Security;

namespace ConsoleApplication1
{
    [SuppressUnmanagedCodeSecurity]
    internal static class SafeNativeMethods
    {
        [DllImport("shell32.dll")]
        public static extern int SetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType.LPWStr)] string AppID);

        [DllImport("kernel32.dll")]
        public static extern bool AllocConsole();

        [DllImport("kernel32.dll")]
        public static extern bool FreeConsole();
    }

    internal class Program
    {
        public static int SetApplicationUserModelId(string appId)
        {
            // check for Windows 7
            Version version = Environment.OSVersion.Version;
            if ((version.Major > 6) || (version.Major == 6 && version.Minor >= 1))
                return SafeNativeMethods.SetCurrentProcessExplicitAppUserModelID(appId);
            return -1;
        }

        [STAThread]
        public static void Main(string[] args)
        {
            int result = SetApplicationUserModelId("Gardiner.Sample1");

            SafeNativeMethods.AllocConsole();

            // Now we have a console, we can write to it
            Console.Title = "Sample 1";

            Console.WriteLine("Sample 1 {0}", result);
            Console.ReadLine();
            SafeNativeMethods.FreeConsole();
        }
    }
}

要使其工作,必须将可执行文件设置为将“输出类型”设置为“Windows 应用程序”,并将“启动对象”配置为“ConsoleApplication1.Program”(对于上面的代码示例)。

【问题讨论】:

    标签: .net winapi taskbar


    【解决方案1】:

    是的,但仅限于 Windows 7 及更高版本。如果多个进程和窗口分配有相同的Application User Model ID,则它们会在任务栏上组合在一起。

    【讨论】:

    • 我通过创建两个 C# 控制台应用程序并在 Main 方法的开头调用 SetCurrentProcessExplicitAppUserModelID 进行了尝试(具有相同的应用程序用户模型 ID)。它似乎不起作用。我在任务栏中得到两个单独的图标
    • 我在我的应用程序 C++ 应用程序中使用 SetCurrentProcessExplicitAppUserModelID(),它对我来说很好用。也许你说得太晚了?它会为您返回什么 HRESULT 值?
    • 可能为时已晚,但如果它是 .NET 控制台应用程序中 Main 方法的第一行,我不确定我该如何更早地做到这一点
    • 它可能是您代码的第一行,但它可能不是 .NET 代码的第一行。必须在调用任何其他与 UI 相关的函数之前调用它。如果您在程序启动期间不能足够早地调用它,那么您可能需要直接通过SHGetPropertyStoreForWindow() 使用IPropertyStore 接口,而不是简单地将ID 添加到您用于运行应用程序的快捷方式中。
    • 看起来我的问题是使用控制台应用程序。 UI 总是首先创建的。切换到 Windows 应用程序(然后手动创建控制台)有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多