博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF自定义Window窗体样式
阅读量:6237 次
发布时间:2019-06-22

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

资源文件代码:

M1,1 L1 ,11 L11,11 L11,1 z M0,0 L12,0 L12,12 L0,12 z
M1,3 L1,11 L9,11 L9,3 z M3,1 L3,2 L10,2 L10,9 L11,9 L11,1 z M2 ,0 L12,0 L12,10 L10,10 L10,12 L0,12 L0,2 L2 ,2 z
View Code

自定义窗体封装WindowEx类代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Reflection;using System.Resources;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows;using System.Windows.Input;using System.Windows.Media;using System.Windows.Resources;using System.Windows.Shapes;namespace SunCreate.Common.Controls{    ///     /// 窗体封装    ///     public class WindowEx : Window, INotifyPropertyChanged    {        public event EventHandler
FixEvent; private ResourceDictionary _resource; private ICommand _WindowBtnCommand; ///
/// 窗体按钮命令 /// public ICommand WindowBtnCommand { get { return _WindowBtnCommand; } set { _WindowBtnCommand = value; OnPropertyChanged("WindowBtnCommand"); } } private Thickness _BorderMargin = new Thickness(0, 0, 0, 0); public Thickness BorderMargin { get { return _BorderMargin; } set { _BorderMargin = value; OnPropertyChanged("BorderMargin"); } } private HorizontalAlignment _BtnPanelHorizontalAlignment = HorizontalAlignment.Right; ///
/// 窗体按钮的Panel位置 /// public HorizontalAlignment BtnPanelHorizontalAlignment { get { return _BtnPanelHorizontalAlignment; } set { _BtnPanelHorizontalAlignment = value; OnPropertyChanged("BtnPanelHorizontalAlignment"); } } private Visibility _BtnMinimizeVisibility = Visibility.Visible; ///
/// 窗体最小化按钮的显示状态 /// public Visibility BtnMinimizeVisibility { get { return _BtnMinimizeVisibility; } set { _BtnMinimizeVisibility = value; OnPropertyChanged("BtnMinimizeVisibility"); } } private Visibility _BtnMaximizeVisibility = Visibility.Visible; ///
/// 窗体最大化按钮的显示状态 /// public Visibility BtnMaximizeVisibility { get { return _BtnMaximizeVisibility; } set { _BtnMaximizeVisibility = value; OnPropertyChanged("BtnMaximizeVisibility"); } } private Geometry _BtnMaximizePathData; ///
/// 窗体最大化按钮的样式 /// public Geometry BtnMaximizePathData { get { return _BtnMaximizePathData; } set { _BtnMaximizePathData = value; OnPropertyChanged("BtnMaximizePathData"); } } private Visibility _TitleVisibility = Visibility.Visible; ///
/// 是否显示标题 /// public Visibility TitleVisibility { get { return _TitleVisibility; } set { _TitleVisibility = value; OnPropertyChanged("TitleVisibility"); } } private WindowExTheme _Theme = WindowExTheme.Default; ///
/// 窗体主题 /// public WindowExTheme Theme { get { return _Theme; } set { _Theme = value; OnPropertyChanged("Theme"); } } ///
/// 窗体 构造函数 /// public WindowEx() { this.Loaded += WindowEx_Loaded; this.DataContext = this; #region 窗体样式设置 //this.AllowsTransparency = true; //AllowsTransparency会导致视频播放不显示 this.WindowStyle = WindowStyle.None; #endregion #region 窗体按钮事件 WindowBtnCommand windowBtnCommand = new WindowBtnCommand(); windowBtnCommand.DoAction = (parameter) => { if (parameter == 1) //最小化 { MinimizedSet(); this.WindowState = WindowState.Minimized; } if (parameter == 2) //窗口还原、最大化 { if (this.WindowState == WindowState.Normal) { MaximizedSet(); this.WindowState = WindowState.Maximized; } else if (this.WindowState == WindowState.Maximized) { RestoredSet(); this.WindowState = WindowState.Normal; } else if (this.WindowState == WindowState.Minimized) { RestoredSet(); this.WindowState = WindowState.Normal; } } if (parameter == 3) //关闭窗口 { this.Close(); } if (parameter == 4) //固定窗口 { if (FixEvent != null) { WindowExFixEventArgs args = new WindowExFixEventArgs(this.Content); FixEvent(this, args); } } }; this.WindowBtnCommand = windowBtnCommand; this.StateChanged += (s, e) => { if (this.WindowState == WindowState.Maximized) { MaximizedSet(); } if (this.WindowState == WindowState.Normal) { RestoredSet(); } if (this.WindowState == WindowState.Minimized) { MinimizedSet(); } }; #endregion } ///
/// 窗体Loaded /// private void WindowEx_Loaded(object sender, RoutedEventArgs e) { #region 窗体样式设置 Uri uri = null; switch (Theme) { case WindowExTheme.Default: uri = new Uri("/SunCreate.Common.Controls;Component/WindowEx/WindowExResource.xaml", UriKind.Relative); break; case WindowExTheme.MessageBox: uri = new Uri("/SunCreate.Common.Controls;Component/WindowEx/MessageBoxExResource.xaml", UriKind.Relative); break; case WindowExTheme.TabContainer: uri = new Uri("/SunCreate.Common.Controls;Component/WindowEx/TabContainerResource.xaml", UriKind.Relative); break; } _resource = new ResourceDictionary(); _resource.Source = uri; this.Style = _resource["stlWindowEx"] as Style; if (this.WindowState == WindowState.Maximized) { this.BtnMaximizePathData = _resource["pathRestore"] as PathGeometry; } else { this.BtnMaximizePathData = _resource["pathMaximize"] as PathGeometry; } #endregion #region 最大化设置 if (this.WindowState == WindowState.Maximized) { this.BorderMargin = CalculateWinMargin(true); } #endregion } #region 最小化设置 private void MinimizedSet() { this.BorderMargin = new Thickness(0, 0, 0, 0); BtnPanelHorizontalAlignment = HorizontalAlignment.Left; BtnMinimizeVisibility = Visibility.Collapsed; if (this.Content != null) (this.Content as FrameworkElement).Visibility = Visibility.Collapsed; //最小化时隐藏Content if (this.Theme == WindowExTheme.TabContainer) TitleVisibility = Visibility.Visible; this.BtnMaximizePathData = _resource["pathRestore"] as PathGeometry; } #endregion #region 还原设置 private void RestoredSet() { this.BorderMargin = new Thickness(0, 0, 0, 0); BtnPanelHorizontalAlignment = HorizontalAlignment.Right; BtnMinimizeVisibility = Visibility.Visible; if (this.Content != null) (this.Content as FrameworkElement).Visibility = Visibility.Visible; //最大化或还原时显示Content this.BtnMaximizePathData = _resource["pathMaximize"] as PathGeometry; if (this.Theme == WindowExTheme.TabContainer) TitleVisibility = Visibility.Collapsed; } #endregion #region 最大化设置 private void MaximizedSet() { this.BorderMargin = CalculateWinMargin(false); BtnPanelHorizontalAlignment = HorizontalAlignment.Right; BtnMinimizeVisibility = Visibility.Visible; if (this.Content != null) (this.Content as FrameworkElement).Visibility = Visibility.Visible; //最大化或还原时显示Content this.BtnMaximizePathData = _resource["pathRestore"] as PathGeometry; if (this.Theme == WindowExTheme.TabContainer) TitleVisibility = Visibility.Collapsed; } #endregion #region 计算窗体Margin大小 ///
/// 计算窗体Margin大小 /// private Thickness CalculateWinMargin(bool firstLoad = false) { double taskBarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.WorkArea.Height; double taskBarWidth = SystemParameters.PrimaryScreenWidth - SystemParameters.WorkArea.Width; if (this.Theme == WindowExTheme.TabContainer || firstLoad) { if (taskBarWidth > 0) { return new Thickness(7, 7, taskBarWidth + 7, 7); } if (taskBarHeight > 0) { return new Thickness(7, 7, 7, taskBarHeight + 7); } return new Thickness(7, 7, 7, 7); } else { if (taskBarWidth > 0) { return new Thickness(0, 0, taskBarWidth, 0); } if (taskBarHeight > 0) { return new Thickness(0, 0, 0, taskBarHeight); } return new Thickness(0, 0, 0, 0); } } #endregion #region 实现INotifyPropertyChanged接口 public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } #endregion }}
View Code

窗体最小化、最大化、关闭按钮的命令WindowBtnCommand:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Input;namespace SunCreate.Common.Controls{    public class WindowBtnCommand : ICommand    {        public Action
DoAction { get; set; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public bool CanExecute(object parameter) { return true; } public void Execute(object parameter) { if (DoAction != null) { DoAction(Convert.ToInt32(parameter)); } } }}
View Code

使用WindowEx类的示例代码:

View Code

效果图:

窗体最小化效果图:

 

转载于:https://www.cnblogs.com/s0611163/p/9994529.html

你可能感兴趣的文章
Kerberos
查看>>
.Net最佳实践3:使用性能计数器收集性能数据
查看>>
Mac上编译libimobiledevice库
查看>>
AngularJS中的ng-controller是什么东东
查看>>
使用AngularJS中的filterFilter函数进行过滤
查看>>
windows 下安装nodejs 要怎么设置环境变量
查看>>
零售月结
查看>>
Android与iOS支持的音频格式
查看>>
深入理解软件构造系统:原理与最佳实践
查看>>
MySQL5.7: sql script demo
查看>>
VC助手失效解决办法
查看>>
一起谈.NET技术,模拟IIS向Silverlight输出策略文件
查看>>
Asp.Net知识锦分享,徐汇区网站设计
查看>>
如果编程语言是一条船
查看>>
J2EE 全面简介
查看>>
网站长期被挂马 访问者屡遭木马侵害
查看>>
Google推反恶意广告网站 防护恶意软件威胁
查看>>
跟我学AngularJs:AngularJs入门及第一个实例
查看>>
翻译处女作欢迎批评指正-tornado的模板系统文档
查看>>
什么时候该用MVC,什么时候该用Webform?
查看>>