• WinForm水印文本框


        [ToolboxBitmap(typeof(TextBox))]
        public class WatermarkTextBox : TextBox
        {
            private string _emptyTextTip;
            private Color _emptyTextTipColor = Color.DarkGray;
            private const int WM_PAINT = 0xF;
    
            public WatermarkTextBox()
                : base()
            {
            }
    
            [DefaultValue("")]
            public string EmptyTextTip
            {
                get { return _emptyTextTip; }
                set
                {
                    _emptyTextTip = value;
                    Invalidate();
                }
            }
    
            [DefaultValue(typeof(Color), "DarkGray")]
            public Color EmptyTextTipColor
            {
                get { return _emptyTextTipColor; }
                set
                {
                    _emptyTextTipColor = value;
                    Invalidate();
                }
            }
    
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                if (m.Msg == WM_PAINT)
                {
                    WmPaint();
                }
            }
    
            private void WmPaint()
            {
                using (Graphics graphics = Graphics.FromHwnd(Handle))
                {
                    if (Text.Length == 0
                        && !string.IsNullOrEmpty(_emptyTextTip)
                        && !Focused)
                    {
                        TextFormatFlags format =
                            TextFormatFlags.EndEllipsis |
                            TextFormatFlags.VerticalCenter;
    
                        if (RightToLeft == RightToLeft.Yes)
                        {
                            format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
                        }
    
                        TextRenderer.DrawText(
                            graphics,
                            _emptyTextTip,
                            Font,
                            ClientRectangle,
                            _emptyTextTipColor,
                            format);
                    }
                }
            }
        }
  • 相关阅读:
    洛谷 2574 XOR的艺术
    洛谷 1774 最接近神的人
    洛谷 3398 仓鼠找sugar 【模板】判断树上两链有交
    洛谷 2440 木材加工
    洛谷 1824 进击的奶牛
    洛谷 1873 砍树
    洛谷 1577 切绳子
    51nod1485 字母排序
    51nod1055 最长等差数列
    51nod1128 正整数分组V2
  • 原文地址:https://www.cnblogs.com/xinzheng/p/13141098.html
Copyright © 2020-2023  润新知