-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSiraUIBorderless.cs
More file actions
235 lines (210 loc) · 7.85 KB
/
Copy pathSiraUIBorderless.cs
File metadata and controls
235 lines (210 loc) · 7.85 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;
namespace SiraUIPackage
{
public class SiraUIBorderless : Component
{
private Form _targetForm;
private bool _enable = true;
private bool _resizeForm = true;
private bool _dragForm = true;
private int _resizeBorder = 6;
private int _borderRadius = 12;
private bool _hasShadow = true;
private Color _shadowColor = Color.Black;
private bool _dockForm = true;
private Color _dockIndicatorColor = Color.White;
private int _dockIndicatorTransparency = 60;
[Category("SiraUI - Borderless")]
public bool Enable { get => _enable; set => _enable = value; }
[Category("SiraUI - Borderless")]
public bool ResizeForm { get => _resizeForm; set => _resizeForm = value; }
[Category("SiraUI - Borderless")]
public bool DragForm { get => _dragForm; set => _dragForm = value; }
[Category("SiraUI - Borderless")]
public int ResizeBorder { get => _resizeBorder; set => _resizeBorder = Math.Max(1, value); }
[Category("SiraUI - Borderless")]
public int BorderRadius { get => _borderRadius; set { _borderRadius = Math.Max(0, value); ApplyRadius(); } }
[Category("SiraUI - Shadow")]
public bool HasFormShadow { get => _hasShadow; set => _hasShadow = value; }
[Category("SiraUI - Shadow")]
public Color ShadowColor { get => _shadowColor; set => _shadowColor = value; }
[Category("SiraUI - Dock")]
public bool DockForm { get => _dockForm; set => _dockForm = value; }
[Category("SiraUI - Dock")]
public Color DockIndicatorColor { get => _dockIndicatorColor; set => _dockIndicatorColor = value; }
[Category("SiraUI - Dock")]
public int DockIndicatorTransparencyValue { get => _dockIndicatorTransparency; set => _dockIndicatorTransparency = Math.Max(0, Math.Min(100, value)); }
[Category("SiraUI - Borderless")]
public Form TargetForm
{
get => _targetForm;
set
{
if (_targetForm != null) Unsubscribe(_targetForm);
_targetForm = value;
if (_targetForm != null) Subscribe(_targetForm);
}
}
private void Subscribe(Form f)
{
f.Load += Form_Load;
f.Paint += Form_Paint;
f.MouseDown += Form_MouseDown;
f.MouseMove += Form_MouseMove;
f.MouseUp += Form_MouseUp;
f.FormClosing += Form_FormClosing;
f.Resize += Form_Resize;
f.FormBorderStyle = FormBorderStyle.None;
try
{
var prop = typeof(Control).GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
prop?.SetValue(f, true, null);
}
catch { }
ApplyRadius();
}
private void Unsubscribe(Form f)
{
try
{
f.Load -= Form_Load;
f.Paint -= Form_Paint;
f.MouseDown -= Form_MouseDown;
f.MouseMove -= Form_MouseMove;
f.MouseUp -= Form_MouseUp;
f.FormClosing -= Form_FormClosing;
f.Resize -= Form_Resize;
}
catch { }
}
private Point _dragOffset;
private bool _dragging = false;
private void Form_Load(object sender, EventArgs e)
{
ApplyRadius();
}
private void Form_Paint(object sender, PaintEventArgs e)
{
var f = sender as Form;
if (_borderRadius > 0)
{
using (GraphicsPath path = GetRoundedPath(new Rectangle(0, 0, f.Width, f.Height), _borderRadius))
{
f.Region = new Region(path);
using (Pen pen = new Pen(Color.FromArgb(60, f.ForeColor), 1))
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawPath(pen, path);
}
}
}
else
{
f.Region = null;
}
}
private void ApplyRadius()
{
if (_targetForm == null) return;
if (_borderRadius > 0)
{
using (GraphicsPath path = GetRoundedPath(new Rectangle(0, 0, _targetForm.Width, _targetForm.Height), _borderRadius))
{
_targetForm.Region = new Region(path);
}
}
else
{
_targetForm.Region = null;
}
}
private void Form_MouseDown(object sender, MouseEventArgs e)
{
if (!_enable || !_dragForm) return;
var f = sender as Form;
if (e.Button == MouseButtons.Left)
{
_dragging = true;
_dragOffset = new Point(e.X, e.Y);
}
}
private void Form_MouseMove(object sender, MouseEventArgs e)
{
if (!_enable) return;
var f = sender as Form;
if (_dragging && _dragForm && e.Button == MouseButtons.Left)
{
f.Location = new Point(f.Left + e.X - _dragOffset.X, f.Top + e.Y - _dragOffset.Y);
}
else if (_resizeForm)
{
var c = f.PointToClient(Cursor.Position);
var w = f.ClientSize.Width;
var h = f.ClientSize.Height;
int margin = _resizeBorder;
bool right = c.X >= w - margin;
bool bottom = c.Y >= h - margin;
bool left = c.X <= margin;
bool top = c.Y <= margin;
if (left || right || top || bottom)
{
Cursor.Current = Cursors.SizeAll;
}
else
{
Cursor.Current = Cursors.Default;
}
}
}
private void Form_MouseUp(object sender, MouseEventArgs e)
{
_dragging = false;
}
private void Form_Resize(object sender, EventArgs e)
{
ApplyRadius();
}
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
Unsubscribe((Form)sender);
}
public void ShowFormSafely()
{
if (_targetForm == null) return;
if (Application.OpenForms.Count > 0)
{
var owner = Application.OpenForms[0];
if (owner != _targetForm)
{
_targetForm.Show(owner);
return;
}
}
_targetForm.Show();
}
private GraphicsPath GetRoundedPath(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
if (radius <= 0)
{
path.AddRectangle(rect);
return path;
}
float r = radius * 2f;
float maxR = Math.Min(rect.Width, rect.Height);
if (r > maxR) r = maxR;
path.AddArc(rect.X, rect.Y, r, r, 180, 90);
path.AddArc(rect.Right - r, rect.Y, r, r, 270, 90);
path.AddArc(rect.Right - r, rect.Bottom - r, r, r, 0, 90);
path.AddArc(rect.X, rect.Bottom - r, r, r, 90, 90);
path.CloseFigure();
return path;
}
}
}