-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModernWindowFrame.cs
More file actions
158 lines (134 loc) · 4.55 KB
/
Copy pathModernWindowFrame.cs
File metadata and controls
158 lines (134 loc) · 4.55 KB
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
154
155
156
157
158
// Design-Rule / UI consistency:
// Keep layout, spacing, colors, sizes, and fonts aligned with ModernTheme.
// Add new shared visual values to ModernTheme instead of hardcoding local exceptions here.
// 03.05.2026 /dc
using System.Drawing;
using System.Windows.Forms;
namespace EasyVersionBackup
{
public static class ModernWindowFrame
{
public const int WmNclButtonDown = 0xA1;
public const int WmNcHitTest = 0x84;
public const int HtCaption = 0x2;
public static void Apply(Form form)
{
form.Paint += ModernWindowFrame_Paint;
form.Resize += ModernWindowFrame_Resize;
}
private static void ModernWindowFrame_Paint(object? sender, PaintEventArgs e)
{
if (sender is not Form form)
{
return;
}
using Pen borderPen = new Pen(ModernTheme.WindowBorderColor, ModernTheme.WindowBorderWidth);
e.Graphics.DrawRectangle(
borderPen,
0,
0,
form.ClientSize.Width - 1,
form.ClientSize.Height - 1);
}
public static bool TryGetResizeHitTestResult(Size clientSize, Point cursorPosition, out int hitTestResult)
{
const int htLeft = 10;
const int htRight = 11;
const int htTop = 12;
const int htTopLeft = 13;
const int htTopRight = 14;
const int htBottom = 15;
const int htBottomLeft = 16;
const int htBottomRight = 17;
int resizeBorderSize = Math.Max(8, SystemInformation.FrameBorderSize.Width + 4);
bool isLeft = cursorPosition.X <= resizeBorderSize;
bool isRight = cursorPosition.X >= clientSize.Width - resizeBorderSize;
bool isTop = cursorPosition.Y <= resizeBorderSize;
bool isBottom = cursorPosition.Y >= clientSize.Height - resizeBorderSize;
if (isTop && isLeft)
{
hitTestResult = htTopLeft;
return true;
}
if (isTop && isRight)
{
hitTestResult = htTopRight;
return true;
}
if (isBottom && isLeft)
{
hitTestResult = htBottomLeft;
return true;
}
if (isBottom && isRight)
{
hitTestResult = htBottomRight;
return true;
}
if (isTop)
{
hitTestResult = htTop;
return true;
}
if (isBottom)
{
hitTestResult = htBottom;
return true;
}
if (isLeft)
{
hitTestResult = htLeft;
return true;
}
if (isRight)
{
hitTestResult = htRight;
return true;
}
hitTestResult = 0;
return false;
}
public static Cursor GetResizeCursor(int hitTestResult)
{
const int htLeft = 10;
const int htRight = 11;
const int htTop = 12;
const int htTopLeft = 13;
const int htTopRight = 14;
const int htBottom = 15;
const int htBottomLeft = 16;
const int htBottomRight = 17;
return hitTestResult switch
{
htLeft or htRight => Cursors.SizeWE,
htTop or htBottom => Cursors.SizeNS,
htTopLeft or htBottomRight => Cursors.SizeNWSE,
htTopRight or htBottomLeft => Cursors.SizeNESW,
_ => Cursors.Default
};
}
public static bool HandleResizeHitTest(Form form, ref Message message)
{
if (message.Msg != WmNcHitTest)
{
return false;
}
Point cursorPosition = form.PointToClient(new Point(
unchecked((short)(long)message.LParam),
unchecked((short)((long)message.LParam >> 16))));
if (!TryGetResizeHitTestResult(form.ClientSize, cursorPosition, out int resizeHitTestResult))
{
return false;
}
message.Result = resizeHitTestResult;
return true;
}
private static void ModernWindowFrame_Resize(object? sender, System.EventArgs e)
{
if (sender is Form form)
{
form.Invalidate();
}
}
}
}