圖片放置
下載如下四張圖片 tankD.bmp tankL.bmp tankR.bmp tankU.bmp
再放置PictureBox, 名稱改為picTank. 然後點選屬性Image, 匯入專案資源檔. 將四張圖匯入, 此時四張圖即會置於Resources目錄之下
標簽設置
拉入三個Label, 名稱改為lblx, lbly, lblMsg, 分別顯示x, y座標及一般訊息
程式碼
private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Up: picTank.Image = tank.Properties.Resources.tankU; if (picTank.Top + picTank.Height <= 0) { picTank.Top = this.Height; } else picTank.Top -= 10; break; case Keys.Down: picTank.Image = tank.Properties.Resources.tankD; if (picTank.Top >=this.Height) { picTank.Top = -picTank.Height; } else picTank.Top += 10; break; case Keys.Left: picTank.Image = tank.Properties.Resources.tankL; if (picTank.Left<=-picTank.Height) { picTank.Left = this.Width; } else picTank.Left -= 10; break; case Keys.Right: picTank.Image = tank.Properties.Resources.tankR; if (picTank.Left>=this.Width) { picTank.Left = -picTank.Width; } else picTank.Left += 10; break; } lblx.Text = "X 座標 : " + picTank.Location.X; lbly.Text = "Y 座標 : " + picTank.Location.Y; lblMsg.Text = "KeyCode : " + e.KeyCode.ToString() + ", KeyValue : " + e.KeyValue; } private void Form1_Load(object sender, EventArgs e) { this.BackColor = Color.White; }