Dav*_*ner 5 .net winapi taskbar
我有一些逻辑上相关的独立进程(但是所有进程都是单独启动的-没有通用的“父”进程)。
是否可以使它们在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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
为了使其正常工作,必须将可执行文件的“输出类型”设置为“ Windows应用程序”,并将“启动对象”配置为“ ConsoleApplication1.Program”(对于上面的代码示例)。