yangyin
2024-10-25 1e65c49ba929103e79d87322ca1a3573c2b532e2
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace DPumpHydr.WinFrmUI.WenSkin.Controls
{
    [DefaultEvent("TextChanged")]
    [DefaultBindingProperty("Text")]
    public class WenLineTextBox : WenControl
    {
        public WenLineTextBox() : base()
        {
            textBox = new TextBox()
            {
                BorderStyle = BorderStyle.None,
                Anchor = AnchorStyles.Left | AnchorStyles.Right,
            };
 
            //鼠标移动
            textBox.MouseLeave += TextBox_MouseLeave;
            textBox.MouseEnter += TextBox_MouseEnter;
            this.MouseLeave += TextBox_MouseLeave;
            this.MouseEnter += TextBox_MouseEnter;
 
            this.MouseMove += WenLineTextBox_MouseMove;
 
            textBox.LostFocus += TextBox_LostFocus;
            textBox.GotFocus += TextBox_GotFocus;
 
            //创建消息 NativeWindow
            textBox.HandleCreated += TextBox_HandleCreated;
            textBox.HandleDestroyed += TextBox_HandleDestroyed;
 
            //鼠标点击事件
            this.MouseClick += WenLineTextBox_MouseClick;
 
            this.SizeChanged += WenLineTextBox_SizeChanged;
            this.Controls.Add(textBox);
 
            //初始化颜色
            BackColor = Color.FromArgb(37, 37, 38);
            BorderColor = Color.FromArgb(63, 63, 70);
            HotColor = Color.FromArgb(0, 122, 204);
            ForeColor = Color.FromArgb(241, 241, 241);
            WateTextColor = Color.FromArgb(153, 153, 153);
 
            PasswordChar = '\0';
        }
 
        #region 私有属性
        private Color hotColor;
        private Color borderColor;
        private bool isMouseOver;
        private string wateText;
        private Color wateTextColor;
        private bool buttonImageMouseOver;
        private HeaderNativeWindow headerNativeWindow;
        private Image buttonImage;
 
        private TextBox textBox { get; set; }
        private bool IsMouseOver { get => isMouseOver; set { isMouseOver = value; this.Invalidate(); } }
 
        private bool ButtonImageMouseOver { get => buttonImageMouseOver; set { buttonImageMouseOver = value; this.Invalidate(); } }
        #endregion
 
        #region 公用属性
 
        [Category("Wen颜色"), Description("背景颜色")]
        [DefaultValue(typeof(Color), "37, 37, 38")]
        public new Color BackColor
        {
            get => base.BackColor;
            set
            {
                value = value == Color.Transparent ? Color.FromArgb(37, 37, 38) : value;
                base.BackColor = value;
                textBox.BackColor = value;
            }
        }
 
        [Category("Wen颜色"), Description("前景颜色")]
        [DefaultValue(typeof(Color), "241, 241, 241")]
        public new Color ForeColor
        {
            get => base.ForeColor;
            set
            {
                value = value == Color.Transparent ? Color.FromArgb(37, 37, 38) : value;
                base.ForeColor = value;
                textBox.ForeColor = value;
            }
        }
 
        [Category("Wen颜色"), Description("热点颜色或者鼠标在控件颜色")]
        [DefaultValue(typeof(Color), "0,122,204")]
        public Color HotColor { get => this.hotColor; set { this.hotColor = value; this.Invalidate(); } }
 
        [Category("Wen颜色"), Description("获得或设置控件的边框颜色")]
        [DefaultValue(typeof(Color), "63,63,70")]
        public Color BorderColor { get => this.borderColor; set { this.borderColor = value; this.Invalidate(); } }
 
        [Category("Wen颜色"), Description("提示字符颜色")]
        [DefaultValue(typeof(Color), "153,153,153")]
        public Color WateTextColor { get => this.wateTextColor; set { this.wateTextColor = value; this.Invalidate(); } }
 
 
        [Category("Wen设计"), Description("当文本类容为空的时候提示内容")]
        [DefaultValue(null)]
        public string WateText { get => wateText; set { wateText = value; this.Invalidate(); } }
 
        [Category("Wen设计"), Description("尾部按钮图标")]
        [DefaultValue(null)]
        public Image ButtonImage
        {
            get => buttonImage;
            set
            {
                buttonImage = value;
                WenLineTextBox_SizeChanged(null, EventArgs.Empty);
                this.Invalidate();
            }
        }
        #endregion
 
        #region TextBox 属性
 
        [Category("Wen设计"), Description("文本内容")]
        [DefaultValue(null)]
        [Localizable(true)]
        public new string Text { get => textBox.Text; set => textBox.Text = value; }
 
        [Category("Wen设计"), Description("可编辑状态")]
        [DefaultValue(false)]
        public bool ReadOnly { get => textBox.ReadOnly; set => textBox.ReadOnly = value; }
 
        [Category("Wen设计"), Description("文本格式")]
        public new Font Font { get => textBox.Font; set { textBox.Font = value; base.Font = value; } }
 
        [Category("Wen设计"), Description("密码掩盖字符")]
        [DefaultValue('\0')]
        public char PasswordChar { get => textBox.PasswordChar; set => textBox.PasswordChar = value; }
        #endregion
 
        #region 委托
 
        [Category("Wen"), Description("图标按钮点击事件")]
        public event EventHandler ButtonClick;
 
        #endregion
 
        #region 创建消息 TextBox 消息拦截重绘
 
        private void TextBox_HandleDestroyed(object sender, EventArgs e)
        {
            if (headerNativeWindow != null)
            {
                headerNativeWindow = null;
            }
        }
 
        private void TextBox_HandleCreated(object sender, EventArgs e)
        {
            if (headerNativeWindow == null)
            {
                headerNativeWindow = new HeaderNativeWindow(this);
            }
        }
 
        private class HeaderNativeWindow : NativeWindow
        {
            private readonly WenLineTextBox owner;
            readonly IntPtr header;
            public HeaderNativeWindow(WenLineTextBox owner)
            {
                this.owner = owner;
                header = owner.textBox.Handle;
                base.AssignHandle(header);
            }
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                if (m.Msg == 0xf || m.Msg == 0x133)
                {
                    using Graphics g = Graphics.FromHwnd(m.HWnd).SetGDIHigh();
                    //当文本为空,提示文本不为空,水印TextBox
                    if (string.IsNullOrEmpty(owner.Text) && !string.IsNullOrEmpty(owner.WateText))
                    {
                        using Brush b = new SolidBrush(owner.WateTextColor);
                        g.DrawString(owner.WateText, owner.Font, b, new Rectangle(0, 0, owner.textBox.Width, owner.textBox.Height), ControlHelper.StringConters);
                    }
                }
            }
        }
 
        #endregion
 
        #region 鼠标移动 点击,获得焦点 失去事件 
        private void TextBox_LostFocus(object sender, EventArgs e)
        {
            this.Invalidate();
        }
 
        private void TextBox_MouseEnter(object sender, EventArgs e)
        {
            IsMouseOver = true;
        }
 
        private void TextBox_MouseLeave(object sender, EventArgs e)
        {
            IsMouseOver = false;
            ButtonImageMouseOver = false;
        }
        private void TextBox_GotFocus(object sender, EventArgs e)
        {
            this.Invalidate();
        }
 
        //鼠标点击
 
        private void WenLineTextBox_MouseClick(object sender, MouseEventArgs e)
        {
            Rectangle recImageBack = new Rectangle(this.Width - textBox.Height - 10, 0, textBox.Height + 10, this.Height);
            if (recImageBack.Contains(e.Location) && e.Button == MouseButtons.Left && ButtonImage != null)
            {
                ButtonClick?.Invoke(this, e);
            }
        }
 
        //鼠标所在位置
        private void WenLineTextBox_MouseMove(object sender, MouseEventArgs e)
        {
            Rectangle recImageBack = new Rectangle(this.Width - textBox.Height - 10, 0, textBox.Height + 10, this.Height);
            if (recImageBack.Contains(e.Location) && ButtonImage != null && !ButtonImageMouseOver)
            {
                ButtonImageMouseOver = true;
            }
            else if (!recImageBack.Contains(e.Location) && ButtonImageMouseOver)
            {
                ButtonImageMouseOver = false;
            }
        }
        #endregion
 
        private void WenLineTextBox_SizeChanged(object sender, EventArgs e)
        {
            textBox.Width = ButtonImage == null ? Width - 4 : Width - 16 - textBox.Height;
            textBox.Location = new Point(2, 5);
        }
 
        #region 重写事件 保持控件高度
 
        //保持控件宽度
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            base.SetBoundsCore(x, y, width, textBox.Height + 8, specified);
        }
 
        #endregion
 
        #region 重绘 画外框
 
        protected override void WenOnPaint(Graphics g, Rectangle rec, PaintEventArgs e)
        {
            base.WenOnPaint(g, rec, e);
            Rectangle reck = new Rectangle(0, 0, Width - 1, Height - 1);
 
            if (IsMouseOver || textBox.Focused)
                g.DrawRectangle(new Pen(HotColor), reck);
            else
                g.DrawRectangle(new Pen(BorderColor), reck);
 
            if (ButtonImage != null)
            {
                Rectangle recImage = new Rectangle(this.Width - textBox.Height - 6, 4, textBox.Height, textBox.Height);
                if (ButtonImageMouseOver)
                {
                    Rectangle recImageBack = new Rectangle(this.Width - textBox.Height - 10, 0, textBox.Height + 10, this.Height);
                    g.FillRectangle(new SolidBrush(this.HotColor), recImageBack);
                }
                g.DrawImage(ButtonImage, recImage);
            }
        }
        #endregion
    }
}