zoukankan      html  css  js  c++  java
  • Winfrom TextBox 添加水印文字 + 字体颜色

    using System.Drawing;
    using System.Windows.Forms;
    
    namespace KK.WaterMark.Control
    {
        public partial class WaterMarkTextBox : TextBox
        {
            private string watermarkText;
            public string WatermarkText
            {
                get { return watermarkText; }
                set { watermarkText = value; }
            }
            private Color watermarkTextColor;
            public Color WatermarkTextColor
            {
                get { return watermarkTextColor; }
                set { watermarkTextColor = value; }
            }
    
            public WaterMarkTextBox()
            {
                InitializeComponent();
            }
    
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
    
                if (m.Msg ==0x000F )//WM_PAINT
                {
                    WmPaint(ref m);
                }
            }
    
            private void WmPaint(ref Message m)
            {
                Rectangle rectangle = new Rectangle(0, 0, Width, Height);
                using (Graphics graphics = Graphics.FromHwnd(base.Handle))
                {
                    if (Text.Length == 0
                       && !string.IsNullOrEmpty(watermarkText)
                       && !Focused)
                    {
                        TextFormatFlags format = TextFormatFlags.EndEllipsis
                            | TextFormatFlags.VerticalCenter;
    
                        if (RightToLeft == RightToLeft.Yes)
                        {
                            format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
                        }
    
                        TextRenderer.DrawText(graphics, watermarkText, Font, 
                            base.ClientRectangle, watermarkTextColor, format);
                    }
                }
            }
        }
    }
    

    运行效果如下所示:

  • 相关阅读:
    js动态生成按钮,页面用DIV简单布局
    Maven初学之经验浅谈
    pl/sql注册码
    windows server 2012R2 网络慢的那些事
    sql 优化
    巧用selectKey
    list集合,map集合遍历
    oracle中declare程序块用法
    处理oracle锁表
    关于img标签图片不加载不识别相对路径得解决办法
  • 原文地址:https://www.cnblogs.com/YYkun/p/11389800.html
Copyright © 2011-2022 走看看