tangxu
2024-10-22 7a0d1efa4784d176a32f182ecae671711e98ca92
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
241
242
243
244
245
246
247
248
249
250
251
252
253
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Controls;
using DPumpHydr.WinFrmUI.RLT.Helper;
using DPumpHydr.WinFrmUI.RLT.Manager;
using DPumpHydr.WinFrmUI.RLT.Util;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using static DPumpHydr.WinFrmUI.RLT.Helper.MaterialDrawHelper;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Child.Material
{
    #region MaterialBaseTextBox
 
    [ToolboxItem(false)]
    public class MaterialBaseTextBox : TextBox, MaterialControlI
    {
        #region "Public Properties"
 
        //Properties for managing the material design properties
        [Browsable(false)]
        public int Depth { get; set; }
 
        [Browsable(false)]
        public MaterialSkinManager SkinManager => MaterialSkinManager.Instance;
 
        [Browsable(false)]
        public MaterialMouseState MouseState { get; set; }
 
        private string hint = string.Empty;
        public string Hint
        {
            get => hint;
            set
            {
                hint = value;
                Invalidate();
            }
        }
 
        public new void SelectAll()
        {
            BeginInvoke((MethodInvoker)delegate ()
            {
                base.Focus();
                base.SelectAll();
            });
        }
 
        #endregion
 
        public MaterialBaseTextBox()
        {
        }
 
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            Invalidate();
        }
 
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            Invalidate();
        }
 
        private const int WM_ENABLE = 0x0A;
        private const int WM_PAINT = 0xF;
        private const uint WM_USER = 0x0400;
        private const uint EM_SETBKGNDCOLOR = WM_USER + 67;
        private const uint WM_KILLFOCUS = 0x0008;
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
 
 
            if (m.Msg == WM_PAINT)
            {
                if (m.Msg == WM_ENABLE)
                {
                    Graphics g = Graphics.FromHwnd(Handle);
                    Rectangle bounds = new(0, 0, Width, Height);
                    g.FillRectangle(SkinManager.BackgroundDisabledBrush, bounds);
                }
            }
 
            if (m.Msg == WM_PAINT && string.IsNullOrEmpty(Text) && !Focused)
            {
                using MaterialNativeTextRenderer NativeText = new(Graphics.FromHwnd(m.HWnd));
                NativeText.DrawTransparentText(
                Hint,
                SkinManager.GetFontByType(MaterialSkinManager.FontType.Subtitle1),
                Enabled ?
                MaterialColorHelper.RemoveAlpha(SkinManager.TextMediumEmphasisColor, BackColor) : // not focused
                MaterialColorHelper.RemoveAlpha(SkinManager.TextDisabledOrHintColor, BackColor), // Disabled
                ClientRectangle.Location,
                ClientRectangle.Size,
                MaterialNativeTextRenderer.TextAlignFlags.Left | MaterialNativeTextRenderer.TextAlignFlags.Top);
            }
 
            if (m.Msg == EM_SETBKGNDCOLOR)
            {
                Invalidate();
            }
 
            if (m.Msg == WM_KILLFOCUS) //set border back to normal on lost focus
            {
                Invalidate();
            }
 
        }
 
    }
 
    #endregion
 
    #region MaterialBaseMaskedTextBox
 
    [ToolboxItem(false)]
    public class MaterialBaseMaskedTextBox : MaskedTextBox, MaterialControlI
    {
        //Properties for managing the material design properties
        [Browsable(false)]
        public int Depth { get; set; }
 
        [Browsable(false)]
        public MaterialSkinManager SkinManager => MaterialSkinManager.Instance;
 
        [Browsable(false)]
        public MaterialMouseState MouseState { get; set; }
 
        private string hint = string.Empty;
        public string Hint
        {
            get => hint;
            set
            {
                hint = value;
                Invalidate();
            }
        }
 
        public new void SelectAll()
        {
            BeginInvoke((MethodInvoker)delegate ()
            {
                base.Focus();
                base.SelectAll();
            });
        }
 
        public MaterialBaseMaskedTextBox()
        {
        }
 
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            Invalidate();
        }
 
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            Invalidate();
        }
 
        private const int WM_ENABLE = 0x0A;
        private const int WM_PAINT = 0xF;
        private const uint WM_USER = 0x0400;
        private const uint EM_SETBKGNDCOLOR = WM_USER + 67;
        private const uint WM_KILLFOCUS = 0x0008;
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
 
 
            if (m.Msg == WM_PAINT)
            {
                if (m.Msg == WM_ENABLE)
                {
                    Graphics g = Graphics.FromHwnd(Handle);
                    Rectangle bounds = new(0, 0, Width, Height);
                    g.FillRectangle(SkinManager.BackgroundDisabledBrush, bounds);
                }
            }
 
            if (m.Msg == WM_PAINT && string.IsNullOrEmpty(Text) && !Focused)
            {
                using MaterialNativeTextRenderer NativeText = new(Graphics.FromHwnd(m.HWnd));
                NativeText.DrawTransparentText(
                Hint,
                SkinManager.GetFontByType(MaterialSkinManager.FontType.Subtitle1),
                Enabled ?
                MaterialColorHelper.RemoveAlpha(SkinManager.TextMediumEmphasisColor, BackColor) : // not focused
                MaterialColorHelper.RemoveAlpha(SkinManager.TextDisabledOrHintColor, BackColor), // Disabled
                ClientRectangle.Location,
                ClientRectangle.Size,
                MaterialNativeTextRenderer.TextAlignFlags.Left | MaterialNativeTextRenderer.TextAlignFlags.Top);
            }
 
            if (m.Msg == EM_SETBKGNDCOLOR)
            {
                Invalidate();
            }
 
            if (m.Msg == WM_KILLFOCUS) //set border back to normal on lost focus
            {
                Invalidate();
            }
 
        }
    }
 
    #endregion
 
    #region MaterialBaseTextBoxContextMenuStrip
 
    [ToolboxItem(false)]
    public class MaterialBaseTextBoxContextMenuStrip : MaterialContextMenuStrip
    {
        public readonly ToolStripItem undo = new MaterialToolStripMenuItem { Text = "Undo" };
        public readonly ToolStripItem seperator1 = new ToolStripSeparator();
        public readonly ToolStripItem cut = new MaterialToolStripMenuItem { Text = "Cut" };
        public readonly ToolStripItem copy = new MaterialToolStripMenuItem { Text = "Copy" };
        public readonly ToolStripItem paste = new MaterialToolStripMenuItem { Text = "Paste" };
        public readonly ToolStripItem delete = new MaterialToolStripMenuItem { Text = "Delete" };
        public readonly ToolStripItem seperator2 = new ToolStripSeparator();
        public readonly ToolStripItem selectAll = new MaterialToolStripMenuItem { Text = "Select All" };
 
        public MaterialBaseTextBoxContextMenuStrip()
        {
            Items.AddRange(new[]
            {
                    undo,
                    seperator1,
                    cut,
                    copy,
                    paste,
                    delete,
                    seperator2,
                    selectAll
                });
        }
    }
 
    #endregion
}