Removing Icon from a WPF window

前端 未结 6 1777
执念已碎
执念已碎 2020-12-01 11:17

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

相关标签:
6条回答
  • 2020-12-01 11:19

    I have modified the sample from 'LnDCobra' so it can be used as an attached property (as 'Thomas' suggested:

    <Window 
            ...
            xmlns:i="clr-namespace:namespace-to-WindowEx"
            i:WindowEx.ShowIcon = "false"
            ...
    >
    

    Implementation of WindowEx:

    public class WindowEx
      {
        private const int GwlExstyle = -20;
        private const int SwpFramechanged = 0x0020;
        private const int SwpNomove = 0x0002;
        private const int SwpNosize = 0x0001;
        private const int SwpNozorder = 0x0004;
        private const int WsExDlgmodalframe = 0x0001;
    
        public static readonly DependencyProperty ShowIconProperty =
          DependencyProperty.RegisterAttached(
            "ShowIcon",
            typeof (bool),
            typeof (WindowEx),
            new FrameworkPropertyMetadata(true, new PropertyChangedCallback((d, e) => RemoveIcon((Window) d))));
    
    
        public static Boolean GetShowIcon(UIElement element)
        {
          return (Boolean) element.GetValue(ShowIconProperty);
        }
    
        public static void RemoveIcon(Window window)
        {
          window.SourceInitialized += delegate {
            // Get this window's handle
            var hwnd = new WindowInteropHelper(window).Handle;
    
            // Change the extended window style to not show a window icon
            int extendedStyle = GetWindowLong(hwnd, GwlExstyle);
            SetWindowLong(hwnd, GwlExstyle, extendedStyle | WsExDlgmodalframe);
    
            // Update the window's non-client area to reflect the changes
            SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SwpNomove |
              SwpNosize | SwpNozorder | SwpFramechanged);
          };
        }
    
        public static void SetShowIcon(UIElement element, Boolean value)
        {
          element.SetValue(ShowIconProperty, value);
        }
    
        [DllImport("user32.dll")]
        private static extern int GetWindowLong(IntPtr hwnd, int index);
    
        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
          IntPtr wParam, IntPtr lParam);
    
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
    
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
          int x, int y, int width, int height, uint flags);
      }
    }
    
    0 讨论(0)
  • 2020-12-01 11:21

    From WPFTutorial:

    How to remove the icon of a WPF window

    alt text

    Unfortunately 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);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 11:31

    I just use very small transparent image as an icon (1x1 px) for WPF window.

    0 讨论(0)
  • 2020-12-01 11:34

    Here a simple and pure XAML solution:

    <Window x:Class="...">
    
        <Window.Icon>
            <DrawingImage />
        </Window.Icon>
    
        ...
    
    </Window>
    
    0 讨论(0)
  • 2020-12-01 11:35

    Create a transparent 1 by 1 icon and replace it with a standard one

    Icon = BitmapSource.Create(1, 1, 0, 0, PixelFormats.Bgra32, null, new byte[4], 4);
    
    0 讨论(0)
  • 2020-12-01 11:44

    Just add this WindowStyle="ToolWindow" to your window properties.

    0 讨论(0)
提交回复
热议问题