C# 从窗口隐藏最小化、最大化、关闭按钮并显示图标
我试图隐藏窗口顶部的最小化、最大化和关闭按钮,但仍然显示我的图标 我尝试了一些不同的方法,但无法让图标保持不变。这是我正在使用的代码:C# 从窗口隐藏最小化、最大化、关闭按钮并显示图标,c#,wpf,interop,interopservices,C#,Wpf,Interop,Interopservices,我试图隐藏窗口顶部的最小化、最大化和关闭按钮,但仍然显示我的图标 我尝试了一些不同的方法,但无法让图标保持不变。这是我正在使用的代码: private const int GWL_STYLE = -16; private const int WS_SYSMENU = 0x00080000; [DllImport("user32.dll")] private extern static int SetWindowLong(IntPtr hwnd, int index, int value); [
private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x00080000;
[DllImport("user32.dll")]
private extern static int SetWindowLong(IntPtr hwnd, int index, int value);
[DllImport("user32.dll")]
private extern static int GetWindowLong(IntPtr hwnd, int index);
public Window()
{
SourceInitialized += MainWindow_SourceInitialized;
InitializeComponent();
Uri iconUri = new Uri("pack://application:,,,/Icon1.ico", UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(iconUri);
}
void MainWindow_SourceInitialized(object sender, EventArgs e)
{
WindowInteropHelper wih = new WindowInteropHelper(this);
int style = GetWindowLong(wih.Handle, GWL_STYLE);
SetWindowLong(wih.Handle, GWL_STYLE, style & ~WS_SYSMENU);
}
任何帮助都将不胜感激!
谢谢 请注意,某些窗口样式在创建窗口后无法更改,但我不知道这是否适用于这些标志。。。据我所知,如果你的标题栏是由系统绘制的,你要么有一个图标和一个关闭按钮,要么没有,因为它们都由
WS\u SYSMENU
窗口样式控制。在表单属性中,例如在WPF应用程序中,你只能隐藏最小化和最小化按钮
有一个名为ResizeMode的属性,如果您设置为NoResize,这两个按钮将被隐藏 您可以将
XAML
中WPF窗口的WindowStyle
属性设置为无。
i、 e
使用代码,您可以执行以下相同操作:-
WindowName.WindowStyle = WindowStyle.None;
必须隐藏所有三个按钮。这是我用来在winforms中启用和禁用关闭按钮的代码。我意识到这在三个方面与你想要的不同
1) 它只处理关闭按钮(尽管,如果奥斯卡是正确的,它是唯一一个你需要担心的)
2) 它不隐藏它,只是禁用/灰显它(尽管您可以更改参数以完全隐藏它)
3) 它适用于winforms,而不是wpf
尽管存在这些差异,但查看代码可能会帮助您找出缺少的内容。如果你能弄明白的话,我很想你发布你的解决方案:)
您希望完全禁用调整大小,还是只删除按钮?我正在尝试隐藏/删除窗口右上角的所有三个按钮(最小化、最大化和关闭)。
WindowName.WindowStyle = WindowStyle.None;
#region Enable / Disable Close Button
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
private const int SC_CLOSE = 0xF060;
private const int MF_BYCOMMAND = 0x0000;
private const int MF_ENABLED = 0x0000;
private const int MF_GRAYED = 0x0001;
protected void DisableCloseButton()
{
try
{
EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
this.CloseButtonIsDisabled = true;
}
catch{}
}
protected void EnableCloseButton()
{
try
{
EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_BYCOMMAND | MF_ENABLED);
this.CloseButtonIsDisabled = false;
}
catch{}
}
protected override void OnSizeChanged(EventArgs e)
{
if (this.CloseButtonIsDisabled)
this.DisableCloseButton();
base.OnSizeChanged(e);
}
#endregion