tangxu
2024-10-22 7a0d1efa4784d176a32f182ecae671711e98ca92
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Colors;
using DPumpHydr.WinFrmUI.RLT.Controls;
using DPumpHydr.WinFrmUI.RLT.Util;
using System;
using System.Drawing;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Forms
{
    #region RoyalForm
 
    public class RoyalForm : Form
    {
        private RoyalButton maximizeButton;
        private RoyalButton minimizeButton;
        private RoyalButton closeButton;
        private const int WM_NCHITTEST = 0x0084;
        private const int HTCLIENT = 0x01;
        private const int HTCAPTION = 0x02;
        private const int wmNcHitTest = 0x84;
        private const int htLeft = 10;
        private const int htRight = 11;
        private const int htTop = 12;
        private const int htTopLeft = 13;
        private const int htTopRight = 14;
        private const int htBottom = 15;
        private const int htBottomLeft = 16;
        private const int htBottomRight = 17;
 
        public bool DrawBorder { get; set; }
        public int BorderThickness { get; set; }
        public bool Moveable { get; set; } = true;
        public bool Sizable { get; set; } = true;
 
        public RoyalForm()
        {
            InitializeComponent();
 
            ForeColor = RoyalColors.ForeColor;
            BackColor = RoyalColors.BackColor;
 
            closeButton.BackColor = BackColor;
            closeButton.HotTrackColor = Color.Crimson;
            closeButton.PressedColor = Color.Firebrick;
            closeButton.Cursor = Cursors.Hand;
 
            maximizeButton.BackColor = BackColor;
            maximizeButton.HotTrackColor = RoyalColors.HotTrackColor;
            maximizeButton.PressedColor = RoyalColors.PressedBackColor;
            maximizeButton.Cursor = Cursors.Hand;
 
            minimizeButton.BackColor = BackColor;
            minimizeButton.HotTrackColor = RoyalColors.HotTrackColor;
            minimizeButton.PressedColor = RoyalColors.PressedBackColor;
            minimizeButton.Cursor = Cursors.Hand;
 
            DrawBorder = false;
            BorderThickness = 1;
            Moveable = true;
            StartPosition = FormStartPosition.CenterScreen;
            MinimumSize = new(250, 250);
        }
 
        protected override void OnResize(EventArgs e)
        {
            closeButton.Location = new(Width - 34, 1);
            maximizeButton.Location = new(Width - 68, 1);
            if (MaximizeBox)
            {
                minimizeButton.Location = new(Width - 102, 1);
            }
            else
            {
                minimizeButton.Location = new(Width - 68, 1);
            }
 
            base.OnResize(e);
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
 
            if (DrawBorder)
            {
                e.Graphics.DrawRectangle(new(RoyalColors.BorderColor, BorderThickness), new Rectangle(0, 0, Width - BorderThickness, Height - BorderThickness));
            }
        }
 
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
 
            if (Sizable && m.Msg == wmNcHitTest && WindowState != FormWindowState.Maximized)
            {
                int gripDist = 10;
                //int x = (int)(m.LParam.ToInt64() & 0xFFFF);
                //int x = Cursor.Position.X;
                // int y = (int)((m.LParam.ToInt64() & 0xFFFF0000) >> 16);
                //Console.WriteLine(x);
                Point pt = PointToClient(Cursor.Position);
                //Console.WriteLine(pt);
                Size clientSize = ClientSize;
                ///allow resize on the lower right corner
                if (pt.X >= clientSize.Width - gripDist && pt.Y >= clientSize.Height - gripDist && clientSize.Height >= gripDist)
                {
                    m.Result = (IntPtr)(IsMirrored ? htBottomLeft : htBottomRight);
                    return;
                }
                ///allow resize on the lower left corner
                if (pt.X <= gripDist && pt.Y >= clientSize.Height - gripDist && clientSize.Height >= gripDist)
                {
                    m.Result = (IntPtr)(IsMirrored ? htBottomRight : htBottomLeft);
                    return;
                }
                ///allow resize on the upper right corner
                if (pt.X <= gripDist && pt.Y <= gripDist && clientSize.Height >= gripDist)
                {
                    m.Result = (IntPtr)(IsMirrored ? htTopRight : htTopLeft);
                    return;
                }
                ///allow resize on the upper left corner
                if (pt.X >= clientSize.Width - gripDist && pt.Y <= gripDist && clientSize.Height >= gripDist)
                {
                    m.Result = (IntPtr)(IsMirrored ? htTopLeft : htTopRight);
                    return;
                }
                ///allow resize on the top border
                if (pt.Y <= 2 && clientSize.Height >= 2)
                {
                    m.Result = (IntPtr)htTop;
                    return;
                }
                ///allow resize on the bottom border
                if (pt.Y >= clientSize.Height - gripDist && clientSize.Height >= gripDist)
                {
                    m.Result = (IntPtr)htBottom;
                    return;
                }
                ///allow resize on the left border
                if (pt.X <= gripDist && clientSize.Height >= gripDist)
                {
                    m.Result = (IntPtr)htLeft;
                    return;
                }
                ///allow resize on the right border
                if (pt.X >= clientSize.Width - gripDist && clientSize.Height >= gripDist)
                {
                    m.Result = (IntPtr)htRight;
                    return;
                }
            }
 
            if (m.Msg == WM_NCHITTEST)
            {
                if (Moveable)
                {
                    if ((int)m.Result == HTCLIENT)
                    {
                        m.Result = new IntPtr(HTCAPTION);
                    }
                }
            }
 
            if (ControlBox == false)
            {
                closeButton.Hide();
                minimizeButton.Hide();
                maximizeButton.Hide();
            }
            else if (Visible && ControlBox == true && !closeButton.Visible)
            {
                closeButton.Show();
 
                if (MinimizeBox)
                {
                    minimizeButton.Show();
                }
 
                if (MaximizeBox)
                {
                    maximizeButton.Show();
                }
            }
 
            if (Visible && !MinimizeBox && minimizeButton.Visible)
            {
                minimizeButton.Hide();
            }
            else if (Visible && MinimizeBox && !minimizeButton.Visible && ControlBox)
            {
                minimizeButton.Show();
            }
 
            if (Visible && !MaximizeBox && maximizeButton.Visible)
            {
                maximizeButton.Hide();
            }
            else if (Visible && MaximizeBox && !maximizeButton.Visible && ControlBox)
            {
                maximizeButton.Show();
            }
        }
 
        private void InitializeComponent()
        {
            minimizeButton = new RoyalButton();
            maximizeButton = new RoyalButton();
            closeButton = new RoyalButton();
            SuspendLayout();
            // 
            // minimizeButton
            // 
            minimizeButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            minimizeButton.BackColor = BackColor;
            minimizeButton.BorderColor = Color.FromArgb(180, 180, 180);
            minimizeButton.BorderThickness = 3;
            minimizeButton.DrawBorder = false;
            minimizeButton.HotTrackColor = Color.Gainsboro;
            minimizeButton.Image = Properties.Resources.Minimize;
            minimizeButton.LayoutFlags = RoyalLayoutFlags.ImageOnly;
            minimizeButton.Location = new(900, 1);
            minimizeButton.Margin = new Padding(1);
            minimizeButton.Name = "minimizeButton";
            minimizeButton.PressedColor = Color.CornflowerBlue;
            minimizeButton.PressedForeColor = Color.White;
            minimizeButton.Size = new(33, 30);
            minimizeButton.TabIndex = 2;
            minimizeButton.Text = "minimizeButton";
            minimizeButton.Click += new EventHandler(MinimizeButton_Click);
            // 
            // maximizeButton
            // 
            maximizeButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            maximizeButton.BackColor = BackColor;
            maximizeButton.BorderColor = Color.FromArgb(180, 180, 180);
            maximizeButton.BorderThickness = 3;
            maximizeButton.DrawBorder = false;
            maximizeButton.HotTrackColor = Color.Gainsboro;
            maximizeButton.Image = Properties.Resources.Maximize;
            maximizeButton.LayoutFlags = RoyalLayoutFlags.ImageOnly;
            maximizeButton.Location = new(935, 1);
            maximizeButton.Margin = new Padding(1);
            maximizeButton.Name = "maximizeButton";
            maximizeButton.PressedColor = Color.CornflowerBlue;
            maximizeButton.PressedForeColor = Color.White;
            maximizeButton.Size = new(33, 30);
            maximizeButton.TabIndex = 1;
            maximizeButton.Text = "maximizeButton";
            maximizeButton.Click += new EventHandler(MaximizeButton_Click);
            // 
            // closeButton
            // 
            closeButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            closeButton.BackColor = BackColor;
            closeButton.BorderColor = Color.FromArgb(180, 180, 180);
            closeButton.BorderThickness = 3;
            closeButton.DrawBorder = false;
            closeButton.HotTrackColor = Color.Gainsboro;
            closeButton.Image = Properties.Resources.Close;
            closeButton.LayoutFlags = RoyalLayoutFlags.ImageOnly;
            closeButton.Location = new(970, 1);
            closeButton.Margin = new Padding(1);
            closeButton.Name = "closeButton";
            closeButton.PressedColor = Color.Crimson;
            maximizeButton.PressedForeColor = Color.White;
            closeButton.Size = new(33, 30);
            closeButton.TabIndex = 0;
            closeButton.Text = "closeButton";
            closeButton.Click += new EventHandler(CloseButton_Click);
            // 
            // RoyalForm
            // 
            FormBorderStyle = FormBorderStyle.None;
            BackColor = Color.WhiteSmoke;
            ClientSize = new(512, 512);
            Controls.Add(minimizeButton);
            Controls.Add(maximizeButton);
            Controls.Add(closeButton);
            Name = "RoyalForm";
            Text = "RoyalForm";
            ResumeLayout(false);
        }
 
        private void CloseButton_Click(object sender, EventArgs e)
        {
            Close();
        }
 
        private void MaximizeButton_Click(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Maximized)
            {
                WindowState = FormWindowState.Normal;
            }
            else
            {
                WindowState = FormWindowState.Maximized;
            }
        }
 
        private void MinimizeButton_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }
    }
 
    #endregion
}