博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF自定义控件(三)——Window
阅读量:5138 次
发布时间:2019-06-13

本文共 10185 字,大约阅读时间需要 33 分钟。

  一样!先来看看效果吧:

  

  怎么样?效果很好吧,而且不只是样式哟!所有系统窗体有的交互操作都可以实现!

  但可惜。。。有很多和系统API有关的东西本人了解得并不多,所以这个窗体是基于他人的成果上产生的。关于窗体交互的东西还是看看原作者的吧:

  

  接下来说说我在这上面多加的功能:可设置标题栏可见性,可扩展标题栏下拉按钮,可全屏并拦截键盘,另外还多公布了一些属性

  再来一张图:

  

  这就是没有标题栏的,只是设置一个属性就可以了哟!

  废话不多说了!再来看看我代码,只发一些我加了的吧,上面的链接里有的我就不发了,想看完整的,之后我会发源码的。

  先是Xaml的:

1 
4
121

  还有CS的:

1         ///   2         /// 静态构造方法  3         ///   4         static XWindow()  5         {  6             XWindow.XIsShowBdrTitleBarProperty = DependencyProperty.Register("XIsShowBdrTitleBar", typeof(bool), typeof(XWindow),  7                 new PropertyMetadata(true));  8             XWindow.XIsShowBtnDropDownProperty = DependencyProperty.Register("XIsShowBtnDropDown", typeof(bool), typeof(XWindow),  9                 new PropertyMetadata(false)); 10             FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(XWindow), new FrameworkPropertyMetadata(typeof(XWindow))); 11         }     12 /// 13  14 public void InitializeEvent() 15         { 16  17             //解决没有图标时标题不顶头问题 18             if (imgIcon.Source == null) 19             { 20                 imgIcon.Visibility = Visibility.Collapsed; 21             } 22  23             //防止窗体一启动时就最大化引发的边框和按钮问题 24             if (WindowState == WindowState.Maximized) 25             { 26                 bdrWindow.Margin = new Thickness(0); 27                 ImageBrush img = new ImageBrush(); 28                 ImageBrush imgE = new ImageBrush(); 29                 ImageBrush imgM = new ImageBrush(); 30                 img.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestore.png")); 31                 btnMax.Background = img; 32                 imgE.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestoreEnter.png")); 33                 btnMax.XEnterBrush = imgE; 34                 imgM.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestoreMove.png")); 35                 btnMax.XMoverBrush = imgM; 36             } 37             else 38             { 39                 bdrWindow.Margin = new Thickness(customMargin); 40                 ImageBrush img = new ImageBrush(); 41                 ImageBrush imgE = new ImageBrush(); 42                 ImageBrush imgM = new ImageBrush(); 43                 img.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMax.png")); 44                 btnMax.Background = img; 45                 imgE.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMaxEnter.png")); 46                 btnMax.XEnterBrush = imgE; 47                 imgM.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMaxMove.png")); 48                 btnMax.XMoverBrush = imgM; 49             } 50  51             //最小化窗体 52             btnMin.Click += delegate 53             { 54                 this.WindowState = WindowState.Minimized; 55             }; 56  57             //最大化窗体 58             btnMax.Click += delegate 59             { 60                 if (this.WindowState == WindowState.Maximized) 61                 { 62                     this.WindowState = WindowState.Normal; 63                 } 64                 else 65                 { 66                     this.WindowState = WindowState.Maximized; 67                 } 68             }; 69  70             //关闭窗体 71             btnClose.Click += delegate 72             { 73                 Exit(); 74             }; 75  76             //双击标题栏最大化窗体 77             bdrTitleBar.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e) 78             { 79                 if ((e.ClickCount >= 2 && ResizeMode == System.Windows.ResizeMode.CanResize)) 80                 { 81                     btnMax.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); 82                 } 83             }; 84  85             // 86             bdrTitleBar.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e) 87             { 88                 Window_MouseLeftButtonDown(sender, e); 89             }; 90         } 91  92  93 ///  94         /// 处理最大化时阴影边框问题,和最大化按钮变化 95         ///  96         ///  97         ///  98         void Window_StateChanged(object sender, EventArgs e) 99         {100             if (WindowState == WindowState.Maximized)101             {102                 bdrWindow.Margin = new Thickness(0);103                 ImageBrush img = new ImageBrush();104                 ImageBrush imgE = new ImageBrush();105                 ImageBrush imgM = new ImageBrush();106                 img.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestore.png"));107                 btnMax.Background = img;108                 imgE.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestoreEnter.png"));109                 btnMax.XEnterBrush = imgE;110                 imgM.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestoreMove.png"));111                 btnMax.XMoverBrush = imgM;112             }113             else114             {115                 bdrWindow.Margin = new Thickness(customMargin);116                 ImageBrush img = new ImageBrush();117                 ImageBrush imgE = new ImageBrush();118                 ImageBrush imgM = new ImageBrush();119                 img.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMax.png"));120                 btnMax.Background = img;121                 imgE.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMaxEnter.png"));122                 btnMax.XEnterBrush = imgE;123                 imgM.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMaxMove.png"));124                 btnMax.XMoverBrush = imgM;125             }126         }127 ////128 129 /// 130         /// 键盘捕捉131         /// 132         /// 133         /// 134         public void OnKeyPress(Hook.HookStruct hookStruct, out bool handle)135         {136             //默认不屏蔽137             handle = false;138 139             //屏蔽键Win140             if (hookStruct.vkCode == (int)WinForms.Keys.LWin || hookStruct.vkCode == (int)WinForms.Keys.RWin)141             {142                 handle = true;143             }144 145         }146 147 148  #region 公布方法149         /// 150         /// 进入全屏151         /// 152         public void XFullScreen()153         {154             //解决最大化后,全屏不覆盖任务栏的问题155             this.WindowState = WindowState.Normal;156             bdrTitleBar.Visibility = Visibility.Collapsed;157             bdrWindow.Margin = new Thickness(0);158 159             this.ResizeMode = System.Windows.ResizeMode.NoResize;160 161             this.Left = 0.0;162             this.Top = 0.0;163             this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;164             this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;165 166             //屏蔽鼠标拖动167             this.MouseLeftButtonDown -= new MouseButtonEventHandler(Window_MouseLeftButtonDown);168 169             //安装钩子170             keyboardHook = new Hook();171             keyboardHook.InstallHook(this.OnKeyPress);172         }173 174         /// 175         /// 退出全屏176         /// 177         public void XExitFullScreen(double width, double height, double x, double y)178         {179             bdrTitleBar.Visibility = Visibility.Visible;180             bdrWindow.Margin = new Thickness(customMargin);181 182             //窗体恢复位置183             this.ResizeMode = ResizeMode.CanResize;184             this.WindowState = WindowState.Normal;185             this.Width = width;186             this.Height = height;187             this.Left = x;188             this.Top = y;189 190             //添加鼠标拖动事件191             this.MouseLeftButtonDown += new MouseButtonEventHandler(Window_MouseLeftButtonDown);192 193             //取消钩子194             keyboardHook.UninstallHook();195         }196 197         /// 198         /// 退出方法199         /// 200         public virtual void Exit()201         {202             this.Close();203         }204         #endregion205 206         #region 公布属性207         /// 208         /// 公布属性XIsShowBdrTitleBar(是否显示标题栏)209         /// 210         public bool XIsShowBdrTitleBar211         {212             get213             {214                 return (bool)base.GetValue(XWindow.XIsShowBdrTitleBarProperty);215             }216             set217             {218                 base.SetValue(XWindow.XIsShowBdrTitleBarProperty, value);219             }220         }221 222         /// 223         /// 公布属性XIsShowBtnDropDown(是否显示下拉按钮)224         /// 225         public bool XIsShowBtnDropDown226         {227             get228             {229                 return (bool)base.GetValue(XWindow.XIsShowBtnDropDownProperty);230             }231             set232             {233                 base.SetValue(XWindow.XIsShowBtnDropDownProperty, value);234             }235         }236         #endregion237 238         #region 重写方法239         /// 240         /// 应用样式241         /// 242         public override void OnApplyTemplate()243         {244             base.OnApplyTemplate();245             btnMin = GetTemplateChild("btnMin") as XButton;246             btnMax = GetTemplateChild("btnMax") as XButton;247             btnClose = GetTemplateChild("btnClose") as XButton;248             btnDropDown = GetTemplateChild("btnDropDown") as XButton;249             imgIcon = GetTemplateChild("imgIcon") as Image;250             bdrWindow = GetTemplateChild("bdrWindow") as Border;251             bdrTitleBar = GetTemplateChild("bdrTitleBar") as Border;252         }253         #endregion

  好啦!太多了,有点乱是吧!待会我把源码发上来吧!大家看看!多多提意见啊!

转载于:https://www.cnblogs.com/QinYK/p/4075205.html

你可能感兴趣的文章
iOS中图片与视频一次性多选
查看>>
XML —— 简介
查看>>
用Wdatepicker日期控件实现结束时间必须为开始时间之后
查看>>
PyNest——part 3: connecting networks with synapses
查看>>
[转载]AngularJS入门教程03:迭代器
查看>>
live555学习笔记1-引子
查看>>
Android画布和图形绘制---Canvas and Drawables(一)
查看>>
(二十一)即时通信的聊天气泡的实现II
查看>>
(四十九)Quartz2D自定义控件
查看>>
Trac 经验谈之(5)插件篇
查看>>
NDK学习 登录逻辑 C调用java方法
查看>>
Form.ShowWithoutActivation 属性
查看>>
删除github项目的某一个文件夹
查看>>
接口没添加@responseBody注解
查看>>
winsock 收发广播包
查看>>
Junit3和Junit4使用区别
查看>>
CentOS 7 网络配置
查看>>
No repeats please 全排列
查看>>
李航《统计学习方法》CH03
查看>>
《分析服务从入门到精通读书笔记》第四章、父子维度补充篇
查看>>