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
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
#region Imports
 
using System;
using System.Drawing;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region ExtendedPanel
 
    public class ExtendedPanel : System.Windows.Forms.Panel
    {
        private const int WS_EX_TRANSPARENT = 0x20;
        private readonly Timer Most = new()
        {
            Interval = 100
        };
 
        public enum Drawer
        {
            Default,
            Image,
            Debug
        }
 
        private Drawer _DrawMode = Drawer.Default;
        public Drawer DrawMode
        {
            get => _DrawMode;
            set
            {
                if (value == Drawer.Image)
                {
                    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                    SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                    SetStyle(ControlStyles.SupportsTransparentBackColor, true);
                }
                else
                {
                    SetStyle(ControlStyles.AllPaintingInWmPaint, false);
                    SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
                    SetStyle(ControlStyles.SupportsTransparentBackColor, false);
                }
 
                _DrawMode = value;
                Invalidate();
            }
        }
 
        private bool _TopMost = true;
        public bool TopMost
        {
            get => _TopMost;
            set
            {
                _TopMost = value;
                Invalidate();
            }
        }
 
        private int _Opacity = 50;
        public int Opacity
        {
            get => _Opacity;
            set
            {
                if (value is < 0 or > 100)
                {
                    value = 0;
                }
 
                _Opacity = value;
                Invalidate();
            }
        }
 
        public int MostInterval
        {
            get => Most.Interval;
            set
            {
                Most.Interval = value;
                Invalidate();
            }
        }
 
        public ExtendedPanel()
        {
            SetStyle(ControlStyles.Opaque, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            BackColor = Color.Transparent;
        }
 
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams CP = base.CreateParams;
                CP.ExStyle |= WS_EX_TRANSPARENT;
                CP.Style &= ~0x04000000; //WS_CLIPSIBLINGS
                CP.Style &= ~0x02000000; //WS_CLIPCHILDREN
                return CP;
            }
        }
 
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0xF)
            {
                foreach (Control C in Controls) { C.Invalidate(); C.Update(); BringToFront(); }
            }
 
            FindForm().Controls.SetChildIndex(this, 0);
            UpdateZOrder();
 
            base.WndProc(ref m);
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            switch (DrawMode)
            {
                case Drawer.Default:
                    using (SolidBrush Brush = new(Color.FromArgb(Opacity * 255 / 100, BackColor)))
                    {
                        e.Graphics.FillRectangle(Brush, ClientRectangle);
                    }
 
                    break;
                case Drawer.Image:
                    e.Graphics.Clear(BackColor);
                    e.Graphics.CopyFromScreen(PointToScreen(new Point(0, 0)), new Point(0, 0), new Size(Width, Height));
                    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(Opacity * 255 / 100, BackColor)), new Rectangle(new Point(0, 0), Size));
                    break;
                default:
                    break;
            }
 
            if (TopMost && !Most.Enabled)
            {
                Most.Tick += new EventHandler(Most_Tick);
                Most.Start();
            }
            else if (!TopMost && Most.Enabled)
            {
                Most.Stop();
            }
 
            base.OnPaint(e);
        }
 
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Invalidate();
            base.OnPaintBackground(e);
        }
 
        protected override void OnBackColorChanged(EventArgs e)
        {
            if (Parent != null)
            {
                Parent.Invalidate(Bounds, true);
            }
 
            base.OnBackColorChanged(e);
        }
 
        protected override void OnParentBackColorChanged(EventArgs e)
        {
            Invalidate();
            base.OnParentBackColorChanged(e);
        }
 
        private void Most_Tick(object sender, EventArgs e)
        {
            switch (DrawMode)
            {
                case Drawer.Default:
                    Invalidate();
                    //PaintHelperA.Resume(this);
                    //PaintHelperB.Suspend(this);
                    break;
                case Drawer.Image:
                    //BackColor = Color.FromArgb(Opacity * 255 / 100, BackColor);
                    Graphics Graph = CreateGraphics();
                    Graph.CopyFromScreen(PointToScreen(new Point(0, 0)), new Point(0, 0), new Size(Width, Height));
                    Graph.FillRectangle(new SolidBrush(Color.FromArgb(Opacity * 255 / 100, BackColor)), new Rectangle(new Point(0, 0), Size));
                    break;
                case Drawer.Debug:
                    foreach (Control CTRL in FindForm().Controls)
                    {
                        try
                        {
                            ExtendedPanel EP = CTRL as ExtendedPanel;
                            EP.BringToFront();
                            FindForm().Controls.SetChildIndex(CTRL, 0);
                            EP.UpdateZOrder();
                            EP.Invalidate();
                        }
                        catch
                        {
                            //
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
 
    #endregion
}