Custom form background color
Winforms by default don’t offer much customization as far as coloring, especially when it comes down to gradient patterns.
If you want to change the background of your form to something a bit more unique to make it look like this
then simply follow steps below.
First handle the form’s Paint
event.
1 2 3 4 5 6 7 8 | private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Rectangle rect = new Rectangle(0, 0, this.Size.Width, this.Size.Height); LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Green, Color.White, LinearGradientMode.ForwardDiagonal); g.FillRectangle(brush, rect); brush.Dispose(); } |
Feel free to change the colors and the gradient direction to what you would like.
Then we need to make sure that our form is properly painted when the user resizes the form. In order to do that we will need to handle the form#s Resize
event and Invalidate our form when it gets resized.
1 2 3 4 | private void Form1_Resize(object sender, EventArgs e) { this.Invalidate(); } |
You will notice than when you resize the form some flickering happens. In order to eliminate that make sure to set your form’s DoubleBuffered
to true either by simply changing the property in the property window or by calling this.DoubleBuffered = true;
in your form’s constructor or in the Load
event
Leave a Reply