G
Guest
In my WinForms app I've long been able to create a linear gradient effect by
just intercepting the PaintEventHandler event. But I'd now like to create an
inherited user control, specifically of a panel.
I thought I was on the right track and did achieve the effect ... until I
started putting some controls on the panel. Then I got an "Invalid
parameter" error but don't understand what it's referring to.
Based on examples I've found, I've tried two approaches but neither work.
Hopefully someone will know why.
Here are the two approaches:
protected override void OnPaintBackground(PaintEventArgs e)
{
Graphics gfx = e.Graphics;
Rectangle rect = new Rectangle (0, 0, this.Width, this.Height);
LinearGradientBrush lgb = new
LinearGradientBrush(this.ClientRectangle,
gradientColorOne, gradientColorTwo, lgm);
e.Graphics.FillRectangle(lgb, this.ClientRectangle);
lgb.Dispose();
e.Dispose();
gfx.Dispose();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
using(LinearGradientBrush lgb = new
LinearGradientBrush(this.ClientRectangle,
this.GradientColorOne,
this.GradientColorTwo,
this.GradientMode)) //.Rotation))
{e.Graphics.FillRectangle(lgb, this.ClientRectangle);
}
base.OnPaint(e); //right, want anything handled to be drawn too.
}
Incidentally, dragging a TextBox onto the panel DOES work. But none of the
other basic controls seem to. I don't know if that gives a clue.
just intercepting the PaintEventHandler event. But I'd now like to create an
inherited user control, specifically of a panel.
I thought I was on the right track and did achieve the effect ... until I
started putting some controls on the panel. Then I got an "Invalid
parameter" error but don't understand what it's referring to.
Based on examples I've found, I've tried two approaches but neither work.
Hopefully someone will know why.
Here are the two approaches:
protected override void OnPaintBackground(PaintEventArgs e)
{
Graphics gfx = e.Graphics;
Rectangle rect = new Rectangle (0, 0, this.Width, this.Height);
LinearGradientBrush lgb = new
LinearGradientBrush(this.ClientRectangle,
gradientColorOne, gradientColorTwo, lgm);
e.Graphics.FillRectangle(lgb, this.ClientRectangle);
lgb.Dispose();
e.Dispose();
gfx.Dispose();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
using(LinearGradientBrush lgb = new
LinearGradientBrush(this.ClientRectangle,
this.GradientColorOne,
this.GradientColorTwo,
this.GradientMode)) //.Rotation))
{e.Graphics.FillRectangle(lgb, this.ClientRectangle);
}
base.OnPaint(e); //right, want anything handled to be drawn too.
}
Incidentally, dragging a TextBox onto the panel DOES work. But none of the
other basic controls seem to. I don't know if that gives a clue.