public class Form1 : System.Windows.Forms.Form { // ユーザ定義変数 // Bitmapクラス変数 // キャラ private System.Drawing.Bitmap bmp_Char = null; // その他 // 座標 private int char_x,char_y;こんな感じで。
private void Form1_Load(object sender, System.EventArgs e) { // キャラのBitmapインスタンスを生成 this.bmp_Char = new Bitmap("char.png"); // 初期座標 this.char_x = 100; this.char_y = 200; }こう。
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { e.Graphics.DrawImageUnscaled(this.bmp_Char, char_x, char_y); }こうなる、と。
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { }これで、キーが押されるとこのイベントが呼ばれる。
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { switch (e.KeyCode) { case Keys.Up: System.Diagnostics.Debug.WriteLine("上矢印が押されました"); break; case Keys.Down: System.Diagnostics.Debug.WriteLine("下矢印が押されました"); break; case Keys.Left: System.Diagnostics.Debug.WriteLine("左矢印が押されました"); break; case Keys.Right: System.Diagnostics.Debug.WriteLine("右矢印が押されました"); break; default: break; }System.Diagnostics.Debug.WriteLine() メソッドを使ってデバッグ出力している。
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { switch (e.KeyCode) { case Keys.Up: this.char_y--; System.Diagnostics.Debug.WriteLine("上矢印が押されました"); break; case Keys.Down: this.char_y++; System.Diagnostics.Debug.WriteLine("下矢印が押されました"); break; case Keys.Left: this.char_x--; System.Diagnostics.Debug.WriteLine("左矢印が押されました"); break; case Keys.Right: this.char_x++; System.Diagnostics.Debug.WriteLine("右矢印が押されました"); break; default: break; } // pictureboxを更新 this.pictureBox1.Invalidate(); }で、実行するとこんな感じ。
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace GDI_Prac { /// <summary> /// Form1 の概要の説明です。 /// </summary> public class Form1 : System.Windows.Forms.Form { // ユーザ定義変数 // Bitmapクラス変数 // キャラ private System.Drawing.Bitmap bmp_Char = null; // その他 // 座標 private int char_x,char_y; private System.Windows.Forms.PictureBox pictureBox1; /// <summary> /// 必要なデザイナ変数です。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // } /// <summary> /// 使用されているリソースに後処理を実行します。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows フォーム デザイナで生成されたコード /// <summary> /// デザイナ サポートに必要なメソッドです。このメソッドの内容を /// コード エディタで変更しないでください。 /// </summary> private void InitializeComponent() { this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.SuspendLayout(); // // pictureBox1 // this.pictureBox1.BackColor = System.Drawing.Color.White; this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(640, 480); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 12); this.ClientSize = new System.Drawing.Size(656, 493); this.Controls.Add(this.pictureBox1); this.Name = "Form1"; this.Text = "Form1"; this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// <summary> /// アプリケーションのメイン エントリ ポイントです。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { // キャラのBitmapインスタンスを生成 this.bmp_Char = new Bitmap("char.png"); // 初期座標 this.char_x = 100; this.char_y = 200; } private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { e.Graphics.DrawImageUnscaled(this.bmp_Char, char_x, char_y); } private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { switch (e.KeyCode) { case Keys.Up: this.char_y -= 3; System.Diagnostics.Debug.WriteLine("上矢印が押されました"); break; case Keys.Down: this.char_y += 3; System.Diagnostics.Debug.WriteLine("下矢印が押されました"); break; case Keys.Left: this.char_x -= 3; System.Diagnostics.Debug.WriteLine("左矢印が押されました"); break; case Keys.Right: this.char_x += 3; System.Diagnostics.Debug.WriteLine("右矢印が押されました"); break; default: break; } // pictureboxを更新 this.pictureBox1.Invalidate(); } } }
Copyright (C) Zawa 2005
<<戻る>>