只有想不到 没有做不到(防疲劳软件)
截图:
一、界面
在界面上没什么好说的,因为自用所以控件的摆放也比较随意。
二、逻辑
当点击开始按钮后,记录一个开始时间、启用一个Timer(Timer1)去不断的计算提醒剩余时间,当提醒剩余时间为0时,停用Timer1并调用WindowsApI去播放选中的歌,启用一个Timer(Timer2)用于计算歌曲是否已结束,当歌曲结束后再启用Timer1、把开始时间置为当前时间、停用Timer2,以上Timer时间间隔为1秒,其中在播放歌曲时会锁定计算机,这样是为了强迫使用者休息保护好自己的眼睛和保持清淅的思路。(如此不停的循环调用)
二、实现要点
1.时间差计算:
剩余时间=提醒间隔-(现在时间-开始时间)
TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
TimeSpan ts2 = new TimeSpan(startTime.Ticks);
TimeSpan ts = ts1.Subtract(ts2);
//这里要把天和小时数也算上,之前就是因为只算了分钟数,使剩余时间计算错误
int PastMinutes = ((ts.Days * 24) + (ts.Hours * 60) + ts.Minutes);
remainTime(剩余时间) = numericUpDown1.Value(提醒间隔) - PastMinutes;
2.窗体渐变效果:
我这里是使用窗体的Opacity属性来实现的,先把Opacity置为0然后不但的去累加。
if (this.Opacity <= 0.9)
{
this.Opacity += 0.05;
}
else {
timer4.Stop();
timer4.Dispose();
}
提示:因为在渐变的过程中Windows要对窗体不停进行绘制,挺耗资源。
3.窗体样式:
使用SkinEngine样式控件(如果需要可以传给大家)+.ssk样式文件去实现
实现方式:可以IO类提取样式文件夹中所有文件的名称,绑定到下拉列表中,通过下拉列表的相应事件
可以对窗体的样式进行实时改变。
skinEngine1.SkinFile = AppDomain.CurrentDomain.BaseDirectory + 样式文件夹名 + "\\" + 样式文
件名+".ssk";
4.窗体托盘化:
使用.Net自带的NotifyIcon托盘控件去完成使用很简单。
//指定托盘图片这里是使用资源文件指定的,你也可以指定图片路径
this.notifyIcon1.Icon = global::PreventFatigue.Properties.Resources.MA_SMILE_9;
//指定托盘的名称
this.notifyIcon1.Text = this.Text;
//托盘单击事件中的代码
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
//窗体大小调整事件(Resize事件)中的代码
if (this.WindowState == FormWindowState.Minimized)
{
this.Visible = false;
this.notifyIcon1.Visible = true;
}
5.歌曲播放windowAPI代码:
大家可以使用这个类做其它类似的软件
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
namespace clsMCIPlay
{
/// <summary>
/// clsMci 的摘要说明。
/// </summary>
public class clsMCI
{
public clsMCI()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//定义API函数使用的字符串变量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
private string Name = "";
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
private string durLength = "";
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
private string TemStr = "";
int ilong;
//定义播放状态枚举变量
public enum State
{
mPlaying = 1,
mPuase = 2,
mStop = 3
};
//结构变量
public struct structMCI
{
public bool bMut;
public int iDur;
public int iPos;
public int iVol;
public int iBal;
public string iName;
public State state;
};
public structMCI mc = new structMCI();
/// <summary>
/// 取得和设置播放文件属性
/// </summary>
public string FileName
{
get
{
return mc.iName;
}
set
{
//ASCIIEncoding asc = new ASCIIEncoding();
try
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
Name = Name.PadLeft(260, Convert.ToChar(" "));
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
Name = GetCurrPath(Name);
//Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias
media";
Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias
media";
ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString("set media time format milliseconds", TemStr,
TemStr.Length, 0);
mc.state = State.mStop;
}
catch
{
throw new Exception("设置播放文件属性出错!");
}
}
}
/// <summary>
/// 播放
/// </summary>
public void play()
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
mc.state = State.mPlaying;
}
/// <summary>
/// 停止
/// </summary>
public void StopT()
{
TemStr = "";
TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
ilong = APIClass.mciSendString("close media", TemStr, 128, 0);
ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
mc.state = State.mStop;
}
/// <summary>
/// 暂停
/// </summary>
public void Puase()
{
TemStr = "";
TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
mc.state = State.mPuase;
}
/// <summary>
/// 获得当前路径
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private string GetCurrPath(string name)
{
if (name.Length < 1) return "";
name = name.Trim();
name = name.Substring(0, name.Length - 1);
return name;
}
/// <summary>
/// 总时间(秒钟)
/// </summary>
public int Duration
{
get
{
durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
durLength = durLength.Trim();
if (durLength == "") return 0;
return (int)(Convert.ToDouble(durLength) / 1000f);
}
}
/// <summary>
/// 当前时间(秒钟)
/// </summary>
public int CurrentPosition
{
get
{
durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
durLength = durLength.Trim();
mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
return mc.iPos;
}
}
}
/// <summary>
/// WindowsAPI类
/// </summary>
public class APIClass
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer
);
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
}