WPF窗体关闭/放大/缩小按钮禁用、隐藏的实现

今天接到同事的一个需求,想把wpf中window窗体的关闭按钮禁用,他要在页面中写一个添加按钮点击函数调用this.close();来关闭窗体。html

二话不说去百度了,而后找到一堆方法,实验结果都不相同,也出现一些问题。下面先放正确的方法:ide

  •  借助user32.dll的API,disable关闭按钮(效果以下图):

 

在页面的.xaml.cs文件window类中添加以下代码:函数

[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, UInt32 bRevert);
        [DllImport("USER32.DLL ", CharSet = CharSet.Unicode)]
        private static extern UInt32 RemoveMenu(IntPtr hMenu, UInt32 nPosition, UInt32 wFlags);
        private const UInt32 SC_CLOSE = 0x0000F060;
        private const UInt32 MF_BYCOMMAND = 0x00000000;

而后再window类的Loaded函数中添加以下代码:this

var hwnd = new WindowInteropHelper(this).Handle;  //获取window的句柄
            IntPtr hMenu = GetSystemMenu(hwnd, 0);
            RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);

  • 放大按钮禁用

直接在window中设置属性 ResizeMode="CanMinimize"便可。spa

  • 完全删除关闭按钮(效果如图)

 

这个方法会将页面logo,放大、缩小、关闭所有隐藏。.net

代码以下:code

在window类里面添加:orm

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

而后在类的Loaded函数中添加:xml

var hwnd = new WindowInteropHelper(this).Handle;
            SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);

 

同上一个方法差很少,使用自定义类来实现,重用它并将它定义为窗口的附加属性,实现效果以下:htm

窗体上logo、标题、放大、缩小、关闭全都隐藏了。

代码以下:(新建一个类文件)

public class WindowBehavior
    {
        private static readonly Type OwnerType = typeof(WindowBehavior);

        #region HideCloseButton (attached property)

        public static readonly DependencyProperty HideCloseButtonProperty =
        DependencyProperty.RegisterAttached(
       "HideCloseButton",
        typeof(bool),
        OwnerType,
        new FrameworkPropertyMetadata(false, new PropertyChangedCallback(HideCloseButtonChangedCallback)));

        [AttachedPropertyBrowsableForType(typeof(Window))]
        public static bool GetHideCloseButton(Window obj)
        {
            return (bool)obj.GetValue(HideCloseButtonProperty);
        }

        [AttachedPropertyBrowsableForType(typeof(Window))]
        public static void SetHideCloseButton(Window obj, bool value)
        {
            obj.SetValue(HideCloseButtonProperty, value);
        }

        private static void HideCloseButtonChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var window = d as Window;
            if (window == null) return;

            var hideCloseButton = (bool)e.NewValue;
            if (hideCloseButton && !GetIsHiddenCloseButton(window))
            {
                if (!window.IsLoaded)
                {
                    window.Loaded += HideWhenLoadedDelegate;
                }
                else
                {
                    HideCloseButton(window);
                }
                SetIsHiddenCloseButton(window, true);
            }
            else if (!hideCloseButton && GetIsHiddenCloseButton(window))
            {
                if (!window.IsLoaded)
                {
                    window.Loaded -= ShowWhenLoadedDelegate;
                }
                else
                {
                    ShowCloseButton(window);
                }
                SetIsHiddenCloseButton(window, false);
            }
        }

        #region Win32 imports

        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

        private static readonly RoutedEventHandler HideWhenLoadedDelegate = (sender, args) => {
            if (sender is Window == false) return;
            var w = (Window)sender;
            HideCloseButton(w);
            w.Loaded -= HideWhenLoadedDelegate;
        };

        private static readonly RoutedEventHandler ShowWhenLoadedDelegate = (sender, args) => {
            if (sender is Window == false) return;
            var w = (Window)sender;
            ShowCloseButton(w);
            w.Loaded -= ShowWhenLoadedDelegate;
        };

        private static void HideCloseButton(Window w)
        {
            var hwnd = new WindowInteropHelper(w).Handle;
            SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
        }

        private static void ShowCloseButton(Window w)
        {
            var hwnd = new WindowInteropHelper(w).Handle;
            SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_SYSMENU);
        }

        #endregion

        #region IsHiddenCloseButton (readonly attached property)

        private static readonly DependencyPropertyKey IsHiddenCloseButtonKey =
        DependencyProperty.RegisterAttachedReadOnly(
       "IsHiddenCloseButton",
        typeof(bool),
        OwnerType,
        new FrameworkPropertyMetadata(false));

        public static readonly DependencyProperty IsHiddenCloseButtonProperty =
        IsHiddenCloseButtonKey.DependencyProperty;

        [AttachedPropertyBrowsableForType(typeof(Window))]
        public static bool GetIsHiddenCloseButton(Window obj)
        {
            return (bool)obj.GetValue(IsHiddenCloseButtonProperty);
        }

        private static void SetIsHiddenCloseButton(Window obj, bool value)
        {
            obj.SetValue(IsHiddenCloseButtonKey, value);
        }

        #endregion

    }

而后在window页面中使用:

<Window 
 x:Class="WafClient.Presentation.Views.SampleWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:model="clr-namespace:EGIS.MISPlatform.Model;assembly=EGIS.MISPlatform"
 ResizeMode="NoResize"
 model:WindowBehavior.HideCloseButton="True"
 . . .
</Window>
  • 直接重写onlosing事件(缺点须要在任务管理器中才能关闭窗口)不推荐

 在window类中添加:

 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true; 
        } 

我找到的差很少就这些方法了。下面是参考文

http://www.cnblogs.com/khler/archive/2009/11/26/1611446.html

https://ask.helplib.com/220145

http://blog.csdn.net/gywtzh0889/article/details/47728569

相关文章
相关标签/搜索