1.公共方法

 

    public class MyTextBox : TextBox
    {
        
protected override void OnEnabledChanged(EventArgs e)
        {
            
base.OnEnabledChanged(e);
            
//设置Enabled为False时
            if (this.Enabled)
            {
                
this.SetStyle(ControlStyles.UserPaint, false);
               
            }
            
else
                
this.SetStyle(ControlStyles.UserPaint, true);
            
//再描绘
            this.Invalidate();
        }

        
//描绘TextBox
        protected override void OnPaint(PaintEventArgs e)
        {
            
base.OnPaint(e);
            System.Drawing.Brush b 
=
                
new System.Drawing.SolidBrush(this.ForeColor);

            StringFormat sf 
= new StringFormat();
            sf.LineAlignment 
= StringAlignment.Center;
            sf.Alignment 
= StringAlignment.Center;
            
//描绘字符串
            
//e.Graphics.DrawString(this.Text, this.Font, b, -1, 1);
            e.Graphics.DrawString(this.Text, this.Font, b, this.ClientRectangle, sf);

            b.Dispose();
        }
    }

 

 

2.页面 designer.cs修改为 MyTextBox

相关文章:

  • 2021-12-19
  • 2021-09-25
  • 2022-12-23
  • 2021-10-02
  • 2021-11-21
  • 2021-11-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案