Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
禁用WPF窗口标题栏中的关闭按钮(C#)_C#_Wpf - Fatal编程技术网

禁用WPF窗口标题栏中的关闭按钮(C#)

禁用WPF窗口标题栏中的关闭按钮(C#),c#,wpf,C#,Wpf,我想知道如何在WPF窗口中禁用(而不是删除/隐藏)关闭按钮。我知道如何隐藏它,这使窗口的标题栏如下所示: 但我想禁用它,这意味着它应该如下所示: 我正在用C#编写脚本,并使用WPF(Windows Presentation Foundation)。您必须重写并在事件集中关闭事件集e.cancel=true public MyWindow() { InitializeComponent(); this.Closing += new System.ComponentModel.C

我想知道如何在WPF窗口中禁用(而不是删除/隐藏)关闭按钮。我知道如何隐藏它,这使窗口的标题栏如下所示:

但我想禁用它,这意味着它应该如下所示:


我正在用C#编写脚本,并使用WPF(Windows Presentation Foundation)。

您必须重写并在事件集中关闭事件集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;
}

您可能可以使用win32 hackery来实现这一点

我是这样做的:获取CustomChromeWindow(最终看起来与图片中的完全相同),将Command()属性绑定到viewmodel,然后设置CanExecuteCommand=false,这将使按钮被禁用()

我也可以这样说:

基本上,用pInvoke调用代码。您可以轻松获得WPF窗口句柄

试试这个:

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);
        }
    }
}
取自(编辑:链接已断开)

确保将
ResizeMode
设置为
NoResize

使用
行为
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);
    }
}
公共类HideCloseButtonWindow:System.Windows.Interactivity.Behavior
{
#本地方法的区域组
私有常量int GWL_STYLE=-16;
私有常量int WS_SYSMENU=0x80000;
[DllImport(“user32.dll”,SetLastError=true)]
私有静态外部intgetWindowLong(intptrhwnd,intnindex);
[DllImport(“user32.dll”)]
私有静态外部intsetWindowLong(IntPtr hWnd、intnindex、intdwnewlong);
#端区
受保护的覆盖无效附加()
{
base.onatached();
AssociatedObject.Loaded+=已加载;
}
附加时受保护的覆盖无效()
{
AssociatedObject.Loaded-=已加载;
base.OnDetaching();
}
已加载专用void(对象发送方,RoutedEventArgs e)
{
var hwnd=new System.Windows.Interop.WindowInteropHelper(AssociatedObject).Handle;
SetWindowLong(hwnd,GWL_样式,GetWindowLong(hwnd,GWL_样式)和~WS_系统菜单);
}
}
如何使用它:

<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>


这不会使按钮看起来被禁用。是的,但我想实际禁用关闭按钮,而不是使其取消关闭。恐怕您只能在WPF中隐藏或覆盖。请查看该页面上的@Kyle,它似乎正是OP在这里寻找的。(编辑:我看到它做得更多一点,而且更容易删除。)@hvd链接中的选定答案将永久删除窗口中的关闭图标,输出类似于此问题中用户不想要的第一幅图像。我希望我说得更清楚。我找到此链接,看看此[link][1]是否有帮助:[1]:这不会使“关闭”按钮看起来像已禁用,而是会将其完全删除。