一样!先来看看效果吧:
怎么样?效果很好吧,而且不只是样式哟!所有系统窗体有的交互操作都可以实现!
但可惜。。。有很多和系统API有关的东西本人了解得并不多,所以这个窗体是基于他人的成果上产生的。关于窗体交互的东西还是看看原作者的吧:
接下来说说我在这上面多加的功能:可设置标题栏可见性,可扩展标题栏下拉按钮,可全屏并拦截键盘,另外还多公布了一些属性
再来一张图:
这就是没有标题栏的,只是设置一个属性就可以了哟!
废话不多说了!再来看看我代码,只发一些我加了的吧,上面的链接里有的我就不发了,想看完整的,之后我会发源码的。
先是Xaml的:
14 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
好啦!太多了,有点乱是吧!待会我把源码发上来吧!大家看看!多多提意见啊!