Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  


How to remove the icon of a WPF window

Unfortumately WPF does not provide any function to remove the icon of a window. One solution could be setting the icon to a transparent icon. But this way the extra space between the window border and title remains.

The better approach is to use a function provided by the Win32 API to remove the icon.

 
 public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
 
        protected override void OnSourceInitialized(EventArgs e)
        {
            IconHelper.RemoveIcon(this);
        }
    }
 
 
 
public static class IconHelper
    {
        [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 static void RemoveIcon(Window window)
        {
            // Get this window's handle
            IntPtr hwnd = new WindowInteropHelper(window).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);
        }
 
    }
 
 




Last modified: 0000-00-00 00:00:00
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Ketan
Commented on 23.March 2009
This was a good article. I hat going into the Win32 stuff so having a class like this means I can add to any future projects that require this functionality.
Thanks
konstantin
Commented on 27.May 2009
Does it work in Windows versions less then Vista (XP, Server 2003)?
Steve
Commented on 28.May 2009
I was trying to do this on XP Professional and it did not appear to work. Maybe I am missing something (I do that on occasion).

I still get the default windows icon on my window but no longer get the context menu (that shows Restore, Move, Size, Minimize, etc). I basically copied the code from here and used it in my application. Nothing was changed and I double checked that I did not get a copy/paste error.
Alex
Commented on 8.June 2009
does not work in Xp.
Kevinclcn
Commented on 25.July 2009
SendMessage(hwnd, WM_SETICON, IntPtr.Zero, IntPtr.Zero) can remove the Icon from XP
shradz
Commented on 26.February 2010
itz awesome....but one problem i am facing is ,when i run the exe of project the default icon comes at that place...can u help me for this
shradz
Commented on 26.February 2010
itz awesome....but one problem i am facing is ,when i run the exe of project the default icon comes at that place...can u help me for this
RomanK
Commented on 27.February 2010
Thank you for this great trick!
Do you by chance also know how to remove the window title?
Caian
Commented on 5.March 2010
Excellent approach, exactly what i was looking for!
SomeOne
Commented on 6.April 2010
Another way to do it:
<Window x:Class="Transparent.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Icon="transparent.png"
Title="Window1" SizeToContent="WidthAndHeight">
Gorkster
Commented on 27.September 2010
The SendMessage solution does not remove the icon in VisualStudio 2010 in XP.
Gorkster
Commented on 27.September 2010
The "transparent.png" approach does not remove the space reserved for the icon even thought there is no icon showing.
Max
Commented on 6.October 2010
a trick of lazy:

WindowStyle="ToolWindow"
midspace
Commented on 23.February 2011
SetWindowLong by itself doesn't work on Vista x64 (VS2010) on remote desktop.
Requires both SetWindowLong and SendMessage.
Jesse Chisholm
Commented on 5.August 2011
re: WindowStyle=&quot;ToolWindow&quot;
Yes, that will remove the icon, but it also changes the style of the buttons in the caption bar.

Name
E-Mail (optional)
Comment