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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Util;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Forms
{
    #region ForeverForm
 
    public class ForeverForm : ContainerControl
    {
        private int W;
        private int H;
        private bool Cap = false;
        private Point MousePoint = new(0, 0);
        private readonly int MoveHeight = 50;
 
        private Image _Image; //Properties.Resources.Taiizor
        private Size _ImageSize;
        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;
 
        [Category("Colors")]
        public Color HeaderColor { get; set; } = Color.FromArgb(45, 47, 49);
 
        [Category("Colors")]
        public Color BaseColor { get; set; } = Color.FromArgb(60, 70, 73);
 
        [Category("Colors")]
        public Color BorderColor { get; set; } = Color.DodgerBlue;
 
        [Category("Colors")]
        public Color TextColor { get; set; } = Color.FromArgb(234, 234, 234);
 
        [Category("Colors")]
        public Color TextLight
        {
            get => _TextLight;
            set => _TextLight = value;
        }
 
        [Category("Colors")]
        public Color ForeverColor
        {
            // get { return ForeverLibrary.ForeverColor; }
            // set { ForeverLibrary.ForeverColor = value; }
            get; set;
        } = ForeverLibrary.ForeverColor;
 
        [Category("Options")]
        public bool HeaderMaximize { get; set; } = false;
 
        [Category("Options")]
        public Image Image
        {
            get => _Image;
            set
            {
                if (value == null)
                {
                    _ImageSize = Size.Empty;
                }
                else
                {
                    _ImageSize = value.Size;
                }
 
                _Image = value;
            }
        }
 
        [Category("Options")]
        public bool Sizable { get; set; } = true;
 
        [Category("Options")]
        public Font HeaderTextFont { get; set; } = new("Segoe UI", 12);
 
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
 
            if (Sizable && m.Msg == wmNcHitTest && FindForm().WindowState != FormWindowState.Maximized)
            {
                int gripDist = 10;
 
                //int x = (int)(m.LParam.ToInt64() & 0xFFFF);
                //int x = Cursor.Position.X;
                //Console.WriteLine(x);
 
                // int y = (int)((m.LParam.ToInt64() & 0xFFFF0000) >> 16);
                //Console.WriteLine(y);
 
                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;
                }
            }
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left & new Rectangle(0, 0, Width, MoveHeight).Contains(e.Location))
            {
                Cap = true;
                MousePoint = e.Location;
            }
        }
 
        private void ForeverForm_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (HeaderMaximize)
            {
                if (e.Button == MouseButtons.Left & new Rectangle(0, 0, Width, MoveHeight).Contains(e.Location))
                {
                    if (FindForm().WindowState == FormWindowState.Normal)
                    {
                        FindForm().WindowState = FormWindowState.Maximized;
                        FindForm().Refresh();
                    }
                    else if (FindForm().WindowState == FormWindowState.Maximized)
                    {
                        FindForm().WindowState = FormWindowState.Normal;
                        FindForm().Refresh();
                    }
                }
            }
        }
 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            Cap = false;
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
 
            if (Cap)
            {
                // Parent.Location = MousePosition - MousePoint;
                Parent.Location = new(MousePosition.X - MousePoint.X, MousePosition.Y - MousePoint.Y);
            }
        }
 
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
 
            ParentForm.AllowTransparency = false;
            ParentForm.TransparencyKey = Color.Fuchsia;
            ParentForm.FormBorderStyle = FormBorderStyle.None;
            ParentForm.FindForm().StartPosition = FormStartPosition.CenterScreen;
 
            Dock = DockStyle.Fill;
 
            Invalidate();
        }
 
        private readonly Color _HeaderLight = Color.FromArgb(171, 171, 172);
        private readonly Color _BaseLight = Color.FromArgb(196, 199, 200);
        public Color _TextLight = Color.SeaGreen;
 
        public ForeverForm()
        {
            MouseDoubleClick += ForeverForm_MouseDoubleClick;
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
            DoubleBuffered = true;
            BackColor = Color.White;
            Font = new("Segoe UI", 12);
            MinimumSize = new(210, 50);
            Padding = new Padding(1, 51, 1, 1);
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Bitmap B = new(Width, Height);
            Graphics G = Graphics.FromImage(B);
 
            W = Width;
            H = Height;
 
            Rectangle Base = new(0, 0, W, H);
            Rectangle Header = new(0, 0, W, 50);
 
            Graphics _with2 = G;
            _with2.SmoothingMode = SmoothingMode.HighQuality;
            _with2.PixelOffsetMode = PixelOffsetMode.HighQuality;
            _with2.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            _with2.Clear(BackColor);
 
            //-- Base
            _with2.FillRectangle(new SolidBrush(BaseColor), Base);
 
            //-- Header
            _with2.FillRectangle(new SolidBrush(HeaderColor), Header);
 
            //-- Logo
            if (_Image == null)
            {
                _with2.FillRectangle(new SolidBrush(_TextLight), new Rectangle(8, 16, 4, 18));
                _with2.FillRectangle(new SolidBrush(ForeverColor), 16, 16, 4, 18);
                _with2.DrawString(Text, HeaderTextFont, new SolidBrush(TextColor), new Rectangle(26, 15, W, H), ForeverLibrary.NearSF);
            }
            else
            {
                _with2.DrawImage(_Image, 12, 12, 27, 27);
                _with2.DrawString(Text, HeaderTextFont, new SolidBrush(TextColor), new Rectangle(48, 15, W, H), ForeverLibrary.NearSF);
            }
 
            //-- Border
            _with2.DrawRectangle(new(BorderColor), Base);
 
            base.OnPaint(e);
 
            G.Dispose();
 
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImageUnscaled(B, 0, 0);
 
            B.Dispose();
        }
    }
 
    #endregion
}