c# winform 键盘按键控制操作移动控件,linkLabel,获取按键值,一个俄罗斯方块源码

Form1
按键移动控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace man100
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		public static int buChang = 5; 

		private void Form1_KeyDown(object sender, KeyEventArgs e)
		{
			//MessageBox.Show(e.KeyValue.ToString());//用来找按键int值

			//MessageBox.Show(e.KeyCode.ToString());//用来找按键英文值

			
			if (e.KeyCode.ToString().Equals("Left"))//左
			{
				//MessageBox.Show("左");
				//label1.Location = new Point(0,0);

				int x_ZuoBiao = label1.Location.X;
				int y_ZuoBiao = label1.Location.Y;

				if (label1.Location.X < 15)
				{
					label1.Location = new Point(x_ZuoBiao, y_ZuoBiao);
					return;
				}

				label1.Location = new Point(x_ZuoBiao - buChang, y_ZuoBiao);
			}
			else if (e.KeyCode.ToString().Equals("Up"))//上
			{
				//MessageBox.Show("上");
				int x_ZuoBiao = label1.Location.X;
				int y_ZuoBiao = label1.Location.Y;

				if (label1.Location.Y < 15)
				{
					label1.Location = new Point(x_ZuoBiao, y_ZuoBiao);
					return;
				}

				label1.Location = new Point(x_ZuoBiao, y_ZuoBiao - buChang);
			}
			else if (e.KeyCode.ToString().Equals("Right"))//右
			{
				//MessageBox.Show("右");
				int x_ZuoBiao = label1.Location.X;
				int y_ZuoBiao = label1.Location.Y;

				if (label1.Location.X > 265)
				{
					label1.Location = new Point(x_ZuoBiao, y_ZuoBiao);
					return;
				}

				label1.Location = new Point(x_ZuoBiao + buChang, y_ZuoBiao);
			}
			else if (e.KeyCode.ToString().Equals("Down"))//下
			{
				//MessageBox.Show("下");
				int x_ZuoBiao = label1.Location.X;
				int y_ZuoBiao = label1.Location.Y;

				if (label1.Location.Y > 395)//超出界限就不动
				{
					label1.Location = new Point(x_ZuoBiao, y_ZuoBiao);
					return;
				}

				label1.Location = new Point(x_ZuoBiao, y_ZuoBiao + buChang);
			}
		}
	}
}


Form2
linkLabel用法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace man100
{
	public partial class Form2 : Form
	{
		public Form2()
		{
			InitializeComponent();

			this.linkLabel1.Text = "黑色头发 百度 Google Yahoo";
			this.linkLabel1.Links.Add(0, 4, "http://www.heisetoufa.iteye.com");//第一个参数设置下划线从哪里开始,第二个参数设置下划线占几个长度
			this.linkLabel1.Links.Add(5, 2, "http://www.baidu.com");
			this.linkLabel1.Links.Add(8, 6, "http://www.google.com");
		}

		private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			this.linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true;
			string target = e.Link.LinkData as string;
			if (target != null && target.StartsWith("http://"))
			{
				System.Diagnostics.Process.Start(target);
			}
			else
			{
				MessageBox.Show("Item clicked: " + target);
			}
		}

	}
}


黑色头发:http://heisetoufa.iteye.com/

你可能感兴趣的:(C++,c,C#,百度,WinForm)