c#: Label控件加入AutoHeight属性

本文介绍了一种在Winform中创建自定义Label和TextBox的方法,通过继承和重写实现自动调整高度和添加水印文字的功能,提高了界面布局的灵活性。

此功能在界面布局中颇为实用,录代码以记之:

    public class LabelEx : Label
    {
        private bool autoHeight = true;

        [DefaultValue(true)]
        public bool AutoHeight
        {
            get { return this.autoHeight; }
            set
            {
                this.autoHeight = value;
                RecalcHeight();
            }
        }

        protected override void OnAutoSizeChanged(EventArgs e)
        {
            base.OnAutoSizeChanged(e);
            RecalcHeight();
        }

        protected override void OnFontChanged(EventArgs e)
        {
            base.OnFontChanged(e);
            RecalcHeight();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            RecalcHeight();
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            RecalcHeight();
        }

        private void RecalcHeight()
        {
            if (this.AutoSize || !this.autoHeight)
                return;

            var size = TextRenderer.MeasureText(this.Text, this.Font);
            int lc = (int)Math.Ceiling(size.Width * 1.0 / this.ClientSize.Width);
            this.Height = lc * size.Height;
        }
    }

 

顺手,给TextBox加个水印文字(PlaceHolderText)功能:

    public class TextBoxEx : TextBox
    {
        private string placeHolderText;

        [DefaultValue("")]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string PlaceHolderText { get { return this.placeHolderText; } set { this.placeHolderText = value; PaintPlaceHolderText(); } } protected override void WndProc(ref Message m) { base.WndProc(ref m);
//若使OnPaint()起作用,须设置this.SetStyle(ControlStyles.UserPaint,true);,但这样控件显示都得重绘,所以直截WM_PAINT消息
//WM_PAINT消息:0xf WM_CTLCOLOREDIT:0x133 if (m.Msg == 0xf || m.Msg == 0x133) PaintPlaceHolderText(); } private void PaintPlaceHolderText() { if (this.Focused || string.IsNullOrEmpty(this.placeHolderText) || !string.IsNullOrEmpty(this.Text)) return; var g = Graphics.FromHwnd(this.Handle); var bounds = this.ClientRectangle; g.FillRectangle(new SolidBrush(this.BackColor), bounds); var sf = new StringFormat(); switch (this.TextAlign) { case HorizontalAlignment.Left: sf.Alignment = StringAlignment.Near; break; case HorizontalAlignment.Center: sf.Alignment = StringAlignment.Center; break; case HorizontalAlignment.Right: sf.Alignment = StringAlignment.Far; break; } bounds.Offset(-1, 1); g.DrawString(this.placeHolderText, this.Font, new SolidBrush(Color.DarkGray), bounds, sf); } }

 

 

参考资料:

Winform-TextBox实现 placeholder - zhishiheng的专栏 - 优快云博客

转载于:https://www.cnblogs.com/crwy/p/10185059.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值