从 WPF 窗口中删除图标 [英] Removing Icon from a WPF window

查看:38
本文介绍了从 WPF 窗口中删除图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

如何删除图标?

推荐答案

我修改了来自 'LnDCobra' 的示例,因此它可以用作附加属性(如 'Thomas' 建议的那样:

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"
        ...
>

WindowEx 的实现:

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

这篇关于从 WPF 窗口中删除图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆