WindowBase.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Input;
  4. using System.Windows.Media;
  5. namespace Muchinfo.WPF.Controls.Windows
  6. {
  7. /// <summary>
  8. /// Class WindowBase.
  9. /// </summary>
  10. [TemplatePart(Name = "btnClose", Type = typeof(System.Windows.Controls.Button))]
  11. [TemplatePart(Name = "btnMin", Type = typeof(System.Windows.Controls.Button))]
  12. [TemplatePart(Name = "brdTitle", Type = typeof(System.Windows.Controls.Border))]
  13. [TemplatePart(Name = "btnMax", Type = typeof(System.Windows.Controls.Button))]
  14. //[TemplatePart(Name = "WindowBorder", Type = typeof(Border))]
  15. public class WindowBase : Window
  16. {
  17. static WindowBase()
  18. {
  19. DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowBase), new FrameworkPropertyMetadata(typeof(WindowBase)));
  20. }
  21. private System.Windows.Controls.Button _btnClose;
  22. private System.Windows.Controls.Border _brdTitle;
  23. private System.Windows.Controls.Button _btnMin;
  24. private System.Windows.Controls.Button _btnMax;
  25. //private Border winborborder;
  26. public WindowBase()
  27. {
  28. WindowStartupLocation = WindowStartupLocation.CenterOwner;
  29. this.Loaded += delegate
  30. {
  31. InitializeEvent();
  32. ShowInTaskbar = this.Owner == null;
  33. };
  34. }
  35. public override void OnApplyTemplate()
  36. {
  37. base.OnApplyTemplate();
  38. _btnClose = GetTemplateChild("btnClose") as System.Windows.Controls.Button;
  39. _brdTitle = GetTemplateChild("brdTitle") as System.Windows.Controls.Border;
  40. _btnMin = GetTemplateChild("btnMin") as System.Windows.Controls.Button;
  41. _btnMax = GetTemplateChild("btnMax") as System.Windows.Controls.Button;
  42. //winborborder = GetTemplateChild("WindowBorder") as Border;
  43. }
  44. private void InitializeEvent()
  45. {
  46. if (_btnMin != null)
  47. {
  48. _btnMin.Click += delegate
  49. {
  50. };
  51. }
  52. if (_btnClose != null)
  53. {
  54. _btnClose.Click += delegate
  55. {
  56. this.Close();
  57. };
  58. }
  59. if (_btnMax != null)
  60. {
  61. _btnMax.Click += delegate
  62. {
  63. this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
  64. };
  65. }
  66. if (_brdTitle != null)
  67. {
  68. _brdTitle.MouseMove += delegate(object sender, MouseEventArgs e)
  69. {
  70. if (e.LeftButton == MouseButtonState.Pressed)
  71. {
  72. this.DragMove();
  73. }
  74. };
  75. }
  76. //只手动抬头移动窗口
  77. //if (winborborder != null)
  78. //{
  79. // winborborder.MouseMove += delegate(object sender, MouseEventArgs e)
  80. // {
  81. // if (e.LeftButton == MouseButtonState.Pressed)
  82. // {
  83. // this.DragMove();
  84. // }
  85. // };
  86. //}
  87. }
  88. /// <summary>
  89. /// 窗口圆角
  90. /// </summary>
  91. public CornerRadius CornerRadius
  92. {
  93. get { return (CornerRadius)GetValue(CornerRadiusProperty); }
  94. set { SetValue(CornerRadiusProperty, value); }
  95. }
  96. public static readonly DependencyProperty CornerRadiusProperty =
  97. DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(WindowBase), new PropertyMetadata(new CornerRadius(2)));
  98. public Brush TitleBackGround
  99. {
  100. get { return (Brush)GetValue(TitleBackGroundProperty); }
  101. set { SetValue(TitleBackGroundProperty, value); }
  102. }
  103. // Using a DependencyProperty as the backing store for TitleBackGround. This enables animation, styling, binding, etc...
  104. public static readonly DependencyProperty TitleBackGroundProperty =
  105. DependencyProperty.Register("TitleBackGround", typeof(Brush), typeof(WindowBase), new PropertyMetadata(Brushes.Bisque));
  106. public Brush TitleForeground
  107. {
  108. get { return (Brush)GetValue(TitleForegroundProperty); }
  109. set { SetValue(TitleForegroundProperty, value); }
  110. }
  111. // Using a DependencyProperty as the backing store for TitleFordBrush. This enables animation, styling, binding, etc...
  112. public static readonly DependencyProperty TitleForegroundProperty =
  113. DependencyProperty.Register("TitleForeground", typeof(Brush), typeof(WindowBase), new PropertyMetadata(Brushes.Black));
  114. /// <summary>
  115. /// 关闭按钮样式
  116. /// </summary>
  117. public Style CloseButtonStyle
  118. {
  119. get { return (Style)GetValue(CloseButtonStyleProperty); }
  120. set { SetValue(CloseButtonStyleProperty, value); }
  121. }
  122. // Using a DependencyProperty as the backing store for CloseButtonStyle. This enables animation, styling, binding, etc...
  123. public static readonly DependencyProperty CloseButtonStyleProperty =
  124. DependencyProperty.Register("CloseButtonStyle", typeof(Style), typeof(WindowBase), new PropertyMetadata(null));
  125. /// <summary>
  126. /// 最大最小化
  127. /// </summary>
  128. public Style MaxBtnStyle
  129. {
  130. get { return (Style)GetValue(MaxBtnStyleProperty); }
  131. set { SetValue(MaxBtnStyleProperty, value); }
  132. }
  133. // Using a DependencyProperty as the backing store for MaxStyle. This enables animation, styling, binding, etc...
  134. public static readonly DependencyProperty MaxBtnStyleProperty =
  135. DependencyProperty.Register("MaxBtnStyle", typeof(Style), typeof(WindowBase), new PropertyMetadata(null));
  136. /// <summary>
  137. /// 是否可最大化最小化
  138. /// </summary>
  139. public Visibility MaxBtnVisibility
  140. {
  141. get { return (Visibility)GetValue(MaxBtnVisibilityProperty); }
  142. set { SetValue(MaxBtnVisibilityProperty, value); }
  143. }
  144. // Using a DependencyProperty as the backing store for MaxBtnVisibility. This enables animation, styling, binding, etc...
  145. public static readonly DependencyProperty MaxBtnVisibilityProperty =
  146. DependencyProperty.Register("MaxBtnVisibility", typeof(Visibility), typeof(WindowBase), new PropertyMetadata(Visibility.Collapsed));
  147. public ICommand ClosedCommand
  148. {
  149. get { return (ICommand)GetValue(ClosedCommandProperty); }
  150. set { SetValue(ClosedCommandProperty, value); }
  151. }
  152. // Using a DependencyProperty as the backing store for ClosedCommand. This enables animation, styling, binding, etc...
  153. public static readonly DependencyProperty ClosedCommandProperty =
  154. DependencyProperty.Register("ClosedCommand", typeof(ICommand), typeof(WindowBase), new PropertyMetadata(null));
  155. protected override void OnClosed(EventArgs e)
  156. {
  157. if (ClosedCommand != null)
  158. {
  159. ClosedCommand.Execute(this);
  160. }
  161. base.OnClosed(e);
  162. }
  163. }
  164. }