是否可以在标题栏中显示没有图标的wpf窗口?

Mic*_*ell 29 wpf xaml dialog titlebar application-icon

众所周知,如果未定义wpf窗口的图标,则会显示默认图标.我想在标题栏中显示一个没有任何图标的窗口.我意识到我可以使用空白图像,但这会导致标题栏中的文本偏移到右侧.

有谁知道完全删除图标的方法?

(我试图寻找类似的问题,但找不到任何东西.)

Nir*_*Nir 27

很简单,将此代码添加到您的窗口:

[DllImport("user32.dll")]
static extern uint GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);

private const int GWL_STYLE = -16;

private const uint WS_SYSMENU = 0x80000;

protected override void OnSourceInitialized(EventArgs e)
{
    IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
    SetWindowLong(hwnd, GWL_STYLE,
        GetWindowLong(hwnd, GWL_STYLE) & (0xFFFFFFFF ^ WS_SYSMENU));

    base.OnSourceInitialized(e);
}
Run Code Online (Sandbox Code Playgroud)

  • 这似乎摆脱了"关闭"按钮,这并不总是需要. (15认同)

And*_*rej 17

虽然不是一个恰当的解决方案,但您可以尝试以下方法之一:

  1. 将WindowStyle-Property设置为ToolWindow会使Icon消失,但标题栏(显然)会更小.

  2. 为整个Window编写一个ControlTemplate.根据Window是否必须看起来像"真实"窗口,尝试在模板中重新创建默认样式需要付出很多努力.


Joe*_*oey 10

不,这似乎不可能.引用Icon属性的文档(强调我的):

WPF窗口始终显示图标.如果未通过设置图标提供一个,WPF会根据以下规则选择要显示的图标:

  1. 如果指定,请使用装配图标.
  2. 如果未指定程序集图标,请使用默认的Microsoft Windows图标.

如果使用"图标"指定自定义窗口图标,则可以通过将"图标"设置为还原默认应用程序图标null.

所以,显然一个完全透明的图标似乎是你最好的选择.或者通过使用Windows API函数在窗口上设置适当的样式来解决所有这些问题.但这可能会干扰WPF的窗口管理.


Bre*_*yan 10

我知道这已得到解答,但是Dan Rigsby的博客上有一篇文章,展示了如何在不隐藏最小化/最大化框的情况下执行此操作.

我发现这让我感到很沮丧,因为我正在使用这些文章(这里这里,但它隐藏了系统菜单时隐藏了所有按钮,以帮助我创建这个帮助器,如上所示调用OnSourceInitialized.

public static class WpfWindowHelper {

    [DllImport("user32.dll")]
    public static extern int GetWindowLong(IntPtr hwnd, int index);
    [DllImport("user32.dll")]
    public static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
    [DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags);

    public const int GWL_EXSTYLE = -20;
    public const int WS_EX_DLGMODALFRAME = 0x0001;
    public const int SWP_NOSIZE = 0x0001;
    public const int SWP_NOMOVE = 0x0002;
    public const int SWP_NOZORDER = 0x0004;
    public const int SWP_FRAMECHANGED = 0x0020;
    public const int GWL_STYLE = -16;
    public const int WS_MAXIMIZEBOX = 0x00010000;
    public const int WS_MINIMIZEBOX = 0x00020000;
    public const int WS_SYSMENU = 0x00080000;

    public static void HideSysMenu(this Window w) {
        IntPtr hwnd = new WindowInteropHelper(w).Handle;
        int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
        SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);
        SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
    }

    public static void HideMinimizeBox(this Window w) {
        IntPtr hwnd = new WindowInteropHelper(w).Handle;
        SetWindowLong(hwnd, GWL_STYLE,
            GetWindowLong(hwnd, GWL_STYLE) & ~(WS_MINIMIZEBOX));
    }

    public static void HideMaximizeBox(this Window w) {
        IntPtr hwnd = new WindowInteropHelper(w).Handle;
        SetWindowLong(hwnd, GWL_STYLE,
            GetWindowLong(hwnd, GWL_STYLE) & ~(WS_MAXIMIZEBOX));
    }

    public static void HideMinimizeAndMaximizeBoxes(this Window w) {
        IntPtr hwnd = new WindowInteropHelper(w).Handle;
        SetWindowLong(hwnd, GWL_STYLE,
            GetWindowLong(hwnd, GWL_STYLE) & ~(WS_MAXIMIZEBOX | WS_MINIMIZEBOX));
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 对不起,它没有做任何事情.图标和关闭按钮仍然存在.根据微软自己的对话框窗口UI指南,我只希望图标消失. (2认同)

meh*_*hdi 7

您可以使用空的png图像并将其转换为图标并将其设置为窗口的图标!

在此输入图像描述