C#WPF - 应用程序图标+ ShowInTaskbar =假应用程序、图标、WPF、ShowInTaskbar

2023-09-03 16:19:09 作者:草莓软糖

我创建了一个自定义的分层WPF窗口具有以下属性:

I've created a custom layered WPF window with the following properties:

AllowsTransparency = TRUE ShowInTaskbar =假 背景=透明 在最顶层=真 图标=Icon.ico

我添加Icon.ico在项目属性 - >应用程序选项卡

I've added Icon.ico under "Project Properties"->"Application" tab.

该图标显示为,如果ShowInTaskBar是假的,但正确显示,如果ShowInTaskbar是真实的。默认的WPF窗口图标

The icon displays as the default WPF window icon if ShowInTaskBar is false, but displays correctly if ShowInTaskbar is true.

我们想要的图标来正确显示中的Alt + Tab键菜单。我们怎样才能做到这一点,有ShowInTaskbar =假?

We want the icon to show up correctly in the Alt+Tab menu. How can we achieve this and have ShowInTaskbar = False?

推荐答案

这里有几个问题。首先,当ShowInTaskBar属性设置为false,不可见的窗口被创建和分配为当前窗口的父。窗口之间切换时,会显示此不可见的窗口的图标。

There are several problems here. First of all, when ShowInTaskbar property is set to false, an invisible window gets created and assigned as a parent of current window. This invisible window's icon is displayed when switching between windows.

您可以捕获该窗口与互操作,并设置它的图标是这样的:

You can catch that window with Interop and set it's icon like this:

private void Window_Loaded(object sender, RoutedEventArgs e) {
    SetParentIcon();
}

private void SetParentIcon() {
    WindowInteropHelper ih = new WindowInteropHelper(this);
    if(this.Owner == null && ih.Owner != IntPtr.Zero) { //We've found the invisible window
        System.Drawing.Icon icon = new System.Drawing.Icon("ApplicationIcon.ico");
        SendMessage(ih.Owner, 0x80 /*WM_SETICON*/, (IntPtr)1 /*ICON_LARGE*/, icon.Handle); //Change invisible window's icon
    }
}

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

其他的问题,你想想会是:

The other problems for you to think about would be:

找到发生了什么,在运行时,ShowInTaskBar属性的变化; 从你的窗口,而不是从文件中提取图标;