WinForm 窗体应用程序 (初步)之二
现在,我们来了解一些基本控件。控件是放置在工具箱里的,你可以在界面的左侧或者通过菜单栏的视图选项找到它。
(1)Label 控件 这是一个用于放置文字的控件,因为你不能在窗体上直接输入文字。
(2)TextBox 文本框
(3)Button 按钮
(4)CheckBox 复选框
(5)Panel 分组容器,类似于HTML中的div
(6)PictureBox 图片框
(7)WebBrowser 它可以允许用户在窗体内浏览网页,可用于制作浏览器
下面附上笔者自制的一个拼图游戏及代码文件:
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 }
}
WinForm 窗体应用程序 (初步)之二的更多相关文章
- C# winform 窗体应用程序之图片上传Oracle数据库保存字段BLOB
C# winform 窗体应用程序之图片上传Oracle数据库保存字段BLOB 我用的数据库是Oracle,就目前来看,许多数据库现在都倾向于Oracle数据库,对ORACLE数据库基本的操作也是必须 ...
- winform窗体 小程序【线程】
线程是进程中执行运算的最小单位,也是执行处理机调度的基本单位.实际上线程是轻量级的进程.那么为什么要使用线程呢? (1)易于调度. (2)提高并发性.通过线程可方便有效地实现并发性.进程可创建多个线程 ...
- WinForm 窗体应用程序(初步)之一
学习制作一个WinForm程序,有两样东西是需要首先掌握的.第一部分,我们称之为属性面板.无论是窗体还是控件,都有着自己的属性面板.第二部分,则是我们称之为控件的东西. 我们先来讨论一下属性面板.新建 ...
- WinForm 窗体应用程序(初步)之三
进程: 进程,简单的说,就是让你的程序启动另一个程序. 1.Process.Start("calc");//启动计算器 弊端:只认识系统自带的程序,如果写错系统会崩溃. 2. // ...
- WinForm窗体更新程序
流程介绍: 打包参阅:WinForm程序打包说明 图一 图二 图三 实现步骤: 主程序 1.检测是否连上ftp服务器 1.1 连接不上,不检测. 1.2 连接上,如果有更新进程, ...
- 《winform窗体应用程序》----------简易记事本
首先先给大家发表几张图片,描述一下记事本程序要实现的功能以及界面设计. 以上这些就是简易记事本的的主界面设计. 下面我们来做一些简单的讲解: 1.使用MenuStrip控件来实现菜单栏的基本设计. 在 ...
- 设置WinForm窗体及程序图标
自己留着看,总是用的时候给忘记了,百度来百度去的麻烦. 设置 Ico 图标为 [资源文件] 项目名à右键à属性,在选项卡中选择"资源" 选择 "添加资源"à ...
- winform窗体 小程序【进程】
进程 一个应用程序就是一个进程,我的理解是,只要是打开应用程序,就会创建进程. 在.NET框架在using.System.Diagnostics名称空间中,有一个类Process,用来创建一个新的进程 ...
- winform窗体 小程序【登录窗体】【恶搞程序】
登录窗体 using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; ...
随机推荐
- Mybatis 缓存
1. 一级缓存:其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 Cache 就将清空. 2. 二级缓存与一级缓存其机制相同,不同在于其存 ...
- -bash: /usr/local/bin/react-native: No such file or directory
执行react-native run-android/run-ios的时候出现 -bash: /usr/local/bin/react-native: No such file or director ...
- CSS中的::after ::before
利用::after和before来清除浮动 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- struts2结果(Result)
一.结果(result)类型 result的type属性默认为dispatcher,其他常见的属性有redirect\chain\redirectAction <action name=&quo ...
- PhoneGap介绍及简单部署
一.什么是PhoneGap: PhoneGap是一个自由开放源码的开发工具和框架,允许利用HTML + JavaScript + CSS的强大功能在多个手机平台上开发程序,开发出来的程序经过在各自的平 ...
- poj1006生理周期(中国剩余定理)
/* 中国剩余定理可以描述为: 若某数x分别被d1..….dn除得的余数为r1.r2.….rn,则可表示为下式: x=R1r1+R2r2+…+Rnrn+RD 其中R1是d2.d3.….dn的公倍数,而 ...
- 将数据转化成字符串时:用字符串的链接 还是 StringBuilder
/* 目的:将数据转化成字符串时:用字符串的链接 还是 StringBuilder呢? */ public class Test{ public static void main(String[] a ...
- Ext.Net全部Icon图标名称展示
- Linux的学习--配置LNMP环境
最近,回到公司,发现电脑都换成linux系统了...很无力... 配置环境,跑起项目来就花了一天...额...在这里记录一下-- 系统是ubuntu 12.04. 一.安装nginx1:ubuntu因 ...
- spring aop源码实现分析
1. 先分析Advice before执行Cglib2AopProxy的intercept方法: /** * General purpose AOP callback. Used when the t ...