从WPF窗口中删除图标

时间:2010-02-26 11:33:59

标签: wpf user-interface

我可以使用WinApi从WPF窗口中删除窗口图标,但是当我只运行WPF项目的可执行文件时,我在应用程序窗口中再次显示该图标。

如何删除图标?

6 个答案:

答案 0 :(得分:21)

来自WPFTutorial

如何删除WPF窗口的图标

alt text

不幸的是,WPF没有提供删除窗口图标的任何功能。一种解决方案是将图标设置为透明图标。但是这样,窗口边框和标题之间的额外空间仍然存在。

更好的方法是使用Win32 API提供的功能删除图标。

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

答案 1 :(得分:12)

我修改了'LnDCobra'中的样本,因此它可以用作附加属性(如'Thomas'所示:

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

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

答案 2 :(得分:5)

我只是使用非常小的透明图像作为WPF窗口的图标(1x1像素)。

答案 3 :(得分:4)

只需将此WindowStyle="ToolWindow"添加到您的窗口属性中。

答案 4 :(得分:2)

创建一个透明的1比1图标,并将其替换为标准图标

Icon = BitmapSource.Create(1, 1, 0, 0, PixelFormats.Bgra32, null, new byte[4], 4);

答案 5 :(得分:2)

这里有一个简单而又纯粹的XAML解决方案:

<Window x:Class="...">

    <Window.Icon>
        <DrawingImage />
    </Window.Icon>

    ...

</Window>