tangxu
2024-12-24 91105b77c916d06dd30380e20594e29f85eae3da
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Colors;
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 ForeverButton
 
    public class ForeverButton : Control
    {
        private int W;
        private int H;
        private MouseStateForever State = MouseStateForever.None;
 
        [Category("Colors")]
        public Color BaseColor { get; set; } = ForeverLibrary.ForeverColor;
 
        [Category("Colors")]
        public Color TextColor { get; set; } = Color.FromArgb(243, 243, 243);
 
        [Category("Options")]
        public bool Rounded { get; set; } = false;
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            State = MouseStateForever.Down;
            Invalidate();
        }
 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            State = MouseStateForever.Over;
            Invalidate();
        }
 
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            State = MouseStateForever.Over;
            Invalidate();
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            State = MouseStateForever.None;
            Invalidate();
        }
 
        public ForeverButton()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
            DoubleBuffered = true;
            Size = new(120, 40);
            BackColor = Color.Transparent;
            Font = new("Segoe UI", 12);
            Cursor = Cursors.Hand;
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            //UpdateColors();
 
            Bitmap B = new(Width, Height);
            Graphics G = Graphics.FromImage(B);
            W = Width - 1;
            H = Height - 1;
 
            GraphicsPath GP = new();
            Rectangle Base = new(0, 0, W, H);
 
            Graphics _with8 = G;
            _with8.SmoothingMode = SmoothingMode.HighQuality;
            _with8.PixelOffsetMode = PixelOffsetMode.HighQuality;
            _with8.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            _with8.Clear(BackColor);
 
            switch (State)
            {
                case MouseStateForever.None:
                    if (Rounded)
                    {
                        //-- Base
                        GP = ForeverLibrary.RoundRec(Base, 6);
                        _with8.FillPath(new SolidBrush(BaseColor), GP);
 
                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(TextColor), Base, ForeverLibrary.CenterSF);
                    }
                    else
                    {
                        //-- Base
                        _with8.FillRectangle(new SolidBrush(BaseColor), Base);
 
                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(TextColor), Base, ForeverLibrary.CenterSF);
                    }
                    break;
                case MouseStateForever.Over:
                    if (Rounded)
                    {
                        //-- Base
                        GP = ForeverLibrary.RoundRec(Base, 6);
                        _with8.FillPath(new SolidBrush(BaseColor), GP);
                        _with8.FillPath(new SolidBrush(Color.FromArgb(20, Color.White)), GP);
 
                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(TextColor), Base, ForeverLibrary.CenterSF);
                    }
                    else
                    {
                        //-- Base
                        _with8.FillRectangle(new SolidBrush(BaseColor), Base);
                        _with8.FillRectangle(new SolidBrush(Color.FromArgb(20, Color.White)), Base);
 
                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(TextColor), Base, ForeverLibrary.CenterSF);
                    }
                    break;
                case MouseStateForever.Down:
                    if (Rounded)
                    {
                        //-- Base
                        GP = ForeverLibrary.RoundRec(Base, 6);
                        _with8.FillPath(new SolidBrush(BaseColor), GP);
                        _with8.FillPath(new SolidBrush(Color.FromArgb(20, Color.Black)), GP);
 
                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(TextColor), Base, ForeverLibrary.CenterSF);
                    }
                    else
                    {
                        //-- Base
                        _with8.FillRectangle(new SolidBrush(BaseColor), Base);
                        _with8.FillRectangle(new SolidBrush(Color.FromArgb(20, Color.Black)), Base);
 
                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(TextColor), Base, ForeverLibrary.CenterSF);
                    }
                    break;
            }
 
            base.OnPaint(e);
            G.Dispose();
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImageUnscaled(B, 0, 0);
            B.Dispose();
        }
 
        private void UpdateColors()
        {
            ForeverColors Colors = ForeverLibrary.GetColors(this);
 
            BaseColor = Colors.Forever;
        }
    }
 
    #endregion
}