环境: VS2005 C# XP我的工具栏自绘按钮派生自ToolStripButton:
        protected override void OnPaint(PaintEventArgs e)
        {
            if(!this.Checked)
            { base.OnPaint(e); }            LinearGradientBrush Brush = new LinearGradientBrush(
            new Point(10, 0),
            new Point(10, this.Height),
            Color.FromArgb(248, 247, 239),
            Color.FromArgb(238, 237, 229));            if(this.Checked)
            {
                Tools.DrawRoundRect(e.Graphics, Color.FromArgb(206, 206, 195),
                    new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width -2, e.ClipRectangle.Height));
                Tools.FillRoundRect(e.Graphics, Brush,
                    new Rectangle(e.ClipRectangle.X + 1, e.ClipRectangle.Y + 1, e.ClipRectangle.Width - 4, e.ClipRectangle.Height-1));
                e.Graphics.DrawLine(new Pen(Color.FromArgb(215,210,198)), new Point(e.ClipRectangle.X + 1, e.ClipRectangle.Height - 1), new Point(e.ClipRectangle.Width - 2, e.ClipRectangle.Height - 1));
                e.Graphics.DrawLine(new Pen(Color.FromArgb(228,225,214)), new Point(e.ClipRectangle.X + 1, e.ClipRectangle.Height - 2), new Point(e.ClipRectangle.Width - 2, e.ClipRectangle.Height - 2));
                e.Graphics.DrawLine(new Pen(Color.FromArgb(238,237,229)), new Point(e.ClipRectangle.X + 1, e.ClipRectangle.Height - 3), new Point(e.ClipRectangle.Width - 2, e.ClipRectangle.Height - 3));
                e.Graphics.DrawImage(InfiKeeper.Properties.Resources.page_white, new Point(2, 2));
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), 19, 4);
            }
            Brush.Dispose();
        }//end fun问题:当整个应用程序窗口刷新时(比如应用程序最大、最小化),我的自绘按钮也被刷新。
如果只是部分刷新,比如用另一个窗口盖住我的自绘按钮,移开后我的按钮未被刷新。请问如何让我的自绘按钮在两种情况下都能刷新?

解决方案 »

  1.   

    没试过你那样的。我一般这样的:
                SetStyle(ControlStyles.Selectable, true);
                SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                SetStyle(ControlStyles.DoubleBuffer, true);
                SetStyle(ControlStyles.UserPaint, true);
                SetStyle(ControlStyles.AllPaintingInWmPaint, true);
      

  2.   

    问题已解决,原因出在e.ClipRectangle上。原来系统提供的e.ClipRectangle不是每次相同的。
    当整个应用程序窗口刷新时(比如应用程序最大、最小化),我的自绘按钮也被刷新。
    此时e.ClipRectangle就是整个按钮大小 
    如果只是部分刷新,比如用另一个窗口盖住我的自绘按钮,移开后我的按钮未被刷新。
    此时e.ClipRectangle仅仅是失效区大小,就是被盖住的区域,但由于我的代码是完全画整个按钮,所以就不能用这个区域了。解决方法就是用0,0,this.Width,this.Height代替e.ClipRectangle
    感谢楼上回复!