近期,因为工作的需要接触到了C#这门语言,一开始做的UI页面,觉得特别有成就感。然后,今天上班因为也是周六。上班的性质不是很大,分班制,公司来的人也是比较少。有感而发的制作了一个倒计时,来记录上班的时常。
啥也不说直接上教程:
需要的控件就是 两个label控件 + 一个timer控件 + progressBar控件。
这是效果图
为了方便我就没有设置程序的关闭框以免看着不舒服然后就点掉了。但是在菜单栏还是可以右击关闭的。
FromBorderStyle改为None,就可以看到上图的效果图,但是记得手动去把from窗口给缩成想要的效果。我就是缩成了很小的一个。
然后就是两个label控件了。这里我就只演示一个,另一个也是一样的,我相信大家都有这样的能力去修改。AutoSize选择为false是为了我们可以把控件的大小拖拽成我们想要的大小。字体也可改,我这里就没有标识出来。
代码其实还是很少的就100行左右,因为之前把FromBorderStyle改为None,所以我们的程序运行后的位置是不可以修改的。但是加上移动的代码就可以了。
绿色的框的东西一开始是没有的直接在想要输入的栏双击就会自动创建了。
整个文档的我就一起放了代码:
using System;
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 time
{
public partial class Form1 : Form
{
TimeSpan dtTo = new TimeSpan(3, 0, 0); //设置开始时间
int Prg_Max_value = 10800;//进度条的最大值,也就是上班的时间有多少秒。
//int Prg_Min_value = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;//设置每次间隔1s
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
dtTo = dtTo.Subtract(new TimeSpan(0, 0, 1));
progressBar1.Step = 1;//设置每次增长多少,
progressBar1.Maximum = Prg_Max_value;//设置进度条的最大进度的值,
//这是进度条的最大值。
if (timer1.Enabled)
{
if(progressBar1.Value == Prg_Max_value)
{
progressBar1.Value = Prg_Max_value;
label1.Text = "下班啦!";
timer1.Stop();
MessageBox.Show("恭喜你,今日工作时长以满8小时!","提示");
}
else
{
progressBar1.Value += progressBar1.Step; //让进度条增加一次
label1.Text = dtTo.Hours.ToString() + ":" + dtTo.Minutes.ToString() + ":" + dtTo.Seconds;
}
}
if (dtTo.TotalSeconds < 0.0)//当倒计时完毕
{
//lock_time = false;
timer1.Enabled = false; //其中可自行添加相应的提示框或者方法函数
}
}
bool formMove = false;//窗体是否移动
Point formPoint;//记录窗体的位置
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (formMove == true)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(formPoint.X, formPoint.Y);
Location = mousePos;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
formPoint = new Point();
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
formPoint = new Point(xOffset, yOffset);
formMove = true;//开始移动
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//按下的是鼠标左键
{
formMove = false;//停止移动
}
}
}
}
计时器的参考来自:https://www.csdn.net/tags/MtzaEgwsMTMyNjktYmxvZwO0O0OO0O0O.html
实现无边框的移动来自:C#winform中怎么实现无边框窗体的拖动、最大化、最小化以及关闭。
以上若有侵权请联系我,我会第一时间处理!!!只是为了多学习,没有别的。
有问题可以私信、或者评论。