| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- using System;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace Muchinfo.WPF.Controls.Windows
- {
- /// <summary>
- /// Class WindowBase.
- /// </summary>
- [TemplatePart(Name = "btnClose", Type = typeof(System.Windows.Controls.Button))]
- [TemplatePart(Name = "btnMin", Type = typeof(System.Windows.Controls.Button))]
- [TemplatePart(Name = "brdTitle", Type = typeof(System.Windows.Controls.Border))]
- [TemplatePart(Name = "btnMax", Type = typeof(System.Windows.Controls.Button))]
- //[TemplatePart(Name = "WindowBorder", Type = typeof(Border))]
- public class WindowBase : Window
- {
- static WindowBase()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowBase), new FrameworkPropertyMetadata(typeof(WindowBase)));
- }
- private System.Windows.Controls.Button _btnClose;
- private System.Windows.Controls.Border _brdTitle;
- private System.Windows.Controls.Button _btnMin;
- private System.Windows.Controls.Button _btnMax;
- //private Border winborborder;
- public WindowBase()
- {
- WindowStartupLocation = WindowStartupLocation.CenterOwner;
- this.Loaded += delegate
- {
- InitializeEvent();
- ShowInTaskbar = this.Owner == null;
- };
- }
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- _btnClose = GetTemplateChild("btnClose") as System.Windows.Controls.Button;
- _brdTitle = GetTemplateChild("brdTitle") as System.Windows.Controls.Border;
- _btnMin = GetTemplateChild("btnMin") as System.Windows.Controls.Button;
- _btnMax = GetTemplateChild("btnMax") as System.Windows.Controls.Button;
- //winborborder = GetTemplateChild("WindowBorder") as Border;
- }
- private void InitializeEvent()
- {
- if (_btnMin != null)
- {
- _btnMin.Click += delegate
- {
- };
- }
- if (_btnClose != null)
- {
- _btnClose.Click += delegate
- {
- this.Close();
- };
- }
- if (_btnMax != null)
- {
- _btnMax.Click += delegate
- {
- this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
- };
- }
- if (_brdTitle != null)
- {
- _brdTitle.MouseMove += delegate(object sender, MouseEventArgs e)
- {
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- this.DragMove();
- }
- };
- }
- //只手动抬头移动窗口
- //if (winborborder != null)
- //{
- // winborborder.MouseMove += delegate(object sender, MouseEventArgs e)
- // {
- // if (e.LeftButton == MouseButtonState.Pressed)
- // {
- // this.DragMove();
- // }
- // };
- //}
- }
- /// <summary>
- /// 窗口圆角
- /// </summary>
- public CornerRadius CornerRadius
- {
- get { return (CornerRadius)GetValue(CornerRadiusProperty); }
- set { SetValue(CornerRadiusProperty, value); }
- }
- public static readonly DependencyProperty CornerRadiusProperty =
- DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(WindowBase), new PropertyMetadata(new CornerRadius(2)));
- public Brush TitleBackGround
- {
- get { return (Brush)GetValue(TitleBackGroundProperty); }
- set { SetValue(TitleBackGroundProperty, value); }
- }
- // Using a DependencyProperty as the backing store for TitleBackGround. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty TitleBackGroundProperty =
- DependencyProperty.Register("TitleBackGround", typeof(Brush), typeof(WindowBase), new PropertyMetadata(Brushes.Bisque));
- public Brush TitleForeground
- {
- get { return (Brush)GetValue(TitleForegroundProperty); }
- set { SetValue(TitleForegroundProperty, value); }
- }
- // Using a DependencyProperty as the backing store for TitleFordBrush. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty TitleForegroundProperty =
- DependencyProperty.Register("TitleForeground", typeof(Brush), typeof(WindowBase), new PropertyMetadata(Brushes.Black));
- /// <summary>
- /// 关闭按钮样式
- /// </summary>
- public Style CloseButtonStyle
- {
- get { return (Style)GetValue(CloseButtonStyleProperty); }
- set { SetValue(CloseButtonStyleProperty, value); }
- }
- // Using a DependencyProperty as the backing store for CloseButtonStyle. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty CloseButtonStyleProperty =
- DependencyProperty.Register("CloseButtonStyle", typeof(Style), typeof(WindowBase), new PropertyMetadata(null));
- /// <summary>
- /// 最大最小化
- /// </summary>
- public Style MaxBtnStyle
- {
- get { return (Style)GetValue(MaxBtnStyleProperty); }
- set { SetValue(MaxBtnStyleProperty, value); }
- }
- // Using a DependencyProperty as the backing store for MaxStyle. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty MaxBtnStyleProperty =
- DependencyProperty.Register("MaxBtnStyle", typeof(Style), typeof(WindowBase), new PropertyMetadata(null));
- /// <summary>
- /// 是否可最大化最小化
- /// </summary>
- public Visibility MaxBtnVisibility
- {
- get { return (Visibility)GetValue(MaxBtnVisibilityProperty); }
- set { SetValue(MaxBtnVisibilityProperty, value); }
- }
- // Using a DependencyProperty as the backing store for MaxBtnVisibility. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty MaxBtnVisibilityProperty =
- DependencyProperty.Register("MaxBtnVisibility", typeof(Visibility), typeof(WindowBase), new PropertyMetadata(Visibility.Collapsed));
- public ICommand ClosedCommand
- {
- get { return (ICommand)GetValue(ClosedCommandProperty); }
- set { SetValue(ClosedCommandProperty, value); }
- }
- // Using a DependencyProperty as the backing store for ClosedCommand. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty ClosedCommandProperty =
- DependencyProperty.Register("ClosedCommand", typeof(ICommand), typeof(WindowBase), new PropertyMetadata(null));
- protected override void OnClosed(EventArgs e)
- {
- if (ClosedCommand != null)
- {
- ClosedCommand.Execute(this);
- }
- base.OnClosed(e);
- }
- }
- }
|