开发者

Gradient Drawing Bug

开发者 https://www.devze.com 2023-01-01 23:58 出处:网络
I\'m having trouble with a gradient drawing call. I have a Form that looks like this. Screenshot http://img413.imageshack.us/img413/3570/30364682.png

I'm having trouble with a gradient drawing call. I have a Form that looks like this.

Screenshot http://img413.imageshack.us/img413/3570/30364682.png

The problem is every now and again the above gradient drawing bug will start happening. It should go right across of course. Sometimes it only takes some build-rebuild-mashing to fix and it'll simply just "start" after a build every now and again.

That control (the top white part) is a TableLayoutPanel. The BackColor is set to white and on the panel's Paint event I do this:

/// <summary>
/// Draws the background gradient.
/// </summary>
private void titleBarLayoutPanel_Paint(object sender, PaintEventArgs e)
{
    Brush brush = new LinearGradientBrush(titleBarLayoutPanel.Bounds, TaskHeaderLeftColor, TaskHeaderRightColor, LinearGradientMode.Horizontal);
    e.Graphics.FillRectangle(brush, titleBarLayoutPanel.Bounds);
}

Should I be doing something else? The problem is that it works, and then without so much as a rebuild or build this will start happening!

EDIT I have since rebuilt the class library it is c开发者_JS百科ontained in (it's a generic Form) then rebuilt the app it's used in and the gradient is now filling across completely. This is bizarre.


Building and re-building your application, with no changes, normally doesn't solve this (or most any other bug for that matter) save the ones in which you run your application without doing a clean/rebuild first and then notice that the code you just wrote doesn't run (not sure that's possible these days with the IDEs). I see this a lot with newer devs when they keep rebuilding hoping that somehow the compiler will make the code "correct" or that maybe the compiler is simply not generating the correct code to begin with. (Please note that I do not mean the aforementioned statements to be taken disparagingly.)

To solve the issue at hand, you might try deriving your own TableLayoutPanel class in which you override the OnBackgroundPaint event, painting your own background, or simply returning if you don't want to paint your own background. (You seem to be painting the background in the Paint event). What you are doing in the code above is simply painting over the background already painted by the control, hence the "bug" you see, (double paint). It appears that the form is not resizable. Try making it resizable. Then resize it and watch it paint, or simply move other windows over it.

class CustomTableLayoutPanel : TableLayoutPanel
{
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        Brush brush = new LinearGradientBrush(this.ClientRectangle, TaskHeaderLeftColor, TaskHeaderRightColor, LinearGradientMode.Horizontal);
        e.Graphics.FillRectangle(brush, this.ClientRectangle);
        //base.OnPaintBackground(e);
    }
}


By the way, you should replace Bounds with ClientRectangle.
Bounds is the control's rectangle relative to its parent; ClientRectangle is relative to the control itself.
In this particular case, it won't make a difference, since the control is at 0, 0.

0

精彩评论

暂无评论...
验证码 换一张
取 消