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

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

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

  •  借助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函数中添加如下代码:

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

  • 放大按钮禁用

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

  • 彻底删除关闭按钮(效果如图)

 

这个方法会将页面logo,放大、缩小、关闭全部隐藏。

代码如下:

在window类里面添加:

        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函数中添加:

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

 

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

窗体上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

转载于:https://my.oschina.net/u/3661223/blog/1527006

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中的 DataGrid 和 ListView 都没有内置的分页控件,但可以通过一些方法来实现分页功能。 一种实现方式是使用 CollectionView,它是用于对集合进行排序、过滤和分组的类。可以使用 CollectionViewSource 创建 CollectionView,并在 XAML 中绑定到分页控件中。 以下是一个示例,其中使用 CollectionViewSource 来创建 CollectionView,并将其绑定到 ListView 控件中: ```xml <Grid> <Grid.Resources> <CollectionViewSource x:Key="cvs" Source="{Binding Items}" PageSize="10"/> </Grid.Resources> <ListView ItemsSource="{Binding Source={StaticResource cvs}}" /> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <Button Content="Prev" Command="{Binding Source={StaticResource cvs}, Path=PageUpCommand}" /> <TextBlock Text="{Binding Source={StaticResource cvs}, Path=PageIndex}" /> <Button Content="Next" Command="{Binding Source={StaticResource cvs}, Path=PageDownCommand}" /> </StackPanel> </Grid> ``` 在上面的示例中,CollectionViewSource 用于创建 CollectionView,并使用 PageSize 属性来指定每页的项数。ListView 控件绑定到 CollectionViewSource 中的 CollectionView。 同时,使用按钮来切换分页,每个按钮都绑定到 CollectionViewSource 中的 PageUpCommand 和 PageDownCommand 命令,以便在前一页和后一页之间切换。 还有其他的实现方式,例如手动分页和使用第三方分页控件等等。具体实现方式可以根据项目需求来选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值