This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainWindow.xaml.cs
154 lines (144 loc) · 5.8 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Chrones;
namespace WPFCustomTitleBarDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
#region WINDOW
bool IsClosing = false;
bool CanClose = false;
private void Window_StateChanged(object sender, EventArgs e)
{
this.WindowControlButton_Maximize_Refresh();
}
private void WindowControlButton_Minimize_Click(object sender, EventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void WindowControlButton_Maximize_Click(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
}
else
{
this.WindowState = WindowState.Maximized;
}
}
private void WindowControlButton_Close_Click(object sender, EventArgs e)
{
this.Close();
}
private void WindowControlButton_Maximize_Refresh()
{
if (this.WindowState == WindowState.Maximized)
{
WindowBorderBorder.Visibility = Visibility.Hidden;
this.WindowControlButton_Maximize.Visibility = Visibility.Collapsed;
this.WindowControlButton_MaximizeRestore.Visibility = Visibility.Visible;
}
else
{
WindowBorderBorder.Visibility = Visibility.Visible;
this.WindowControlButton_Maximize.Visibility = Visibility.Visible;
this.WindowControlButton_MaximizeRestore.Visibility = Visibility.Collapsed;
}
}
private void CONSTRUCT()
{
WindowControlButton_Maximize_Refresh();
#region MaximizingFix
SourceInitialized += (s, e) =>
{
WindowCompositionTarget = PresentationSource.FromVisual(this).CompositionTarget;
System.Windows.Interop.HwndSource.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle).AddHook(WindowProc);
};
#endregion
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (CanClose == true)
{
return;
}
if (IsClosing == false)
{
var ta = new DoubleAnimation();
ta.Duration = TimeSpan.FromSeconds(0.2);
QuadraticEase EasingFunction = new QuadraticEase();
EasingFunction.EasingMode = EasingMode.EaseOut;
ta.EasingFunction = EasingFunction;
ta.To = 0;
ta.Completed += (object sender, EventArgs e) => { CanClose = true; this.Close(); };
this.BeginAnimation(OpacityProperty, ta);
IsClosing = true;
e.Cancel = true;
} else
{
e.Cancel = true;
}
}
#region MaximizingFix
CompositionTarget WindowCompositionTarget { get; set; }
double CachedMinWidth { get; set; }
double CachedMinHeight { get; set; }
Cmr.Win32.POINT CachedMinTrackSize { get; set; }
IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case 0x0024:
Cmr.Win32.MINMAXINFO mmi = (Cmr.Win32.MINMAXINFO)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, typeof(Cmr.Win32.MINMAXINFO));
IntPtr monitor = Cmr.Win32.MonitorFromWindow(hwnd, 0x00000002 /*MONITOR_DEFAULTTONEAREST*/);
if (monitor != IntPtr.Zero)
{
Cmr.Win32.MONITORINFO monitorInfo = new Cmr.Win32.MONITORINFO { };
Cmr.Win32.GetMonitorInfo(monitor, monitorInfo);
Cmr.Win32.RECT rcWorkArea = monitorInfo.rcWork;
Cmr.Win32.RECT rcMonitorArea = monitorInfo.rcMonitor;
mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
if (!CachedMinTrackSize.Equals(mmi.ptMinTrackSize) || CachedMinHeight != MinHeight && CachedMinWidth != MinWidth)
{
mmi.ptMinTrackSize.x = (int)((CachedMinWidth = MinWidth) * WindowCompositionTarget.TransformToDevice.M11);
mmi.ptMinTrackSize.y = (int)((CachedMinHeight = MinHeight) * WindowCompositionTarget.TransformToDevice.M22);
CachedMinTrackSize = mmi.ptMinTrackSize;
}
}
System.Runtime.InteropServices.Marshal.StructureToPtr(mmi, lParam, true);
handled = true;
break;
}
return IntPtr.Zero;
}
#endregion
#endregion WINDOW
public MainWindow()
{
InitializeComponent();
CONSTRUCT();
}
}
}