yangyin
2025-02-24 b3b65a002fe68b1383314098790f5a153b87765f
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
#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 ForeverButtonSticky
 
    public class ForeverButtonSticky : Control
    {
        private int W;
        private int H;
        private MouseStateForever State = MouseStateForever.None;
 
        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();
        }
 
        private bool[] GetConnectedSides()
        {
            bool[] Bool = new bool[4] { false, false, false, false };
 
            foreach (Control B in Parent.Controls)
            {
                if (B is ForeverButtonSticky)
                {
                    if (object.ReferenceEquals(B, this) || !Rect.IntersectsWith(Rect))
                    {
                        continue;
                    }
 
                    double A = Math.Atan2(Left - B.Left, Top - B.Top) * 2 / Math.PI;
                    if (A / 1 == A)
                    {
                        Bool[(int)A + 1] = true;
                    }
                }
            }
 
            return Bool;
        }
 
        private Rectangle Rect => new(Left, Top, Width, Height);
 
        [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 OnResize(EventArgs e)
        {
            base.OnResize(e);
            //Height = 32
        }
 
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            //Size = new(112, 32)
        }
 
        public ForeverButtonSticky()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
            DoubleBuffered = true;
            Size = new(160, 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;
            H = Height;
 
            GraphicsPath GP = new();
 
            bool[] GCS = GetConnectedSides();
            // dynamic RoundedBase = ForeverLibrary.RoundRect(0, 0, W, H, ???, !(GCS(2) | GCS(1)), !(GCS(1) | GCS(0)), !(GCS(3) | GCS(0)), !(GCS(3) | GCS(2)));
            GraphicsPath RoundedBase = ForeverLibrary.RoundRect(0, 0, W, H, 0.3, !(GCS[2] || GCS[1]), !(GCS[1] || GCS[0]), !(GCS[3] || GCS[0]), !(GCS[3] || GCS[2]));
            Rectangle Base = new(0, 0, W, H);
 
            Graphics _with17 = G;
            _with17.SmoothingMode = SmoothingMode.HighQuality;
            _with17.PixelOffsetMode = PixelOffsetMode.HighQuality;
            _with17.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            _with17.Clear(BackColor);
 
            switch (State)
            {
                case MouseStateForever.None:
                    if (Rounded)
                    {
                        //-- Base
                        GP = RoundedBase;
                        _with17.FillPath(new SolidBrush(BaseColor), GP);
 
                        //-- Text
                        _with17.DrawString(Text, Font, new SolidBrush(TextColor), Base, ForeverLibrary.CenterSF);
                    }
                    else
                    {
                        //-- Base
                        _with17.FillRectangle(new SolidBrush(BaseColor), Base);
 
                        //-- Text
                        _with17.DrawString(Text, Font, new SolidBrush(TextColor), Base, ForeverLibrary.CenterSF);
                    }
                    break;
                case MouseStateForever.Over:
                    if (Rounded)
                    {
                        //-- Base
                        GP = RoundedBase;
                        _with17.FillPath(new SolidBrush(BaseColor), GP);
                        _with17.FillPath(new SolidBrush(Color.FromArgb(20, Color.White)), GP);
 
                        //-- Text
                        _with17.DrawString(Text, Font, new SolidBrush(TextColor), Base, ForeverLibrary.CenterSF);
                    }
                    else
                    {
                        //-- Base
                        _with17.FillRectangle(new SolidBrush(BaseColor), Base);
                        _with17.FillRectangle(new SolidBrush(Color.FromArgb(20, Color.White)), Base);
 
                        //-- Text
                        _with17.DrawString(Text, Font, new SolidBrush(TextColor), Base, ForeverLibrary.CenterSF);
                    }
                    break;
                case MouseStateForever.Down:
                    if (Rounded)
                    {
                        //-- Base
                        GP = RoundedBase;
                        _with17.FillPath(new SolidBrush(BaseColor), GP);
                        _with17.FillPath(new SolidBrush(Color.FromArgb(20, Color.Black)), GP);
 
                        //-- Text
                        _with17.DrawString(Text, Font, new SolidBrush(TextColor), Base, ForeverLibrary.CenterSF);
                    }
                    else
                    {
                        //-- Base
                        _with17.FillRectangle(new SolidBrush(BaseColor), Base);
                        _with17.FillRectangle(new SolidBrush(Color.FromArgb(20, Color.Black)), Base);
 
                        //-- Text
                        _with17.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
}