2021-08-06 02:19:04 -07:00
|
|
|
using System;
|
2021-06-17 18:26:19 -07:00
|
|
|
using System.Windows;
|
2021-08-06 02:19:04 -07:00
|
|
|
using System.Windows.Documents;
|
|
|
|
using System.Collections.Generic;
|
2021-06-17 18:26:19 -07:00
|
|
|
|
|
|
|
namespace SourceGit.Views.Controls {
|
|
|
|
/// <summary>
|
|
|
|
/// 项目使用的窗体基类
|
|
|
|
/// </summary>
|
|
|
|
public class Window : System.Windows.Window {
|
|
|
|
|
2021-06-21 19:09:50 -07:00
|
|
|
public static readonly DependencyProperty IsMaximizedProperty = DependencyProperty.Register(
|
|
|
|
"IsMaximized",
|
|
|
|
typeof(bool),
|
|
|
|
typeof(Window),
|
|
|
|
new PropertyMetadata(false, OnIsMaximizedChanged));
|
|
|
|
|
|
|
|
public bool IsMaximized {
|
|
|
|
get { return (bool)GetValue(IsMaximizedProperty); }
|
|
|
|
set { SetValue(IsMaximizedProperty, value); }
|
|
|
|
}
|
|
|
|
|
2021-08-06 02:19:04 -07:00
|
|
|
private AdornerLayer adornerLayer = null;
|
|
|
|
private List<Adorner> adorners = new List<Adorner>();
|
|
|
|
|
2021-06-17 18:26:19 -07:00
|
|
|
public Window() {
|
2021-07-20 01:26:10 -07:00
|
|
|
Style = FindResource("Style.Window") as Style;
|
2022-01-03 22:52:13 -08:00
|
|
|
|
|
|
|
Loaded += (_, __) => {
|
|
|
|
adornerLayer = AdornerLayer.GetAdornerLayer(Content as FrameworkElement);
|
|
|
|
OnStateChanged(null);
|
|
|
|
};
|
2021-08-06 02:19:04 -07:00
|
|
|
}
|
2021-06-17 18:26:19 -07:00
|
|
|
|
2021-08-06 05:04:10 -07:00
|
|
|
public static void AddAdorner(FrameworkElement windowContext, Adorner adorner) {
|
|
|
|
var wnd = GetWindow(windowContext) as Window;
|
2021-08-06 02:19:04 -07:00
|
|
|
if (wnd != null && wnd.adornerLayer != null) {
|
|
|
|
wnd.adorners.Add(adorner);
|
|
|
|
wnd.adornerLayer.Add(adorner);
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 18:26:19 -07:00
|
|
|
|
2021-08-06 05:04:10 -07:00
|
|
|
public static void RemoveAdorner(FrameworkElement windowContext, Adorner adorner) {
|
|
|
|
var wnd = GetWindow(windowContext) as Window;
|
2021-08-06 02:19:04 -07:00
|
|
|
if (wnd != null && wnd.adornerLayer != null) {
|
|
|
|
wnd.adorners.Remove(adorner);
|
|
|
|
wnd.adornerLayer.Remove(adorner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnPreviewGiveFeedback(GiveFeedbackEventArgs e) {
|
|
|
|
base.OnPreviewGiveFeedback(e);
|
|
|
|
if (adornerLayer != null && adorners.Count > 0) adornerLayer.Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnStateChanged(EventArgs e) {
|
|
|
|
if (WindowState == WindowState.Maximized) {
|
|
|
|
if (!IsMaximized) IsMaximized = true;
|
|
|
|
BorderThickness = new Thickness(0);
|
|
|
|
Padding = new Thickness((SystemParameters.MaximizedPrimaryScreenWidth - SystemParameters.WorkArea.Width) / 2);
|
|
|
|
} else {
|
|
|
|
if (IsMaximized) IsMaximized = false;
|
|
|
|
BorderThickness = new Thickness(1);
|
|
|
|
Padding = new Thickness(0);
|
|
|
|
}
|
2021-06-17 18:26:19 -07:00
|
|
|
}
|
2021-06-21 19:09:50 -07:00
|
|
|
|
|
|
|
private static void OnIsMaximizedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
|
|
|
|
Window w = d as Window;
|
|
|
|
if (w != null) {
|
|
|
|
if (w.IsMaximized) {
|
|
|
|
SystemCommands.MaximizeWindow(w);
|
2021-06-21 20:42:47 -07:00
|
|
|
} else if (w.WindowState != WindowState.Minimized) {
|
2021-06-21 19:09:50 -07:00
|
|
|
SystemCommands.RestoreWindow(w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 18:26:19 -07:00
|
|
|
}
|
|
|
|
}
|