//================================================================================================= // C#のラベル // 今まで通りラベルを使用して文字を表示するプログラム。 // 「TextAlign」で文字位置の設定、「BorderStyle」で境界線の設定ができる。 // 境界線を3Dにすると微妙にへこみっぽいモノができる。 // また、「Font」で文字のフォントを設定できる。 // // 参考文献:やさしいC#(p171) // 2012/11/14/オワタ //================================================================================================= using System.Windows.Forms; using System.Drawing; class LabelEx : Form { private Label[] mLabel = new Label[3]; //< ラベル * 3 private TableLayoutPanel mTlp; //< テーブルレイアウトパネル //< メイン関数 public static void Main() { Application.Run(new LabelEx()); } //< コンストラクタ public LabelEx() { //< Window生成 設定 this.Text = "ラベル"; this.Width = 400; this.Height = 400; this.BackColor = Color.Black; //< テーブルレイアウトパネル生成 設定 mTlp = new TableLayoutPanel(); mTlp.Dock = DockStyle.Fill; mTlp.ColumnCount = 1; //< 列設定 mTlp.RowCount = 3; //< 行設定 //< ラベル生成 設定 for (int i = 0; i < mLabel.Length; i++) { mLabel[i] = new Label(); mLabel[i].Width = 300; mLabel[i].Height = 50; mLabel[i].Text = i + "Car"; } //< ラベル0設定 mLabel[0].ForeColor = Color.Black; //< 前景色設定 mLabel[0].BackColor = Color.White; //< 背景色設定 mLabel[0].TextAlign = ContentAlignment.TopLeft; //< テキスト位置設定(左上) mLabel[0].BorderStyle = BorderStyle.None; //< 境界線設定(無し) mLabel[0].Font = new Font("SansSerif", 12, FontStyle.Bold); //< フォント設定 //< ラベル1設定 mLabel[1].ForeColor = Color.Black; mLabel[1].BackColor = Color.Gray; mLabel[1].TextAlign = ContentAlignment.MiddleCenter; //< テキスト位置設定(中央) mLabel[1].BorderStyle = BorderStyle.None; //< 境界線設定(無し) mLabel[1].Font = new Font("Helvetica", 14, FontStyle.Bold); //< フォント設定 //< ラベル2設定 mLabel[2].ForeColor = Color.Black; mLabel[2].BackColor = Color.White; mLabel[2].TextAlign = ContentAlignment.BottomRight; //< テキスト位置設定(右下) mLabel[2].BorderStyle = BorderStyle.Fixed3D; //< 境界線設定(3D) mLabel[2].Font = new Font("Century", 16, FontStyle.Bold); //< フォント設定 //< 同期 for (int i = 0; i < mLabel.Length; i++) { mLabel[i].Parent = mTlp; } //< 同期 mTlp.Parent = this; } };