Remove Icon from WPF Window
Posted by Dan Rigsby on May 26th, 2008
In general, a window should always have an icon because it helps give context to the window. However, there are occasions when you may want a window with icon such as a custom tool window or some other dialog. In Windows Forms if you want a window without an icon, you could just simply set:
this.ShowIcon = false;
In WPF, there is no simple way to just remove the icon. If you try to just set the Icon to null, it will display the default windows icon. The only way to disable the icon is to use some interop code to remove the icon directly through the windows API:
using System; using System.Windows; using System.Windows.Interop; using System.Runtime.InteropServices; public partial class WindowWithoutIcon : Window { [DllImport("user32.dll")] static extern int GetWindowLong(IntPtr hwnd, int index); [DllImport("user32.dll")] static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle); [DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags); [DllImport("user32.dll")] static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam); const int GWL_EXSTYLE = -20; const int WS_EX_DLGMODALFRAME = 0x0001; const int SWP_NOSIZE = 0x0001; const int SWP_NOMOVE = 0x0002; const int SWP_NOZORDER = 0x0004; const int SWP_FRAMECHANGED = 0x0020; const uint WM_SETICON = 0x0080; public MainWindow() { InitializeComponent(); } protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); // Get this window's handle IntPtr hwnd = new WindowInteropHelper(this).Handle; // Change the extended window style to not show a window icon int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE); SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME); // Update the window's non-client area to reflect the changes SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); } }
Source derived from: http://blogs.msdn.com/wpfsdk/archive/2007/08/02/a-wpf-window-without-an-window-icon-the-thing-you-click-to-get-the-system-menu.aspx

















June 4th, 2008 at 6:21 am
works wonder …. and i had no idea how simple it could be.
Thanks a bunch
Daniyal
July 13th, 2008 at 11:01 am
I’ll have to take a note of this. I didn’t know it would take this much work to do this in WPF.
July 25th, 2008 at 8:15 am
Good trick.
But this only works as long as WPF uses Win32 as host window. If MS goes through with WPF becoming the Win32 replacement this won’t work anymore. But that’s far in the distant future …
Stefan
September 25th, 2008 at 9:16 pm
It doesn’t work on Windows XP SP3 and .NET Framework 3.5 SP1… Any ideas?
October 7th, 2008 at 8:21 am
I confirm what Luke said, doesn’t work with the mentioned configuration.
Guess it has something to do with the FW version…but actually i really don’t know.
It’s a shame thought that they didn’t port the ShowIcon property from Win32…
December 19th, 2008 at 3:32 am
Another way is to use an empty icon. This works best when you want a window with no icon and no title because, as the icon is there but only transparent, if you put a title it will not be aligned to the left
I know it’s an ugly way of doing things but it avoids DllImport.
May 13th, 2009 at 7:19 am
At first I found this didn’t work.
But then I removed some of the window property settings I had in my xaml.
For example, MaxWidth=400 and WindowStartupLocation=Center, etc.
Then it worked. The icon goes away.
I had to add these settings in my code, after removing the icon.
At at that point, setting WindowStartupLocation was too late… so I had to just reposition the window manually in code.
But in the end, I get the look I want.
Ion
July 25th, 2009 at 4:55 am
SendMessage(hwnd, WM_SETICON, IntPtr.Zero, IntPtr.Zero) can remove the Icon from XP