tangxu
2024-11-04 ebd031e3bed6c1cfddce8fc9b98f7f9e95fb9e32
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
#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.Controls
{
    #region ForeverMaximize
 
    public class ForeverMaximize : Control
    {
        private MouseStateForever State = MouseStateForever.None;
        private int x;
 
        #region Properties
 
        private bool _DefaultLocation = true;
        public bool DefaultLocation
        {
            get => _DefaultLocation;
            set
            {
                _DefaultLocation = value;
                Invalidate();
            }
        }
 
        #endregion
 
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            try
            {
                if (DefaultLocation)
                {
                    Location = new(Parent.Width - Width - 36, 16);
                }
            }
            catch (Exception)
            {
                //
            }
        }
 
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            State = MouseStateForever.Over;
            Invalidate();
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            State = MouseStateForever.Down;
            Invalidate();
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            State = MouseStateForever.None;
            Invalidate();
        }
 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            State = MouseStateForever.Over;
            Invalidate();
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            x = e.X;
            Invalidate();
        }
 
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            switch (FindForm().WindowState)
            {
                case FormWindowState.Maximized:
                    FindForm().WindowState = FormWindowState.Normal;
                    break;
                case FormWindowState.Normal:
                    FindForm().WindowState = FormWindowState.Maximized;
                    break;
            }
        }
 
        [Category("Colors")]
        public Color BaseColor { get; set; } = Color.FromArgb(45, 47, 49);
 
        [Category("Colors")]
        public Color OverColor { get; set; } = Color.FromArgb(30, 255, 255, 255);
 
        [Category("Colors")]
        public Color DownColor { get; set; } = Color.FromArgb(30, 0, 0, 0);
 
        [Category("Colors")]
        public Color TextColor { get; set; } = Color.FromArgb(243, 243, 243);
 
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Size = new(18, 18);
        }
 
        public ForeverMaximize()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
            DoubleBuffered = true;
            BackColor = Color.White;
            Size = new(18, 18);
            Anchor = AnchorStyles.Top | AnchorStyles.Right;
            Font = new("Marlett", 12);
            Cursor = Cursors.Hand;
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Bitmap B = new(Width, Height);
            Graphics G = Graphics.FromImage(B);
 
            Rectangle Base = new(0, 0, Width, Height);
 
            Graphics _with4 = G;
            _with4.SmoothingMode = SmoothingMode.HighQuality;
            _with4.PixelOffsetMode = PixelOffsetMode.HighQuality;
            _with4.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            _with4.Clear(BackColor);
 
            //-- Base
            _with4.FillRectangle(new SolidBrush(BaseColor), Base);
 
            //-- Maximize
            if (FindForm().WindowState == FormWindowState.Maximized)
            {
                _with4.DrawString("2", Font, new SolidBrush(TextColor), new Rectangle(1, 1, Width, Height), ForeverLibrary.CenterSF);
            }
            else if (FindForm().WindowState == FormWindowState.Normal)
            {
                _with4.DrawString("1", Font, new SolidBrush(TextColor), new Rectangle(1, 1, Width, Height), ForeverLibrary.CenterSF);
            }
 
            //-- Hover/down
            switch (State)
            {
                case MouseStateForever.Over:
                    _with4.FillRectangle(new SolidBrush(OverColor), Base);
                    break;
                case MouseStateForever.Down:
                    _with4.FillRectangle(new SolidBrush(DownColor), Base);
                    break;
            }
 
            base.OnPaint(e);
            G.Dispose();
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImageUnscaled(B, 0, 0);
            B.Dispose();
        }
    }
 
    #endregion
}