📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 坦克
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.PictureBox pictureBox3;
private System.Windows.Forms.PictureBox pictureBox4;
private System.Windows.Forms.PictureBox pictureBox5;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
/// <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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.pictureBox4 = new System.Windows.Forms.PictureBox();
this.pictureBox5 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(40, 24);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(96, 80);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Visible = false;
//
// pictureBox2
//
this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
this.pictureBox2.Location = new System.Drawing.Point(216, 16);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(100, 72);
this.pictureBox2.TabIndex = 1;
this.pictureBox2.TabStop = false;
this.pictureBox2.Visible = false;
//
// pictureBox3
//
this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox3.Image")));
this.pictureBox3.Location = new System.Drawing.Point(40, 136);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(100, 64);
this.pictureBox3.TabIndex = 2;
this.pictureBox3.TabStop = false;
this.pictureBox3.Visible = false;
//
// pictureBox4
//
this.pictureBox4.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox4.Image")));
this.pictureBox4.Location = new System.Drawing.Point(216, 128);
this.pictureBox4.Name = "pictureBox4";
this.pictureBox4.Size = new System.Drawing.Size(100, 80);
this.pictureBox4.TabIndex = 3;
this.pictureBox4.TabStop = false;
this.pictureBox4.Visible = false;
//
// pictureBox5
//
this.pictureBox5.Location = new System.Drawing.Point(248, 272);
this.pictureBox5.Name = "pictureBox5";
this.pictureBox5.Size = new System.Drawing.Size(100, 72);
this.pictureBox5.TabIndex = 4;
this.pictureBox5.TabStop = false;
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 256);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 288);
this.label2.Name = "label2";
this.label2.TabIndex = 6;
this.label2.Text = "label2";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 328);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(216, 23);
this.label3.TabIndex = 7;
this.label3.Text = "label3";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(456, 387);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox5);
this.Controls.Add(this.pictureBox4);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.pictureBox2);
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.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
label1.Text="X坐标:"+pictureBox5.Top;
label2.Text="Y坐标:"+pictureBox5.Left;
label3.Text="请按上下左右键控制坦克!";
pictureBox5.Image=pictureBox4.Image;
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.Up:
pictureBox5.Image=pictureBox4.Image;
if((pictureBox5.Top+pictureBox5.Height)<=0)
pictureBox5.Top=this.Height-20;
else
{
pictureBox5.Top-=10;
}
break;
case Keys.Down:
pictureBox5.Image=pictureBox1.Image;
if(pictureBox5.Top>=this.Height)
{
pictureBox5.Top=-60;
}
else
{
pictureBox5.Top+=10;
}
break;
case Keys.Left:
pictureBox5.Image=pictureBox2.Image;
if((pictureBox5.Width+pictureBox5.Left)<=0)
pictureBox5.Left=this.Width-20;
else
{
pictureBox5.Left-=10;
}
break;
case Keys.Right:
pictureBox5.Image=pictureBox3.Image;
if(pictureBox5.Left>=this.Width)
{
pictureBox5.Left=-60;
}
else
{
pictureBox5.Left+=10;
}
break;
}
label1.Text="X坐标:"+pictureBox5.Top;
label2.Text="Y坐标:"+pictureBox5.Left;
label3.Text="现在按下"+e.KeyCode.ToString()+"键,键盘值为"+e.KeyValue.ToString()+"!!";
}
private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
label3.Text="请按上下左右键控制坦克!";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -