Reputation: 7906
How can I set the background color for a form as specified in the attached image?
Upvotes: 3
Views: 4389
Reputation: 113462
One way would be to directly use the image as the form's BackgroundImage
.
If you want to achieve this proceduarally (more flexible), you can manually paint the form's background using OnPaintBackground
:
protected override void OnPaintBackground(PaintEventArgs e)
{
using (var brush = new LinearGradientBrush
(DisplayRectangle, Color.Black, Color.DarkGray, LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(brush, DisplayRectangle);
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Invalidate(); // Force repainting on resize
}
Result:
Upvotes: 5
Reputation: 18290
Use can use OnPaint event
of the winform
and there you can do some modifications. Check the specified links to know in details about this.
Use the LinearGradientBrush
to do this as:
/* Take a Linear Gradient Brush */
LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Orange, Color.Orchid, LinearGradientMode.ForwardDiagonal);
Code Snippet of OnPaint Overload:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
' Declare a variable of type Graphics named formGraphics.
' Assign the address (reference) of this forms Graphics object
' to the formGraphics variable.
Dim formGraphics As Graphics = e.Graphics
' Declare a variable of type LinearGradientBrush named gradientBrush.
' Use a LinearGradientBrush constructor to create a new LinearGradientBrush object.
' Assign the address (reference) of the new object
' to the gradientBrush variable.
Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(Width, 0), Color.White, Color.DarkMagenta)
' Here are two more examples that create different gradients.
' Comment the Dim statement immediately above and uncomment one of these
' Dim statements to see how varying the two colors changes the gradient result.
' Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(Width, 0), Color.Chartreuse, Color.SteelBlue)
' Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(Width, 0), Color.White, Color.SteelBlue)
formGraphics.FillRectangle(gradientBrush, ClientRectangle)
End Sub
Another Way is use OnPaintBackground
event and use LinearGradientBrush
ref:MSDN
protected override void OnPaintBackground(PaintEventArgs e) {
Rectangle rc = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
using (LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Red, Color.Blue, 45F)) {
e.Graphics.FillRectangle(brush, rc);
}
Reference:
How to Add a Gradient Background to a Win Form with VB.NET & VB2005
Windows Forms 2.0-Draw Beautiful Gradient Backdrops
Set Gradient/Shaded Background to Windows form using c#
Check Resize
related information here:
this.Invalidate()
-
Create a Gradient background on your Forms or Controls
check this SO thread also.. Transparent control backgrounds on a VB.NET gradient filled form?
Upvotes: 2