tangxu
2024-12-16 23fadc9cb0c09b665a1bbcef7eaf16f916045dc4
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
#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.Runtime.InteropServices;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region MetroLabel
 
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(MetroLabel), "Bitmaps.Label.bmp")]
    [Designer(typeof(MetroLabelDesigner))]
    [DefaultProperty("Text")]
    [ComVisible(true)]
    public class MetroLabel : Label, 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;
                }
                Invalidate();
            }
        }
 
        [Category("Metro"), Description("Gets or sets the Style Manager associated with the control.")]
        public MetroStyleManager StyleManager
        {
            get => _styleManager;
            set { _styleManager = value; Invalidate(); }
        }
 
        [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;
        private MetroStyleManager _styleManager;
 
        private bool _isDerivedStyle = true;
 
        #endregion Internal Vars
 
        #region Constructors
 
        public MetroLabel()
        {
            SetStyle
            (
                ControlStyles.ResizeRedraw |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.SupportsTransparentBackColor,
                true
            );
            UpdateStyles();
            base.Font = MetroFonts.Light(10);
            mth = new Methods();
            utl = new Utilites();
            ApplyTheme();
        }
 
        #endregion Constructors
 
        #region ApplyTheme
 
        private void ApplyTheme(Style style = Style.Light)
        {
            if (!IsDerivedStyle)
            {
                return;
            }
 
            switch (style)
            {
                case Style.Light:
                    ForeColor = Color.Black;
                    BackColor = Color.Transparent;
                    ThemeAuthor = "Taiizor";
                    ThemeName = "MetroLight";
                    UpdateProperties();
                    break;
                case Style.Dark:
                    ForeColor = Color.FromArgb(170, 170, 170);
                    BackColor = Color.Transparent;
                    ThemeAuthor = "Taiizor";
                    ThemeName = "MetroDark";
                    UpdateProperties();
                    break;
                case Style.Custom:
                    if (StyleManager != null)
                    {
                        foreach (System.Collections.Generic.KeyValuePair<string, object> varkey in StyleManager.LabelDictionary)
                        {
                            switch (varkey.Key)
                            {
                                case "ForeColor":
                                    ForeColor = utl.HexColor((string)varkey.Value);
                                    break;
                                case "BackColor":
                                    BackColor = utl.HexColor((string)varkey.Value);
                                    break;
                                default:
                                    return;
                            }
                        }
                    }
 
                    UpdateProperties();
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(style), style, null);
            }
        }
 
        private void UpdateProperties()
        {
            Invalidate();
        }
 
        #endregion Theme Changing
 
        #region Properties
 
        [Category("Metro"), Description("Gets or sets the form forecolor.")]
        public override Color ForeColor { get; set; }
 
        /// <summary>
        /// Gets or sets the form BackColor.
        /// </summary>
        [Category("Metro"), Description("Gets or sets the form backcolor.")]
        public override Color BackColor { 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 => _isDerivedStyle;
            set
            {
                _isDerivedStyle = value;
                Refresh();
            }
        }
 
        #endregion Properties
    }
 
    #endregion
}