是否可以在标题栏中显示没有图标的 wpf 窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1553715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to display a wpf window without an icon in the title bar?
提问by Michael Arnell
As we all know, if the icon for a wpf window is undefined then the default icon is displayed. I want to display a window without any icon in the title bar. I realise that I could use a blank image, however this would cause the text in the title bar to be offset to the right.
众所周知,如果 wpf 窗口的图标未定义,则显示默认图标。我想在标题栏中显示一个没有任何图标的窗口。我意识到我可以使用空白图像,但这会导致标题栏中的文本向右偏移。
Does anyone know of a way to completely remove the icon?
有谁知道完全删除图标的方法?
(I tried searching for a similar question but couldn't find anything.)
(我尝试搜索类似的问题,但找不到任何内容。)
采纳答案by Nir
Simple, add this code to your window:
简单,将此代码添加到您的窗口:
[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);
}
回答by Andrej
While not exactly a proper solution, you could try one of the following things:
虽然不是完全正确的解决方案,但您可以尝试以下方法之一:
Setting the WindowStyle-Property to ToolWindow will make the Icon disappear, but the title bar (obviously) will be smaller.
Write a ControlTemplate for the whole Window. Depending on if the Window has to look like a "real" Window, there'd be much effort into trying to recreate the default Style in the Template.
将 WindowStyle-Property 设置为 ToolWindow 将使图标消失,但标题栏(显然)会更小。
为整个窗口编写一个 ControlTemplate。根据窗口是否必须看起来像“真实”窗口,尝试在模板中重新创建默认样式需要付出很多努力。
回答by Brett Ryan
I know this is answered, however Dan Rigsby's bloghas an article that shows how to do this without hiding the minimize/maximize boxes.
我知道这是回答,但是Dan Rigsby 的博客有一篇文章展示了如何在不隐藏最小化/最大化框的情况下执行此操作。
I found this was frustrating me as I was using the articles (hereand herebut it kept hiding all the buttons when the sysmenu was hidden, to help I created this helper which as shown above call in OnSourceInitialized
.
我发现这让我很沮丧,因为我在使用文章(这里和这里但是当系统菜单被隐藏时它一直隐藏所有按钮,以帮助我创建了这个助手,如上所示调用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));
}
}
回答by Joey
No, this doesn't seem to be possible. Quoting from the documentation of the Icon property(emphasis mine):
不,这似乎不可能。引用Icon 属性的文档(强调我的):
A WPF window always displays an icon.When one is not provided by setting Icon, WPF chooses an icon to display based on the following rules:
- Use the assembly icon, if specified.
- If the assembly icon is not specified, use the default Microsoft Windows icon.
If you use Icon to specify a custom window icon, you can restore the default application icon by setting Icon to
null
.
WPF 窗口始终显示一个图标。当没有通过设置 Icon 提供时,WPF 根据以下规则选择要显示的图标:
- 如果指定,请使用程序集图标。
- 如果未指定程序集图标,请使用默认的 Microsoft Windows 图标。
如果使用 Icon 指定自定义窗口图标,则可以通过将 Icon 设置为 来恢复默认的应用程序图标
null
。
So, apparently a completely transparent icon seems to be your best bet here. Or perhaps hack around all this by using Windows API functions to set the appropriate style on the window. But this mayinterfere with WPF's window management.
所以,显然一个完全透明的图标似乎是你最好的选择。或者也许通过使用 Windows API 函数在窗口上设置适当的样式来解决所有这些问题。但这可能会干扰 WPF 的窗口管理。
回答by mehdi
You can use an empty png image and convert it to icon and set it as icon for your window!!!
您可以使用空的 png 图像并将其转换为图标并将其设置为窗口的图标!!!
回答by Neil Barnwell
My first suggestion would be don't do it. In WinForms you can use types of formborderstyles to create a dialog box that has no icon, but only because that is a Windows standard. Only forms with those specific border types should have no icon; it's what users expect.
我的第一个建议是不要这样做。在 WinForms 中,您可以使用 formborderstyles 类型来创建没有图标的对话框,但这仅仅是因为这是 Windows 标准。只有具有这些特定边框类型的表单才不应有图标;这是用户所期望的。