Label name label Date Font Size 18 pt
フォームのデザイン Labelコントロール変更点 (name): label. Date Font Size: 18 pt Text. Align: Middle. Center Labelコントロール変更点 Buttonコントロール変更点 (name): button. Finish Text: 終了 Font Size: 12 pt (name): label. Time Font Size: 20 pt Text. Align: Middle. Center
Date. Timeのインスタンスを宣言する場所 namespace Matsushima /// <summary> using System; /// 使用されているリソースに後処理を実行します。 using System. Drawing; /// </summary> using System. Collections; protected override void Dispose( bool disposing ) using System. Component. Model; { using System. Windows. Forms; if( disposing ) using System. Data; 正解その1 { namespace Koushu 80 Date. Timeのインスタンスdtを if (components != null) { Form 1のメンバとして宣言する { /// <summary> components. Dispose(); /// Form 1 の概要の説明です。 →Form 1内からアクセス可能 } /// </summary> } public class Form 1 : System. Windows. Form base. Dispose( disposing ); { } /// <summary> /// 必要なデザイナ変数です。 Windows Form Designer generated code /// </summary> 正解その2 private System. Component. Model. Container components = null; private System. Windows. Forms. Label label. Date; インスタンスdtがForm 1コンストラ/// <summary> /// アプリケーションのメイン エントリ ポイントです。 private System. Windows. Forms. Label label. Time; クタメソッド内のローカル変数でも/// </summary> private System. Windows. Forms. Button button. Finish; private Date. Time dt; 良いならこれでOK (この例では[STAThread] static void Main() ローカル変数でも問題ない) { public Form 1() Date. Time dt; { dt = Date. Time. Now; // Application. Run(new Form 1()); // Windows フォーム デザイナ サポートに必要です。 } // 現在時刻を取得して, } Initialize. Component(); } Labelコントロールに設定 // // TODO: Initialize. Component 呼び出しの後に、コンス・・ 不正解 // インスタンス(変数)dtはMain() Date. Time dt; メソッド内のローカル変数にな dt = Date. Time. Now; label. Date. Text = string. Format("西暦{0}年{1}月{2}日", dt. Year, dt. Month, dt. Day); るため,イベントハンドラ等か label. Time. Text = string. Format("{0}時{1}分", dt. Hour, dt. Minute); らアクセスできない }
Paintハンドラの例 private void Form 1_Paint(object sender, Paint. Event. Args e) { 直線 line Graphics g = e. Graphics; Pen Green. Pen = new Pen(Color. Green); Pen Red. Pen = new Pen(Color. Red); Pen Yellow. Pen = new Pen(Color. Yellow); 四角形 rectangle 円/楕円 ellipse g. Draw. Line(Green. Pen, 10, 200); g. Draw. Ellipse(Red. Pen, 50, 30); g. Draw. Pie(Yellow. Pen, 120, 100, 150, 15, 120); Pen Red. Thick. Pen = new Pen(Color. Red, 10); g. Draw. Rectangle(Red. Thick. Pen, 150, 20, 100, 50); Solid. Brush blue. Brush = new Solid. Brush(Color. Blue); Solid. Brush Brown. Brush = new Solid. Brush(Color. Brown); g. Fill. Pie(blue. Brush, 20, 80, 100, 15, 120); g. Fill. Ellipse(Brown. Brush, 150, 100, 50); } 中身の詰まった扇形 Fill. Pie 扇形 pie 中身の詰まった円/楕円 Fill. Ellipse
Graphicsオブジェクトを Paintハンドラ内で取得 Paintハンドラの例 private void Form 1_Paint(object sender, Paint. Event. Args e) { 青字はインスタンス名(変数 Graphics g = e. Graphics; 名)なので自由に変えられる 緑・赤・黄色で太さが1ピクセル のPenオブジェクトを作成 緑色ペンで座標値(10, 10)から (200, 200)へ直線を引く Pen Green. Pen = new Pen(Color. Green); Pen Red. Pen = new Pen(Color. Red); Pen Yellow. Pen = new Pen(Color. Yellow); g. Draw. Line(Green. Pen, 10, 200); g. Draw. Ellipse(Red. Pen, 50, 30); g. Draw. Pie(Yellow. Pen, 120, 100, 150, 15, 120); 赤色ペンで,左上隅の座標が (50, 50)で幅30,高さ 30の正方形 に内接する円を描く 赤色で太さ 10ピクセルのペン Pen Red. Thick. Pen = new Pen(Color. Red, 10); g. Draw. Rectangle(Red. Thick. Pen, 150, 20, 100, 50); 赤色で太いペンで左上(150, 20),幅100 ,高さ 50の長方形を描く 青と茶色の単色ブラシ Solid. Brush blue. Brush = new Solid. Brush(Color. Blue); Solid. Brush brown. Brush = new Solid. Brush(Color. Brown); g. Fill. Pie(blue. Brush, 20, 80, 100, 15, 120); 左上隅の座標が(20, 80)で g. Fill. Ellipse(brown. Brush, 150, 100, 50); 幅100,高さ 100の長方形 } に内接する円のうち 15度 から120度の扇形を青色ブ 茶色ブラシで,左上隅の座標が ラシで塗りつぶす (150, 100)で幅50,高さ 50の正方 形に内接する円を塗りつぶす 描画メソッドの種類やその詳細については Graphicsクラスをキーワード検索
コントロールで図形描画を制御する public class Form 1 : System. Windows. Form { private System. Windows. Forms. Numeric. Up. Down numeric. Up. Down 1; private System. Windows. Forms. Button button 1; /// <summary> /// 必要なデザイナ変数です。 直線の長さを表わす変数 /// </summary> private System. Component. Model. Container components = null; private int length = 0; ・・・略 作図ボタンを押すと,入力した値の 10倍 の長さ(ピクセル)の太線を引く numeric. Up. Down. ValueはDecimal型 のプロパティなのでそのままではint 型変数に代入できない private void button 1_Click(object sender, System. Event. Args e) { (int) ( x ) で変数xの型を length = (int) (numeric. Up. Down 1. Value); int型に変換する this. Refresh(); Form 1の描画領域を一旦消去して,Paint } 200ピクセル Numeric. Up. Downコント ロールのインスタンス numeric. Up. Down 1 図形描画を制御する仕組み 作図ボタンのクリック イベントハンドラを強制的に呼び出すメソッド private void Form 1_Paint(object sender, Paint. Event. Args e) { Graphics g = e. Graphics; g. Draw. Line(new Pen(Color. Blue, 20), 0, 20, length*10, 20); } } 太さ 20ピクセルの青色ペン で直線を描画 変数値の変更 変数 length 変数値の参照 Paintイベントハンドラ
図形をアニメーションする 緑の円が左から右へ移動してゆく public class Form 1 : System. Windows. Form { private System. Windows. Forms. Timer timer 1; private System. Component. Model. IContainer components; private int position = 0; 円の現在位置を示す変数 public Form 1() { タイマースタート Initialize. Component(); timer 1. Start(); } タイマーのTickイベントハン ・・・略 ドラ.Intervalプロパティは 100[ms]に設定 図形をアニメーションする仕組み private void timer 1_Tick(object sender, System. Event. Args e) { 100[ms]ごとに位置を 5ピクセ position = position + 5; ル移動する this. Refresh(); } private void Form 1_Paint(object sender, Paint. Event. Args e) { positionの値がある程度大きくな if (position > 300) ったら元に戻す position = 0; Graphics g = e. Graphics; Rectangle rect = new Rectangle(position, 50, 40); g. Draw. Ellipse(new Pen(Color. Green), rect); (position, 50)の位置に直 } 径40の円を描画 タイマーのTick イベントハンドラ 変数値の変更 変数 position 変数値の参照 Paintイベントハンドラ
- Slides: 14