2011-12-29 215 views

回答

5

一种方法是直接用图像作为形式的BackgroundImage

如果你想要实现这个proceduarally(更灵活),可以使用手动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 
} 

结果

Gradient

+0

这对我来说很好,但这里有一个问题,我面对的是我左下角和右下角有两个按钮。当我最大化这种形式时,两个按钮都放在中间。设置两个按钮的锚定,但是当我最大化表格 – Rupesh

+1

时确定锚点分别设置为“底部,左侧”和“底部,右侧”。 – Ani

+0

我的错误。我只是在几分钟前扭转了按钮的位置,但忘了再次设置锚点。现在工作很好。非常感谢你 – Rupesh

2

使用可以使用OnPaint eventwinform,在那里你可以做一些修改。检查指定的链接以了解详细信息。

使用LinearGradientBrush要做到这一点是:

/*取一个线性渐变画笔*/

LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Orange, Color.Orchid, LinearGradientMode.ForwardDiagonal); 
的OnPaint的

代码段过载:

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事件和使用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); 
     } 

参考:
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#

检查Resize相关的信息在这里: this.Invalidate() -
Create a Gradient background on your Forms or Controls

检查该SO也是线程.. Transparent control backgrounds on a VB.NET gradient filled form?