2011-12-29 10 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

これは私にとってはうまくいくが、ここで私が直面する問題は左下と右下に2つのボタンがあることだ。このフォームを最大化すると、両方のボタンが中央に配置されます。両方のボタンの固定が設定されていますが、フォームを最大化するときに問題が発生します – Rupesh

+1

アンカーがそれぞれ「下、左」および「下、右」に設定されていることを確認してください。 – Ani

+0

私の間違い。私はちょうど数分前にボタンの位置を逆転させましたが、アンカーをもう一度設定するのを忘れました。今すぐうまくいく。どうもありがとうございます – Rupesh

2

使用しOnPaint eventを使用することができますwinformの一部の変更を行うことができます。詳細については、指定されたリンクを確認してください。

としてこれを行うにはLinearGradientBrushを使用します。別の方法が使用され

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 

のOnPaint過負荷の

/*リニアグラデーションブラシ*/

を取る
LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Orange, Color.Orchid, LinearGradientMode.ForwardDiagonal); 

コードスニペット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); 
     } 

参考:Resize関連情報ここ
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#

チェック: this.Invalidate() - これはSOまた、スレッド
Create a Gradient background on your Forms or Controls

チェック.. Transparent control backgrounds on a VB.NET gradient filled form?

関連する問題