扩展Label控件public class MyLabel : Label
    {
        public MyLabel()
        {
            InitializeComponent();
        }        protected override void OnPaint(PaintEventArgs e)
        {
            //渐变填充
            LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.Yellow, Color.Red, LinearGradientMode.Vertical);
            e.Graphics.DrawString(this.Text, Font, brush, this.ClientRectangle);
            //base.OnPaint(e);
        }        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }
    }
在使用时,比较Text="这个是渐变文字",显示的是"这个是渐变文",少一个字
AutoSize=true;
加上在OnPaint方法后加上base.OnPaint(e),绘制两次发现渐变这一次长度要比原来的要长,不明白为什么?求解

解决方案 »

  1.   

    你可以将label的Width定义长一点,估计是长度不够,看上去就想被截取了一样
      

  2.   

    是否其他控件覆盖了部分label
      

  3.   

    AutoSize=true;长度是自动增长的
    界面上只有一个控件
      

  4.   

    在Label自身的文字绘制中,会有测量字符矩形过程,所以才有autosize,按Label自身的字体(如默认9号宋体)和你一样会超过范围,所以在绘制时,会从负值画,并不是从(0,0)开始,而是从一个四舍五入的负值如(-3,-3),这才是左上角。
      

  5.   

    <asp:Label runat="server"  style="word-break:break-all;"    ID="lb" ></asp:Label>加一个 这样的样式
      

  6.   


    应该是这个理由,不过我还是不知道怎么解决,在重绘时我把重绘字体设成比this.font小点就可以显示完