C# WPF - Application Icon + ShowInTaskbar = False

风格不统一 提交于 2019-12-05 04:23:20

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:

  1. Find out what happens when ShowInTaskbar property changes at runtime;
  2. Extract an icon from your window rather than from file;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!