yangyin
2025-03-27 b0de14c2670b9ff0079dacfb4b7457b438368f11
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Design.Metro;
using DPumpHydr.WinFrmUI.RLT.Enum.Metro;
using DPumpHydr.WinFrmUI.RLT.Extension.Metro;
using DPumpHydr.WinFrmUI.RLT.Interface.Metro;
using DPumpHydr.WinFrmUI.RLT.Manager;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region MetroToolTip
 
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(MetroToolTip), "Bitmaps.ToolTip.bmp")]
    [Designer(typeof(MetroToolTipDesigner))]
    [DefaultEvent("Popup")]
    public class MetroToolTip : ToolTip, IMetroControl
    {
        #region Interfaces
 
        [Category("Metro"), Description("Gets or sets the style associated with the control.")]
        public Style Style
        {
            get => StyleManager?.Style ?? _style;
            set
            {
                _style = value;
                switch (value)
                {
                    case Style.Light:
                        ApplyTheme();
                        break;
                    case Style.Dark:
                        ApplyTheme(Style.Dark);
                        break;
                    case Style.Custom:
                        ApplyTheme(Style.Custom);
                        break;
                    default:
                        ApplyTheme();
                        break;
                }
            }
        }
 
        [Category("Metro"), Description("Gets or sets the Style Manager associated with the control.")]
        public MetroStyleManager StyleManager { get; set; }
 
        [Category("Metro"), Description("Gets or sets the The Author name associated with the theme.")]
        public string ThemeAuthor { get; set; }
 
        [Category("Metro"), Description("Gets or sets the The Theme name associated with the theme.")]
        public string ThemeName { get; set; }
 
        #endregion Interfaces
 
        #region Global Vars
 
        private readonly Methods _mth;
        private readonly Utilites _utl;
 
        #endregion Global Vars
 
        #region Internal Vars
 
        private Style _style;
 
        #endregion Internal Vars
 
        #region Constructors
 
        public MetroToolTip()
        {
            OwnerDraw = true;
            Draw += OnDraw;
            Popup += ToolTip_Popup;
            _mth = new Methods();
            _utl = new Utilites();
            ApplyTheme();
        }
 
        #endregion Constructors
 
        #region Draw Control
 
        private void OnDraw(object sender, DrawToolTipEventArgs e)
        {
            Graphics g = e.Graphics;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            Rectangle rect = new(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1);
            using SolidBrush bg = new(BackColor);
            using Pen stroke = new(BorderColor);
            using SolidBrush tb = new(ForeColor);
            g.FillRectangle(bg, rect);
            g.DrawString(e.ToolTipText, MetroFonts.Light(11), tb, rect, _mth.SetPosition());
            g.DrawRectangle(stroke, rect);
        }
 
        #endregion
 
        #region ApplyTheme
 
        private void ApplyTheme(Style style = Style.Light)
        {
            if (!IsDerivedStyle)
            {
                return;
            }
 
            switch (style)
            {
                case Style.Light:
                    ForeColor = Color.FromArgb(170, 170, 170);
                    BackColor = Color.White;
                    BorderColor = Color.FromArgb(204, 204, 204);
                    ThemeAuthor = "Taiizor";
                    ThemeName = "MetroLight";
                    break;
                case Style.Dark:
                    ForeColor = Color.FromArgb(204, 204, 204);
                    BackColor = Color.FromArgb(32, 32, 32);
                    BorderColor = Color.FromArgb(64, 64, 64);
                    ThemeAuthor = "Taiizor";
                    ThemeName = "MetroDark";
                    break;
                case Style.Custom:
                    if (StyleManager != null)
                    {
                        foreach (System.Collections.Generic.KeyValuePair<string, object> varkey in StyleManager.ToolTipDictionary)
                        {
                            switch (varkey.Key)
                            {
                                case "BackColor":
                                    BackColor = _utl.HexColor((string)varkey.Value);
                                    break;
 
                                case "BorderColor":
                                    BorderColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "ForeColor":
                                    ForeColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                default:
                                    return;
                            }
                        }
                    }
 
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(style), style, null);
            }
        }
 
        #endregion ApplyTheme
 
        #region Properties
 
        [Browsable(false)]
        public new bool ShowAlways { get; } = false;
 
        [Browsable(false)]
        public new bool OwnerDraw
        {
            get => base.OwnerDraw;
            set => base.OwnerDraw = true;
        }
 
        [Browsable(false)]
        public new bool IsBalloon { get; } = false;
 
        [Browsable(false)]
        public new Color BackColor { get; set; }
 
        [Category("Metro"), Description("Gets or sets the foreground color for the ToolTip.")]
        public new Color ForeColor { get; set; }
 
        [Category("Metro"), Description("Gets or sets a title for the ToolTip window.")]
        public new string ToolTipTitle { get; } = string.Empty;
 
        [Browsable(false)]
        public new ToolTipIcon ToolTipIcon { get; } = ToolTipIcon.None;
 
        [Category("Metro"), Description("Gets or sets the border color for the ToolTip.")]
        public Color BorderColor { get; set; }
 
        [Category("Metro")]
        [Description("Gets or sets the whether this control reflect to parent(s) style. \n " +
                     "Set it to false if you want the style of this control be independent. ")]
        public bool IsDerivedStyle { get; set; } = true;
 
        #endregion
 
        #region Methods
 
        public new void SetToolTip(Control control, string caption)
        {
            base.SetToolTip(control, caption);
            foreach (Control c in control.Controls)
            {
                SetToolTip(c, caption);
            }
        }
 
        #endregion
 
        #region Events 
 
        private void ToolTip_Popup(object sender, PopupEventArgs e)
        {
            Control control = e.AssociatedControl;
            if (control is IMetroControl iControl)
            {
                Style = iControl.Style;
                ThemeAuthor = iControl.ThemeAuthor;
                ThemeName = iControl.ThemeName;
                StyleManager = iControl.StyleManager;
            }
            else if (control is IMetroForm form)
            {
                Style = form.Style;
                ThemeAuthor = form.ThemeAuthor;
                ThemeName = form.ThemeName;
                StyleManager = form.StyleManager;
            }
            e.ToolTipSize = new(e.ToolTipSize.Width + 30, e.ToolTipSize.Height + 6);
        }
 
        #endregion
    }
 
    #endregion
}