如何设置附加图像中指定的表单的背景颜色?
答案 0 :(得分:5)
一种方法是直接将图像用作表单BackgroundImage
。
如果您希望以程序方式(更灵活)实现此目的,可以使用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
}
<强>结果强>:
答案 1 :(得分:2)
使用可以使用OnPaint event
的{{1}},您可以在那里进行一些修改。检查指定的链接以了解详细信息。
使用winform
执行此操作:
/ *采用线性渐变画笔* /
LinearGradientBrush
OnPaint Overload的代码片段:
LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Orange, Color.Orchid, LinearGradientMode.ForwardDiagonal);
另一种方式是使用 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
事件并使用OnPaintBackground
参考:MSDN
LinearGradientBrush
参考:
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#
在此处查看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);
}
相关信息:
Resize
-
Create a Gradient background on your Forms or Controls
检查这个SO线程也.. Transparent control backgrounds on a VB.NET gradient filled form?