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
#region Imports
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using static DPumpHydr.WinFrmUI.RLT.Helper.CrownHelper;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region CrownLabel
 
    public class CrownLabel : Label
    {
        #region Field Region
 
        private bool _autoUpdateHeight;
        private bool _isGrowing;
 
        #endregion
 
        #region Property Region
 
        [Category("Layout")]
        [Description("Enables automatic height sizing based on the contents of the label.")]
        [DefaultValue(false)]
        public bool AutoUpdateHeight
        {
            get => _autoUpdateHeight;
            set
            {
                _autoUpdateHeight = value;
 
                if (_autoUpdateHeight)
                {
                    AutoSize = false;
                    ResizeLabel();
                }
            }
        }
 
        public new bool AutoSize
        {
            get => base.AutoSize;
            set
            {
                base.AutoSize = value;
 
                if (AutoSize)
                {
                    AutoUpdateHeight = false;
                }
            }
        }
 
        #endregion
 
        #region Constructor Region
 
        public CrownLabel()
        {
            ForeColor = ThemeProvider.Theme.Colors.LightText;
        }
 
        #endregion
 
        #region Method Region
 
        private void ResizeLabel()
        {
            if (!_autoUpdateHeight || _isGrowing)
            {
                return;
            }
 
            try
            {
                _isGrowing = true;
                Size sz = new(Width, int.MaxValue);
                sz = TextRenderer.MeasureText(Text, Font, sz, TextFormatFlags.WordBreak);
                Height = sz.Height + Padding.Vertical;
            }
            finally
            {
                _isGrowing = false;
            }
        }
 
        #endregion
 
        #region Event Handler Region
 
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            ResizeLabel();
        }
 
        protected override void OnFontChanged(EventArgs e)
        {
            base.OnFontChanged(e);
            ResizeLabel();
        }
 
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            ResizeLabel();
        }
 
        #endregion
    }
 
    #endregion
}