项目需求:
1、程序可以最小化到任务栏
2、tpms标签和限速标签同时只能选择一个,并且要通过button确定修改
3、在程序中需要显示SequenceScanner1.0服务的运行状态
4、能够打开和关闭SequenceScanner1.0服务
5、在程序中建立一个链接,指向配置文件的地址
开发环境:Visual Studio2015基于.NET Framework3.5
实现步骤:
1、以管理员权限,新建一个winform项目,
2、设计程序界面
3、实现程序缩小化至任务栏小图标功能
A、拖动控件notifyIcon到设计界面,修改窗体关闭事件
private void Form1_FormClosing(object sender, FormClosingEventArgs e) //程序关闭时最小化至任务栏
{
if(e.CloseReason==CloseReason.UserClosing) //当用户点击右上角X按钮或者Alt+F4时
{
e.Cancel = true;
this.ShowInTaskbar = false; //判断是否显示窗体
this.myIcon.Icon = this.Icon; //显示任务栏小图标
this.Hide(); //隐藏icon控件
}
}
B、修改notifyIcon的单击事件
private void myIcon_MouseClick(object sender, MouseEventArgs e) //单击任务栏图标事件
{
if(e.Button==MouseButtons.Right) //如果鼠标右击任务栏图标
{
myMenu.Show(); //展示选项
}
if(e.Button==MouseButtons.Left) //如果鼠标左击任务栏图标
{
this.Visible = true;
this.WindowState = FormWindowState.Normal; //显示程序窗体
}
}
C、拖动一个ContextMenuStrip控件作为icon的右键菜单选项,其中设置一个退出选项,修改其click事件
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) //任务栏图标右击退出程序
{
//弹出提示框,决定是否退出,如果确定就往下执行
if (MessageBox.Show("您确定要退出程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
this.myIcon.Visible = false; //任务栏图标消失
this.Close(); //关闭窗体
this.Dispose(); //释放程序使用的资源
Application.Exit(); //退出程序
}
}
4、通过button打开配置文件地址(绝对路径下)
private void openfilepath_Click(object sender, EventArgs e)
{
//这是绝对路径
string path1 = @"C:WindowsSystem32"; //地址位置
System.Diagnostics.Process.Start("explorer",path1);
//如有必要可添加为相对路径
}
5、引用能够对服务进行调用的库
6、获取服务状态(输入服务名称)
public void CheckServerState(string ServiceName) //检测服务状态函数
{
ServiceController[] service = ServiceController.GetServices();
bool isStart = false;
bool isExite = false;
for(int i=0;i<service.Length;i++)
{
if(service[i].ServiceName.ToUpper().Equals(ServiceName.ToUpper()))
{
isExite = true;
if(service[i].Status==ServiceControllerStatus.Running)
{
isStart = true;
break;
}
}
}
if(!isExite) //服务是否存在
{
this.ServiceStatus.Text = ("不存在此服务");
}else
{
if(isStart) //服务是否启动
{
this.ServiceStatus.Text = ("此服务已启动");
}
else
{
this.ServiceStatus.Text = ("此服务已关闭");
}
}
}
private void getServerStatus_Click(object sender, EventArgs e) //服务检测
{
// CheckServerState("SequenceScanner1.0"); //服务名称,不是显示名称
CheckServerState("iphlpsvc"); //测试用---服务名称,不是显示名称
}
7、打开服务/关闭服务(输入显示名称)
打开服务
//首先根据服务状态决定打开/关闭服务
if (ServiceStatus.Text == "不存在此服务")
{
MessageBox.Show("此服务未存在");
}
if (ServiceStatus.Text == "此服务已启动")
{
MessageBox.Show("服务已启动");
}
if (ServiceStatus.Text == "此服务已关闭")
{
// ServiceController sc = new ServiceController("SequenceScanner1.0");
ServiceController sc = new ServiceController("IP Helper"); //测试服务
if(!sc.CanStop)
{
sc.Start(); //启动服务,不传递参数
sc.WaitForStatus(ServiceControllerStatus.Running); //服务当前状态
ServiceStatus.Text = ("此服务已启动");
}
sc.Close(); //释放对该服务的控制权以及相应的资源
}
}
关闭服务
private void closeService_Click(object sender, EventArgs e) //关闭服务
{
if (ServiceStatus.Text == "不存在此服务")
{
MessageBox.Show("此服务未存在");
}
if (ServiceStatus.Text == "此服务已关闭")
{
MessageBox.Show("服务已关闭");
}
if (ServiceStatus.Text == "此服务已启动")
{
// ServiceController sc = new ServiceController("SequenceScanner1.0");
ServiceController sc = new ServiceController("IP Helper"); //测试服务
if (sc.CanStop)
{
sc.Stop(); //停止服务
sc.WaitForStatus(ServiceControllerStatus.Stopped); //服务当前状态已停止
ServiceStatus.Text = ("此服务已关闭");
}
sc.Close(); //释放对该服务的控制权以及相应的资源
}
}
8、鼠标悬停在帮助按键,显示提示功能
private void help_Click(object sender, EventArgs e) //当鼠标悬停时,启动提示
{
ToolTip ttpSetting = new ToolTip(); //提示控件
ttpSetting.InitialDelay = 50;
ttpSetting.AutoPopDelay = 10 * 1000; //提示可见时间
// ttpSetting.ReshowDelay = 100;
ttpSetting.ShowAlways = true; //是否总显示提示窗口
// ttpSetting.IsBalloon = true;
ttpSetting.SetToolTip(this.help, "联系电话:18914250037"); //显示提示内容
}