C # WPF - Application Icon + ShowInTaskbar = False

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

  • AllowsTransparency = True
  • ShowInTaskbar = False
  • Background = Transparent
  • Topmost = true
  • Icon = "Icon.ico"

I added Icon.ico in the section "Project Properties" → "Application".

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

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

+7
c # favicon wpf icons
source share
1 answer

There are several issues here. First of all, if the ShowInTaskbar property is set to false, an invisible window is created and set as the parent of the current window. This invisible window icon is displayed when switching between windows.

You can catch this window with Interop and set it 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 icon } } [DllImport("user32.dll")] private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); 

Other issues you thought about would be:

  • Find out what happens when the ShowInTaskbar property changes at runtime;
  • Remove the icon from your window, not from the file;
+3
source share

All Articles