这里有几个问题。首先,当 ShowInTaskbar 属性设置为 false 时,会创建一个不可见的窗口并将其指定为当前窗口的父级。在窗口之间切换时会显示此不可见窗口的图标。
您可以使用 Interop 捕获该窗口并设置它的图标,如下所示:
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);
您需要考虑的其他问题是:
- 找出当 ShowInTaskbar 属性在运行时发生变化时会发生什么;
- 从您的窗口而不是从文件中提取图标;