99using WPFDevelopers . Controls ;
1010using WPFDevelopers . Core . Helpers ;
1111using WPFDevelopers . Helpers ;
12- using static System . Windows . Forms . VisualStyles . VisualStyleElement . Window ;
1312
1413namespace WPFDevelopers . Net45x
1514{
@@ -32,12 +31,10 @@ public class Window : System.Windows.Window
3231 private Button _titleBarMaximizeButton ;
3332 private Button _titleBarRestoreButton ;
3433 private IntPtr hWnd ;
34+ private System . Windows . Shell . WindowChrome _windowChrome ;
3535
3636 public static readonly DependencyProperty TitleHeightProperty =
37- DependencyProperty . Register ( "TitleHeight" , typeof ( double ) , typeof ( Window ) , new PropertyMetadata ( 50d ) ) ;
38-
39- public static readonly DependencyProperty NoChromeProperty =
40- DependencyProperty . Register ( "NoChrome" , typeof ( bool ) , typeof ( Window ) , new PropertyMetadata ( false ) ) ;
37+ DependencyProperty . Register ( "TitleHeight" , typeof ( double ) , typeof ( Window ) , new PropertyMetadata ( 50d , OnTitleHeightChanged ) ) ;
4138
4239 public static readonly DependencyProperty TitleBarProperty =
4340 DependencyProperty . Register ( "TitleBar" , typeof ( object ) , typeof ( Window ) , new PropertyMetadata ( null ) ) ;
@@ -46,7 +43,7 @@ public class Window : System.Windows.Window
4643 DependencyProperty . Register ( "TitleBackground" , typeof ( Brush ) , typeof ( Window ) , new PropertyMetadata ( null ) ) ;
4744
4845 public static readonly DependencyProperty TitleBarModeProperty =
49- DependencyProperty . Register ( "TitleBarMode" , typeof ( TitleBarMode ) , typeof ( Window ) , new PropertyMetadata ( TitleBarMode . Normal ) ) ;
46+ DependencyProperty . Register ( "TitleBarMode" , typeof ( TitleBarMode ) , typeof ( Window ) , new PropertyMetadata ( TitleBarMode . Normal , OnTitleBarModeChanged ) ) ;
5047
5148 static Window ( )
5249 {
@@ -64,6 +61,13 @@ public Window()
6461 CanMinimizeWindow ) ) ;
6562 CommandBindings . Add ( new CommandBinding ( SystemCommands . RestoreWindowCommand , RestoreWindow ,
6663 CanResizeWindow ) ) ;
64+ _windowChrome = new System . Windows . Shell . WindowChrome
65+ {
66+ CaptionHeight = TitleHeight ,
67+ GlassFrameThickness = new Thickness ( 0 , 0 , 0 , 0.1 ) ,
68+ UseAeroCaptionButtons = false
69+ } ;
70+ System . Windows . Shell . WindowChrome . SetWindowChrome ( this , _windowChrome ) ;
6771 }
6872
6973 private void Resources_ThemeChanged ( ThemeType currentTheme )
@@ -87,8 +91,29 @@ public override void OnApplyTemplate()
8791 _highTitleRestoreButton = GetTemplateChild ( HighTitleRestoreButton ) as Button ;
8892 _titleBarMaximizeButton = GetTemplateChild ( TitleBarMaximizeButton ) as Button ;
8993 _titleBarRestoreButton = GetTemplateChild ( TitleBarRestoreButton ) as Button ;
94+ SetTitleHeight ( ) ;
95+ }
96+ private static void OnTitleBarModeChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
97+ {
98+ var ctrl = d as Window ;
99+ if ( ctrl != null )
100+ ctrl . SetTitleHeight ( ) ;
101+ }
102+ private static void OnTitleHeightChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
103+ {
104+ var ctrl = d as Window ;
105+ if ( ctrl != null )
106+ ctrl . _windowChrome . CaptionHeight = ctrl . TitleHeight ;
107+ }
108+ void SetTitleHeight ( )
109+ {
110+ if ( TitleBarMode == TitleBarMode . Normal )
111+ TitleHeight = SystemParameters . WindowNonClientFrameThickness . Top + SystemParameters . WindowResizeBorderThickness . Top ;
112+ else if ( TitleBarMode == TitleBarMode . High )
113+ TitleHeight = 50d ;
114+ else
115+ _windowChrome . CaptionHeight = TitleHeight ;
90116 }
91-
92117 private void Icon_MouseDoubleClick ( object sender , MouseButtonEventArgs e )
93118 {
94119 if ( e . ChangedButton == MouseButton . Left )
@@ -101,12 +126,6 @@ public double TitleHeight
101126 set => SetValue ( TitleHeightProperty , value ) ;
102127 }
103128
104- public bool NoChrome
105- {
106- get => ( bool ) GetValue ( NoChromeProperty ) ;
107- set => SetValue ( NoChromeProperty , value ) ;
108- }
109-
110129 public object TitleBar
111130 {
112131 get => ( object ) GetValue ( TitleBarProperty ) ;
@@ -137,8 +156,6 @@ protected override void OnSourceInitialized(EventArgs e)
137156 base . OnSourceInitialized ( e ) ;
138157 hWnd = new WindowInteropHelper ( this ) . Handle ;
139158 HwndSource . FromHwnd ( hWnd ) . AddHook ( WindowProc ) ;
140- if ( TitleBarMode == TitleBarMode . Normal )
141- TitleHeight = SystemParameters . WindowNonClientFrameThickness . Top + SystemParameters . WindowResizeBorderThickness . Top ;
142159 }
143160
144161 protected override void OnContentRendered ( EventArgs e )
@@ -167,12 +184,7 @@ private void CloseWindow(object sender, ExecutedRoutedEventArgs e)
167184
168185 private void MaximizeWindow ( object sender , ExecutedRoutedEventArgs e )
169186 {
170- if ( WindowState == WindowState . Normal )
171- {
172- WindowStyle = WindowStyle . SingleBorderWindow ;
173- WindowState = WindowState . Maximized ;
174- WindowStyle = WindowStyle . None ;
175- }
187+ SystemCommands . MaximizeWindow ( this ) ;
176188 }
177189
178190 private void MinimizeWindow ( object sender , ExecutedRoutedEventArgs e )
@@ -224,7 +236,7 @@ private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re
224236 var contentPresenter = button . Template . FindName ( "PART_ContentPresenter" , button ) as ContentPresenter ;
225237 if ( contentPresenter != null )
226238 {
227- var rect = new Rect ( button . PointToScreen ( new Point ( ) ) , new Size ( button . ActualWidth * dpiX , button . ActualHeight * dpiX ) ) ;
239+ var rect = new Rect ( button . PointToScreen ( new Point ( ) ) , new Size ( button . ActualWidth * dpiX , button . ActualHeight * dpiX ) ) ;
228240 if ( rect . Contains ( new Point ( x , y ) ) )
229241 {
230242 handled = true ;
0 commit comments