gpt4 book ai didi

c# - 在没有任务栏按钮的情况下从 C++ 启动 C# 应用程序

转载 作者:行者123 更新时间:2023-11-28 08:10:37 24 4
gpt4 key购买 nike

我正在寻找一种方法来从 C++ 应用程序启动 C# 应用程序,而不显示 C# 应用程序的任务栏按钮,直到我选择显示它为止。

我有一个可以隐藏和显示任务栏按钮的类,但是在启动 C# 应用程序时任务栏按钮显示得非常短暂。

有没有办法在操作系统不创建任务栏按钮的情况下启动 C# 应用程序,同时仍然允许我稍后显示任务栏按钮?

这是我目前用来隐藏和显示任务栏按钮的代码。

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
static void Main()
{
var tb = new Taskbar();
tb.DeleteTab();
bool hidden = true;

while (true)
{
Console.WriteLine("Press any key to toggle taskbar button");
Console.ReadKey();
Console.Clear();

if (hidden)
tb.AddTab();
else
tb.DeleteTab();

hidden = !hidden;
}
}
}

class Taskbar
{
public void AddTab()
{
GetTaskbarList().AddTab(GetMainWindowHandle());
}

public void DeleteTab()
{
GetTaskbarList().DeleteTab(GetMainWindowHandle());
}

ITaskbarList GetTaskbarList()
{
var taskbarList = (ITaskbarList)new CoTaskbarList();
taskbarList.HrInit();
return taskbarList;
}

IntPtr GetMainWindowHandle()
{
return Process.GetCurrentProcess().MainWindowHandle;
}

[ComImport]
[Guid("56fdf344-fd6d-11d0-958a-006097c9a090")]
class CoTaskbarList
{
}

[ComImport,
Guid("56fdf342-fd6d-11d0-958a-006097c9a090"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ITaskbarList
{
/// <summary>
/// Initializes the taskbar list object. This method must be called before any other ITaskbarList methods can be called.
/// </summary>
void HrInit();

/// <summary>
/// Adds an item to the taskbar.
/// </summary>
/// <param name="hWnd">A handle to the window to be added to the taskbar.</param>
void AddTab([In] IntPtr hWnd);

/// <summary>
/// Deletes an item from the taskbar.
/// </summary>
/// <param name="hWnd">A handle to the window to be deleted from the taskbar.</param>
void DeleteTab([In] IntPtr hWnd);

/// <summary>
/// Activates an item on the taskbar. The window is not actually activated; the window's item on the taskbar is merely displayed as active.
/// </summary>
/// <param name="hWnd">A handle to the window on the taskbar to be displayed as active.</param>
void ActivateTab([In] IntPtr hWnd);

/// <summary>
/// Marks a taskbar item as active but does not visually activate it.
/// </summary>
/// <param name="hWnd">A handle to the window to be marked as active.</param>
void SetActiveAlt([In] IntPtr hWnd);
}
}

最佳答案

设置 ShowInTaskbar(WinFormsWPF)属性。

最初将其设置为 false,应用程序将不会出现。

当您希望应用程序出现在任务栏中时,将其设置为 true

关于c# - 在没有任务栏按钮的情况下从 C++ 启动 C# 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9193065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com