Отключить кнопку "Закрыть" в строке заголовка окна WPF (С#)
Я хотел бы знать, как отключить (не удалять/скрывать) кнопку "Закрыть" в окне WPF. Я знаю, как скрыть его, что делает строку заголовка окна такой:
![enter image description here]()
Но я хочу отключить его, чтобы он выглядел так:
![enter image description here]()
Я использую скрипт на С# и использую WPF (Windows Presentation Foundation).
Ответы
Ответ 1
Попробуйте следующее:
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 MF_ENABLED = 0x00000000;
const uint SC_CLOSE = 0xF060;
const int WM_SHOWWINDOW = 0x00000018;
const int WM_CLOSE = 0x10;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
if (hwndSource != null)
{
hwndSource.AddHook(new HwndSourceHook(this.hwndSourceHook));
}
}
IntPtr hwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_SHOWWINDOW)
{
IntPtr hMenu = GetSystemMenu(hwnd, false);
if (hMenu != IntPtr.Zero)
{
EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
}
}
else if (msg == WM_CLOSE)
{
handled = true;
}
return IntPtr.Zero;
}
}
(Взято отсюда: http://blogs.microsoft.co.il/blogs/tamir/archive/2007/09/26/never-ever-close-me-how-to-disable-close-button-in-wpf.aspx)
убедитесь, что вы установили ResizeMode
в NoResize
.
Ответ 2
Вы должны переопределить и в 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;
}
Ответ 3
Возможно, вы можете сделать это с помощью хакерства win32.
Я сделал это следующим образом: получите CustomChromeWindow (который в итоге будет выглядеть точно так же, как на картинке), и просто привяжите свойство Command() к viewmodel, а затем установите CanExecuteCommand = false, что приведет к отключению кнопки (Как кнопка "отключить" в WPF с использованием шаблона MVVM?).
Возможно, так и будет: Как отключить кнопку закрытия в окне в другом процессе с помощью С++?
В принципе, вызовите этот код с помощью pInvoke. Вы можете легко получить дескриптор окна WPF.
Ответ 4
Этот пост, ответ с использованием 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);
}
}
Как использовать его:
<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>