2011-12-29 262 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?