我正在创建一个需要 UI 但不显示在任务栏中的应用程序,我确切地知道如何在 winforms 和 wpf 中执行此操作,但窗口对象不包含我可以更改的
ShowInTaskBar
变量。
我已经尝试过 NotifyIcon 但这并没有将其从任务栏隐藏,我也找不到任何关于这是否可能的文档。
我只在花费超过 5 小时的时间才创建堆栈溢出问题,因此我们将不胜感激。
Nick给我看了这篇文章https://stackoverflow.com/a/551847/14724147
只需将窗口句柄添加到 win32 api,它将在 alt+tab 和任务栏中隐藏它
public MainWindow()
{
InitializeComponent();
IntPtr hWnd = WindowNative.GetWindowHandle(this);
int exStyle = (int)GetWindowLong(hWnd, (int)GetWindowLongFields.GWL_EXSTYLE);
exStyle |= (int)ExtendedWindowStyles.WS_EX_TOOLWINDOW;
SetWindowLong(hWnd, (int)GetWindowLongFields.GWL_EXSTYLE, (IntPtr)exStyle);
}
#region Window styles
[Flags]
public enum ExtendedWindowStyles
{
// ...
WS_EX_TOOLWINDOW = 0x00000080,
// ...
}
public enum GetWindowLongFields
{
// ...
GWL_EXSTYLE = (-20),
// ...
}
[DllImport("user32.dll")]
public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
public static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
{
int error = 0;
IntPtr result = IntPtr.Zero;
// Win32 SetWindowLong doesn't clear error on success
SetLastError(0);
if (IntPtr.Size == 4)
{
// use SetWindowLong
Int32 tempResult = IntSetWindowLong(hWnd, nIndex, IntPtrToInt32(dwNewLong));
error = Marshal.GetLastWin32Error();
result = new IntPtr(tempResult);
}
else
{
// use SetWindowLongPtr
result = IntSetWindowLongPtr(hWnd, nIndex, dwNewLong);
error = Marshal.GetLastWin32Error();
}
if ((result == IntPtr.Zero) && (error != 0))
{
throw new System.ComponentModel.Win32Exception(error);
}
return result;
}
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", SetLastError = true)]
private static extern IntPtr IntSetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetWindowLong", SetLastError = true)]
private static extern Int32 IntSetWindowLong(IntPtr hWnd, int nIndex, Int32 dwNewLong);
private static int IntPtrToInt32(IntPtr intPtr)
{
return unchecked((int)intPtr.ToInt64());
}
[DllImport("kernel32.dll", EntryPoint = "SetLastError")]
public static extern void SetLastError(int dwErrorCode);
#endregion
我是 H.NotifyIcon 的作者,最近我添加了 Window 类型的扩展 - ShowInTaskbar 和 HideInTaskbar 您可以使用它们来实现所需的行为。
我认为你只需要在 GitHub 上的问题中写下它即可
现在看来这很容易。只需在代码隐藏中输入窗口的构造函数即可:
public partial class YourWindow
{
public YourWindow()
{
InitializeComponent();
this.AppWindow.IsShownInSwitchers = false;
}
}