tags:

views:

213

answers:

1

I am able to remove the window icon from WPF window using WinApi's, however I get the icon again in the application window when I run just the executable of the WPF project.

How do I remove the icon?

+4  A: 

From WPFTutorial:

How to remove the icon of a WPF window

alt text

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);
        }
    }

A helper class used to remove the icon.

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);
        }

    }
LnDCobra
the same code i have used it does not remove the icon from the window when i run the project just using the exe....where m not using the visual studio to run the project
shraddha
@LnDCobra: Good answer, +1. Note that you could easily implement this as an attached property, which would enable removing the icon declaratively, in XAML
Thomas Levesque
itz getting removedmy problem is its attaching all together new icon for exe file
shraddha
the statndard icon for exe
shraddha
this helped me alot...n i just wanted to share some info about wpf window icons...http://msdn.microsoft.com/en-us/library/system.windows.window.icon.aspx
shraddha