WinForm 窗体应用程序 (初步)之二

时间:2023-03-09 18:04:07
WinForm 窗体应用程序 (初步)之二

现在,我们来了解一些基本控件。控件是放置在工具箱里的,你可以在界面的左侧或者通过菜单栏的视图选项找到它。

(1)Label 控件 这是一个用于放置文字的控件,因为你不能在窗体上直接输入文字。

(2)TextBox 文本框

(3)Button 按钮

(4)CheckBox 复选框

(5)Panel  分组容器,类似于HTML中的div

(6)PictureBox 图片框

(7)WebBrowser 它可以允许用户在窗体内浏览网页,可用于制作浏览器

下面附上笔者自制的一个拼图游戏及代码文件:WinForm 窗体应用程序 (初步)之二

 using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ImgList = null;
}
#region 定义字段
List<Image> _imgList;
/// <summary>
/// 定义属性
/// </summary>
public List<Image> ImgList
{
get { return _imgList; }
set
{
_imgList = new List<Image>();
_imgList.Add(pictureBox1.BackgroundImage);
_imgList.Add(pictureBox2.BackgroundImage);
_imgList.Add(pictureBox3.BackgroundImage);
_imgList.Add(pictureBox4.BackgroundImage);
_imgList.Add(pictureBox5.BackgroundImage);
_imgList.Add(pictureBox6.BackgroundImage);
}
}
#endregion
#region 开始按钮
private void button1_Click(object sender, EventArgs e)
{
//随机6个不同的数
Random rd = new Random();
int[] x = new int[];
for (int i = ; i < ; i++)
{
x[i] = rd.Next(, );
for (int j = ; j < i; j++)
{
if (x[i] == x[j])
{
i--;
break;
}
}
}
//重新设置图像
pictureBox1.BackgroundImage = ImgList[x[]];
pictureBox2.BackgroundImage = ImgList[x[]];
pictureBox3.BackgroundImage = ImgList[x[]];
pictureBox4.BackgroundImage = ImgList[x[]];
pictureBox5.BackgroundImage = ImgList[x[]];
pictureBox6.BackgroundImage = ImgList[x[]];
//倒计时开始,并允许玩家操作
time = ;
label2.Text="";
timer1.Start();
pictureBox1.Enabled = true;
pictureBox2.Enabled = true;
pictureBox3.Enabled = true;
pictureBox4.Enabled = true;
pictureBox5.Enabled = true;
pictureBox6.Enabled = true;
}
#endregion
#region 玩家操作
//定义匹配变量
int match = ;
//存储上一张图片
PictureBox lpb = new PictureBox();
//响应用户操作
private void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox pb = sender as PictureBox;
//截取Name的最后一位作为唯一标识
int n = int.Parse(pb.Name.Substring(, ));
//判断是否已经正确归位,如果没有正确归位
if (pb.BackgroundImage != ImgList[n - ])
{
//重置参数
if (match == )
{
match = ;
}
//交换背景图片
if (match == )
{
Image img = pb.BackgroundImage;
pb.BackgroundImage = lpb.BackgroundImage;
lpb.BackgroundImage = img;
//判断是否全部归位
if (pictureBox1.BackgroundImage == ImgList[] && pictureBox2.BackgroundImage == ImgList[] && pictureBox3.BackgroundImage == ImgList[] && pictureBox4.BackgroundImage == ImgList[] && pictureBox5.BackgroundImage == ImgList[] && pictureBox6.BackgroundImage == ImgList[])
{
timer1.Stop();
MessageBox.Show("恭喜您,顺利过关!");
}
}
lpb = pb;
match++;
}
}
#endregion
#region 计时功能
int time = ;
private void timer1_Tick(object sender, EventArgs e)
{
if (time > )
{
time--;
label2.Text = time.ToString();
}
else
{
//停止计时,并禁止玩家操作
timer1.Stop();
pictureBox1.Enabled = false;
pictureBox2.Enabled = false;
pictureBox3.Enabled = false;
pictureBox4.Enabled = false;
pictureBox5.Enabled = false;
pictureBox6.Enabled = false;
MessageBox.Show("很遗憾,游戏失败!");
}
}
#endregion }
}