禁用 WPF 窗口标题栏中的关闭按钮 (C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17962429/
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
Disable Close Button In Title Bar of a WPF Window (C#)
提问by Bubbled86
I'd like to know how to disable (not remove/hide) the Close button in a WPF window. I know how to hide it which makes the window's title bar look like this:
我想知道如何禁用(而不是删除/隐藏)WPF 窗口中的关闭按钮。我知道如何隐藏它,使窗口的标题栏看起来像这样:
But I want to disable it meaning it should look like this:
但我想禁用它,这意味着它应该是这样的:
I'm scripting in C# and using WPF (Windows Presentation Foundation).
我在 C# 中编写脚本并使用 WPF(Windows Presentation Foundation)。
采纳答案by Yoav
Try this:
尝试这个:
public partial class MainWindow : Window
{
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
const uint MF_BYCOMMAND = 0x00000000;
const uint MF_GRAYED = 0x00000001;
const uint SC_CLOSE = 0xF060;
public MainWindow()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// Disable close button
IntPtr hwnd = new WindowInteropHelper(this).Handle;
IntPtr hMenu = GetSystemMenu(hwnd, false);
if (hMenu != IntPtr.Zero)
{
EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
}
}
}
Taken from here(edit: link is broken).
取自此处(编辑:链接已损坏)。
Make sure you set the ResizeMode
to NoResize
.
确保将 设置ResizeMode
为NoResize
。
回答by Rohit
You have to override and in OnCLosingevent set e.cancel=true
您必须覆盖和在OnCLosing事件中设置 e.cancel=true
public MyWindow()
{
InitializeComponent();
this.Closing += new System.ComponentModel.CancelEventHandler(MyWindow_Closing);
}
void MyWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
回答by Erti-Chris Eelmaa
You can probably do it with win32 hackery.
你可能可以用 win32 黑客来做到这一点。
I have done it this way: Get CustomChromeWindow(which will eventually look exactly like the one in picture), and just bind Command() property to viewmodel, and then set CanExecuteCommand=false, which will make the button disabled(How does one "disable" a button in WPF using the MVVM pattern?).
我是这样做的:获取 CustomChromeWindow(最终看起来与图片中的一模一样),然后将 Command() 属性绑定到 viewmodel,然后设置 CanExecuteCommand=false,这将使按钮被禁用(怎么办)禁用”使用 MVVM 模式的 WPF 中的按钮?)。
There might me this way too: How to disable close button on a window in another process with C++?
我也可能这样:如何使用 C++ 在另一个进程中禁用窗口上的关闭按钮?
Basically, call that code with pInvoke. You can obtain WPF window handle easily.
基本上,使用 pInvoke 调用该代码。您可以轻松获得 WPF 窗口句柄。
回答by Ron
This postwhich an answer using Behavior
, GetWindowLong
and SetWindowLong
:
这篇文章的答案使用Behavior
,GetWindowLong
和SetWindowLong
:
public class HideCloseButtonOnWindow : System.Windows.Interactivity.Behavior<Window>
{
#region bunch of native methods
private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
#endregion
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += OnLoaded;
}
protected override void OnDetaching()
{
AssociatedObject.Loaded -= OnLoaded;
base.OnDetaching();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
var hwnd = new System.Windows.Interop.WindowInteropHelper(AssociatedObject).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
}
}
How to use it:
如何使用它:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:w="clr-namespace:WpfApplication2">
<i:Interaction.Behaviors>
<w:HideCloseButtonOnWindow />
</i:Interaction.Behaviors>
</Window>