参考サイト1の方法はImage
を作成しているので、初めに1回やれば十分です。
参考サイト2の方法は注意書きにある通り、Paint
イベントで直接描画しています。
パネルのサイズが固定ならば1(panel1
)で、サイズが変わるなら2(panel2
)がいいんじゃないですかね。
.NET6です^^(なにか指定があれば合わせます)
cs
1 using System . Drawing . Drawing2D ;
2
3 namespace Q5mtt1e0e765dl0 ;
4
5 public partial class Form1 : Form
6 {
7 public Form1 ( )
8 {
9 InitializeComponent ( ) ;
10
11 var tableLayoutPanel1 = new TableLayoutPanel
12 {
13 ColumnCount = 2 ,
14 Dock = DockStyle . Fill ,
15 Parent = this ,
16 } ;
17 tableLayoutPanel1 . ColumnStyles . Add ( new ( SizeType . Percent , 50F ) ) ;
18 tableLayoutPanel1 . ColumnStyles . Add ( new ( SizeType . Percent , 50F ) ) ;
19
20
21 var panel1 = new Panel
22 {
23 Dock = DockStyle . Fill ,
24 Parent = tableLayoutPanel1 ,
25 } ;
26
27 var canvas = new Bitmap ( panel1 . Width , panel1 . Height ) ;
28 using var g = Graphics . FromImage ( canvas ) ;
29 using var gb = new LinearGradientBrush ( g . VisibleClipBounds ,
30 Color . DarkBlue , Color . MediumVioletRed , LinearGradientMode . Vertical ) ;
31 g . FillRectangle ( gb , g . VisibleClipBounds ) ;
32 panel1 . BackgroundImage = canvas ;
33
34
35 var panel2 = new Panel
36 {
37 Dock = DockStyle . Fill ,
38 Parent = tableLayoutPanel1 ,
39 } ;
40 panel2 . Paint += Panel2_Paint ;
41 panel2 . Resize += ( _ , _ ) => panel2 . Invalidate ( ) ; // Paintを呼ぶ
42 }
43
44 private void Panel2_Paint ( Object ? sender , PaintEventArgs e )
45 {
46 if ( sender is Panel panel2 )
47 {
48 using var gb = new LinearGradientBrush ( e . Graphics . VisibleClipBounds ,
49 Color . DarkBlue , Color . MediumVioletRed , LinearGradientMode . Vertical ) ;
50 e . Graphics . FillRectangle ( gb , e . Graphics . VisibleClipBounds ) ;
51 }
52 }
53 }
コード片だけではどこに書いたらいいのかわかりませんよね^^;
なので私は全文あげる派です^^
追記(より絞った例&コメント追加)
1. Panelをフォームに追加する
2. Panelのプロパティを変更
Panel
が可変サイズになればなんでもいいがAnchorにした。
3. Panelのイベントを追加する
Paint
・Resize
イベントを追加
4. コード
cs
1 using System ;
2 using System . Collections . Generic ;
3 using System . ComponentModel ;
4 using System . Data ;
5 using System . Drawing ;
6 using System . Drawing . Drawing2D ;
7 using System . Linq ;
8 using System . Text ;
9 using System . Threading . Tasks ;
10 using System . Windows . Forms ;
11
12 namespace Q5mtt1e0e765dl0
13 {
14 public partial class Form1 : Form
15 {
16 public Form1 ( )
17 {
18 InitializeComponent ( ) ;
19 }
20
21 // パネルを描画するときに呼び出される
22 private void panel1_Paint ( object sender , PaintEventArgs e )
23 {
24 // ブロック({})を抜けたら自動的に Dispose するC#構文
25 using (
26 // グラデーションブラシ作成
27 var gb = new LinearGradientBrush (
28 // グラデーション範囲(表示クリッピング領域)
29 e . Graphics . VisibleClipBounds ,
30 // グラデーション開始色(紺色)
31 Color . DarkBlue ,
32 // グラデーション終了色(赤紫)
33 Color . MediumVioletRed ,
34 // グラデーション方向(縦)
35 LinearGradientMode . Vertical ) )
36 {
37 // 四角形の内部を塗りつぶす(表示クリッピング領域)
38 e . Graphics . FillRectangle ( gb , e . Graphics . VisibleClipBounds ) ;
39 }
40 // using構文を使用したため Dispose を書く必要はない
41 //リソースを解放する
42 //gb.Dispose();
43 }
44
45 // パネルサイズが変わったときに呼び出される
46 private void panel1_Resize ( object sender , EventArgs e )
47 {
48 // パネルの表面全体を無効化してパネルを再描画する
49 panel1 . Invalidate ( ) ;
50 }
51 }
52 }
参考
using ステートメント - C# リファレンス | Microsoft Learn
LinearGradientBrush クラス (System.Drawing.Drawing2D) | Microsoft Learn
Graphics.VisibleClipBounds プロパティ (System.Drawing) | Microsoft Learn
Graphics.FillRectangle メソッド (System.Drawing) | Microsoft Learn
Control.Invalidate メソッド (System.Windows.Forms) | Microsoft Learn