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
#region Imports
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Forms
{
    #region NightForm
 
    public class NightForm : ContainerControl
    {
        #region Fields
 
        private readonly int draggableHeight;
        private bool isBeingDragged;
        private Point mouseLocation;
 
        private Rectangle titleBarRect;
        private int titleBarStringLeft;
 
        #endregion
 
        #region Enum
 
        public enum Alignment
        {
            Left,
            Center
        }
 
        #endregion
 
        #region Custom Properties
 
        private bool _ControlMode;
        protected bool ControlMode
        {
            get => _ControlMode;
            set
            {
                _ControlMode = value;
                Invalidate();
            }
        }
 
        private Alignment _TextAlignment = Alignment.Left;
        [Browsable(true)]
        [Description("Indicates how the window title should be aligned.")]
        public Alignment TextAlignment
        {
            get => _TextAlignment;
            set
            {
                _TextAlignment = value;
                Invalidate();
            }
        }
 
        private bool _DrawIcon;
        [Browsable(true)]
        [Description("Determines whether the icon specified in the parent form should be drawn.")]
        public bool DrawIcon
        {
            get => _DrawIcon;
            set
            {
                _DrawIcon = value;
                Invalidate();
            }
        }
 
        private Color _TitleBarTextColor = Color.Gainsboro;
        [Browsable(true)]
        [Description("Sets the title bar title color.")]
        public Color TitleBarTextColor
        {
            get => _TitleBarTextColor;
            set
            {
                _TitleBarTextColor = value;
                Invalidate();
            }
        }
 
        private Color _HeadColor = ColorTranslator.FromHtml("#323A3D");
        [Browsable(true)]
        [Description("Sets the title bar color.")]
        public Color HeadColor
        {
            get => _HeadColor;
            set
            {
                _HeadColor = value;
                Invalidate();
            }
        }
 
        #endregion
 
        #region Hidden Properties
 
        [Bindable(false), EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new ImageLayout BackgroundImageLayout { get; set; }
 
        [Bindable(false), EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Image BackgroundImage { get; set; }
 
        #endregion
 
        #region Functions
 
        /// <summary>
        /// Returns true if the mouse is over the title bar icon.
        /// </summary>
        private static bool IsOverTitleBarIcon(MouseEventArgs e)
        {
            bool point = e.X > 8 && e.X < 26 && e.Y > 6 && e.Y < 22;
            return point;
        }
 
        #endregion
 
        #region EventArgs
 
        protected override void OnSizeChanged(EventArgs e)
        {
            if (!ControlMode)
            {
                titleBarRect = new(9, 2, Width, draggableHeight);
            }
 
            base.OnSizeChanged(e);
        }
 
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            Invalidate();
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }
 
            if (titleBarRect.Contains(e.Location))
            {
                isBeingDragged = true;
                mouseLocation = e.Location;
            }
 
            base.OnMouseDown(e);
        }
 
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }
 
            // Close when double-clicking on the title bar icon
            if (_DrawIcon && IsOverTitleBarIcon(e))
            {
                Application.Exit();
            }
 
            base.OnMouseDoubleClick(e);
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (isBeingDragged)
            {
                Parent.Location = Point.Subtract(MousePosition, (Size)mouseLocation);
            }
 
            base.OnMouseMove(e);
        }
 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (isBeingDragged)
            {
                isBeingDragged = false;
            }
 
            base.OnMouseUp(e);
        }
 
        #endregion
 
        public NightForm()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw, true);
 
            DoubleBuffered = true;
            Dock = DockStyle.Fill;
            Padding = new Padding(0, 31, 0, 0);
            MinimumSize = new(100, 42);
 
            BackColor = Color.FromArgb(40, 48, 51);
 
            Font = new("Segoe UI", 9);
 
            draggableHeight = 28;
        }
 
        protected override void CreateHandle()
        {
            base.CreateHandle();
 
            ParentForm.FormBorderStyle = FormBorderStyle.None;
            ParentForm.TransparencyKey = Color.Fuchsia; // IMPORTANT!
            ParentForm.BackColor = SystemColors.ControlDarkDark;
            ParentForm.MaximumSize = Screen.FromRectangle(ParentForm.Bounds).WorkingArea.Size;
 
            ParentForm.StartPosition = FormStartPosition.CenterScreen;
        }
 
        private void DrawTitleBar(Graphics g)
        {
            using SolidBrush brush = new(HeadColor);
            g.FillRectangle(brush, new Rectangle(0, 0, Width, 31));
 
            // ========== FOR TESTING PURPOSES ONLY! ==========
            // PLACEMENT BACKGROUNDS FOR THE CONTROLBOX BUTTONS
            // ================================================
            //          using (var brush = new(ColorTranslator.FromHtml("#FF0000")))
            //              g.FillRectangle(brush, new Rectangle(Width - 46, 0, 46, 31));
            //          
            //          using (var brush = new(ColorTranslator.FromHtml("#00FF00")))
            //              g.FillRectangle(brush, new Rectangle(Width - 92, 0, 46, 31));
            //          
            //          using (var brush = new(ColorTranslator.FromHtml("#0000FF")))
            //              g.FillRectangle(brush, new Rectangle(Width - 138, 0, 46, 31));
        }
 
        private void DrawTitleBarIcon(Graphics g)
        {
            if (_DrawIcon)
            {
                titleBarStringLeft = _TextAlignment == Alignment.Left ? 33 : 5;
                Rectangle iconRect = new(10, 7, 16, 16);
 
                g.DrawIcon(FindForm().Icon, iconRect);
            }
            else
            {
                titleBarStringLeft = 5;
            }
        }
 
        private void DrawTitleBarText(Graphics g)
        {
            Rectangle stringRect = new(titleBarStringLeft, 7, Width - 13, Height);
 
            switch (_TextAlignment)
            {
                case Alignment.Left:
                    using (SolidBrush stringColor = new(_TitleBarTextColor))
                    using (StringFormat sf = new()
                    {
                        Alignment = StringAlignment.Near,
                        LineAlignment = StringAlignment.Near
                    })
                    {
                        g.DrawString(Text, Font, stringColor, stringRect, sf);
                    }
                    break;
                case Alignment.Center:
                    using (SolidBrush stringColor = new(_TitleBarTextColor))
                    using (StringFormat sf = new()
                    {
                        Alignment = StringAlignment.Center,
                        LineAlignment = StringAlignment.Near
                    })
                    {
                        g.DrawString(Text, Font, stringColor, stringRect, sf);
                    }
                    break;
            }
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.Clear(BackColor);
 
            DrawTitleBar(g);
            DrawTitleBarIcon(g);
            DrawTitleBarText(g);
 
            base.OnPaint(e);
        }
    }
 
    #endregion
}