程序运行的最终效果图
主要功能介绍:程序自动加载指定目录下的所有图片,点击前后按钮可以实现图片的前后切换,三连击屏幕左上角位置,退出程序。
1、启动vs,选择“创建新项目”,项目模版选择“windows窗体应用”,点击下一步,在配置新项目页面中,确定项目名称如:MyPupol,位置,其他选项默认,最后点击“创建”,完成项目创建。
2、打开工具箱,在Form窗体中拖入一个PictureBox控件,拖入两个Button控件。
3、创建一个自定义控件,主要的功能是实现三连击退出程序。
-
打开解决方案,选中项目,右键选择“添加”-“新建项”,选择“用户控件(windows窗体)”,确定名称:TripleButton.cs,然后点击添加按钮。
-
在解决方案中可以看到新创建的TripleButton.cs,选中,右键选择“查看代码”,打开TripleButton.cs 的代码窗口。
-
将下面的的代码全部复制到TripleButton.cs中,注意将命名空间修改成项目名称,
namespace MyPupil
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ImageBigTest
{
/// <summary>
/// 三击按钮
/// </summary>
public partial class TripleButton : System.Windows.Forms.Button
{
/// <summary>
///用Event来修饰委托对象, 委托类型的事件,内部定义了签名相同的两个BtnEventClick(),
///其中私有化了委托类型的事件变量,并为它创建了两个方法add、remove,
///这两个方法就是对委托对象BtnEventClick的操作
/// </summary>
public event DGTripleButton BtnEventClick;
/// <summary>
/// 记录点击时间
/// </summary>
Timer tmr;
/// <summary>
/// 三击按钮构造函数
/// </summary>
public TripleButton()
{
base.Click += new EventHandler(TripleButton_Click);
tmr = new Timer();
tmr.Interval = 1000;
tmr.Tick += new EventHandler(tmr_Tick);
}
/// <summary>
/// 时钟事件,一秒以内必须连续点击三次
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void tmr_Tick(object sender, EventArgs e)
{
clickTimes = 0;
}
/// <summary>
/// 单击次数
/// </summary>
int clickTimes = 0;
/// <summary>
/// 三击按钮执行的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void TripleButton_Click(object sender, EventArgs e)
{
//没有达到单击三次
if (clickTimes < 2)
{
if (clickTimes == 0)
{
tmr.Start();
}
clickTimes++;
}
else//三击之后
{
if (BtnEventClick != null)
{ //1. 执行委托对象的方法(指针)
BtnEventClick(DateTime.Now);
}
//2. 清空单击次数
clickTimes = 0;
tmr.Stop();
tmr.Start();
}
}
}
}
- 粘贴完代码“DGTripleButton”会有红色的下划线错误,提示未能找到类型或命名空间,这里需要添加一个类,在解决方案中选中项目,右键“添加”-“新建项”,选择新建类,确定类名称:DGTripleButton.cs,点击“添加”,在打开的DGTripleButton.cs代码窗口中将下面的代码复制粘贴。同样注意修改命名空间。
namespace MyPupil
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ImageBigTest
{
/// <summary>
/// 三击按钮委托类型
/// </summary>
/// <param name="time"></param>
public delegate void DGTripleButton(DateTime time);
}
- 将项目全部保存,在TripleButton.Designer.cs中,提示“TripleButton”未包含“AutoScaleMode”的定义。将这条语句删除或注释掉.
- 在解决方案中选中项目,右键“生成”,在工具箱页面就出现了新创建的自定义控件TripleButton了,将自定义控件TripleButton拖入到Form窗体中,放置在Form窗体的左上角。在属性页签中设置size:45,45;Location:3,3;BackColor:SeaGreen
- 下面为自定义控件TripleButton添加三连击事件
在Form1.cs代码中添加如下代码
private void threeClick(DateTime time)
{
Application.Exit();
}
- 在Form1.cs[设计]中选中TripleButton控件,在属性中点开事件页签(⚡图标),在BtnEventClick属性中选中刚创建的threeClick事件。
4、自动加载指定目录下的所有图片,将下面代码添加到Form1_Load事件中
将图片添加到泛型集合中,通过下标访问集合中的图片
//获取应用程序的当前工作目录 /bin/debug
string rootPath = Directory.GetCurrentDirectory();
//获取指定目录的父目录 的完整路径 /bin
string parentPath = Directory.GetParent(rootPath).FullName;
//项目根目录
string topPath = Directory.GetParent(parentPath).FullName;
//项目根目录下的img文件夹
topPath = topPath + "\\img";
//定义一个变量用于指向img目录下的某一图片完整路径
string finalPath;
//初始化指定目录的DirectoryInof 类实例
DirectoryInfo root = new DirectoryInfo(topPath);
//定义Image类型的泛型集合
List<Image> listimages = new List<Image>();
//循环获取img目录下的所有文件的完整路径并添加到链表中
foreach (FileInfo f in root.GetFiles())
{
finalPath = topPath + "\\" + f.Name;
listimages.Add(new Bitmap(finalPath));
}
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = listimages[0];
5、在上一步中,用image类型的泛型集合存储了img目录下的所有文件,这一步定义两个按钮,通过点击按钮改变泛型集合的下标完成切换图片的功能。在鼠标单击事件外定义一个变量指向泛型集合下标.
private int imageindex = 0;
- 向后切换图片,泛型集合的下标递增。
private void button2_Click(object sender, EventArgs e)
{
imageindex++;
if (imageindex >= listimages.Count)
{
imageindex = 0;
}
pictureBox1.Image = listimages[imageindex];
}
- 向先前切换图片,泛型集合的下标递减
private void button1_Click(object sender, EventArgs e)
{
if(imageindex == 0)
{
imageindex = listimages.Count;
}
imageindex--;
pictureBox1.Image = listimages[imageindex];
}
6、为按钮的点击事件绑定键盘键,即按键盘键触发按钮单击事件
首先需要将窗体Form的keyPreview属性设置为True
- 然后窗体Form添加KeyDown事件,F键绑定向前选按钮,J建绑定向后选按钮,代码如下:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.F)
{
button1_Click(null, null);
}
if(e.KeyCode == Keys.J)
{
button2_Click(null, null);
}
}
7、到这里大体完成了程序的主要功能,在这一步中对窗体、按钮控件做些设置。
- 程序运行时窗体设置,占满屏幕、无边框、底色(SeaGreen)
this.BackColor = System.Drawing.Color.SeaGreen;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.TopMost = true;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.ClientSize = new System.Drawing.Size(1920, 1080);
- TripleButton属性设置,无边框,底色,鼠标移入(颜色同底色),鼠标按下(颜色同底色),最终效果时,程序运行是,鼠标移入、点击外观都没有变化。
this.tripleButton1.BackColor = System.Drawing.Color.SeaGreen;
this.tripleButton1.FlatAppearance.BorderSize = 0;
this.tripleButton1.FlatAppearance.MouseDownBackColor = System.Drawing.Color.SeaGreen;
this.tripleButton1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.SeaGreen;
this.tripleButton1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- Button1属性设置
this.button1.BackColor = System.Drawing.Color.SeaGreen;
this.button1.FlatAppearance.BorderSize = 0;
this.button1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.SeaGreen;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("宋体", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(10, 510);
this.button1.Size = new System.Drawing.Size(25, 50);
- Button2属性设置
this.button2.BackColor = System.Drawing.Color.SeaGreen;
this.button2.FlatAppearance.BorderSize = 0;
this.button2.FlatAppearance.MouseOverBackColor = System.Drawing.Color.SeaGreen;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Font = new System.Drawing.Font("宋体", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(1885, 510);
this.button1.Size = new System.Drawing.Size(25, 50)
- pictureBox控件扩大,在窗口加载时(Form1_Load)进行设置
wfWidth = this.Width;
wfHieght = this.Height;
pictureBox1.Width = wfWidth - 95;
pictureBox1.Height = wfHieght - 80;
最后附上源码
https://download.csdn.net/download/qq_34598215/12656957