voith项目配置服务程序

项目需求:

1、程序可以最小化到任务栏
2、tpms标签和限速标签同时只能选择一个,并且要通过button确定修改
3、在程序中需要显示SequenceScanner1.0服务的运行状态
4、能够打开和关闭SequenceScanner1.0服务
5、在程序中建立一个链接,指向配置文件的地址

开发环境:Visual Studio2015基于.NET Framework3.5

实现步骤:

1、以管理员权限,新建一个winform项目,

2、设计程序界面

3、实现程序缩小化至任务栏小图标功能

A、拖动控件notifyIcon到设计界面,修改窗体关闭事件

  1. private void Form1_FormClosing(object sender, FormClosingEventArgs e) //程序关闭时最小化至任务栏
  2. {
  3. if(e.CloseReason==CloseReason.UserClosing) //当用户点击右上角X按钮或者Alt+F4时
  4. {
  5. e.Cancel = true;
  6. this.ShowInTaskbar = false; //判断是否显示窗体
  7. this.myIcon.Icon = this.Icon; //显示任务栏小图标
  8. this.Hide(); //隐藏icon控件
  9. }
  10. }

B、修改notifyIcon的单击事件

  1. private void myIcon_MouseClick(object sender, MouseEventArgs e) //单击任务栏图标事件
  2. {
  3. if(e.Button==MouseButtons.Right) //如果鼠标右击任务栏图标
  4. {
  5. myMenu.Show(); //展示选项
  6. }
  7. if(e.Button==MouseButtons.Left) //如果鼠标左击任务栏图标
  8. {
  9. this.Visible = true;
  10. this.WindowState = FormWindowState.Normal; //显示程序窗体
  11. }
  12. }

C、拖动一个ContextMenuStrip控件作为icon的右键菜单选项,其中设置一个退出选项,修改其click事件

  1. private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) //任务栏图标右击退出程序
  2. {
  3. //弹出提示框,决定是否退出,如果确定就往下执行
  4. if (MessageBox.Show("您确定要退出程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
  5. {
  6. this.myIcon.Visible = false; //任务栏图标消失
  7. this.Close(); //关闭窗体
  8. this.Dispose(); //释放程序使用的资源
  9. Application.Exit(); //退出程序
  10. }
  11. }

4、通过button打开配置文件地址(绝对路径下)

  1. private void openfilepath_Click(object sender, EventArgs e)
  2. {
  3. //这是绝对路径
  4. string path1 = @"C:\Windows\System32"; //地址位置
  5. System.Diagnostics.Process.Start("explorer",path1);
  6. //如有必要可添加为相对路径
  7. }

5、引用能够对服务进行调用的库

6、获取服务状态(输入服务名称)

  1. public void CheckServerState(string ServiceName) //检测服务状态函数
  2. {
  3. ServiceController[] service = ServiceController.GetServices();
  4. bool isStart = false;
  5. bool isExite = false;
  6. for(int i=0;i<service.Length;i++)
  7. {
  8. if(service[i].ServiceName.ToUpper().Equals(ServiceName.ToUpper()))
  9. {
  10. isExite = true;
  11. if(service[i].Status==ServiceControllerStatus.Running)
  12. {
  13. isStart = true;
  14. break;
  15. }
  16. }
  17. }
  18. if(!isExite) //服务是否存在
  19. {
  20. this.ServiceStatus.Text = ("不存在此服务");
  21. }else
  22. {
  23. if(isStart) //服务是否启动
  24. {
  25. this.ServiceStatus.Text = ("此服务已启动");
  26. }
  27. else
  28. {
  29. this.ServiceStatus.Text = ("此服务已关闭");
  30. }
  31. }
  32. }
  33. private void getServerStatus_Click(object sender, EventArgs e) //服务检测
  34. {
  35. // CheckServerState("SequenceScanner1.0"); //服务名称,不是显示名称
  36. CheckServerState("iphlpsvc"); //测试用---服务名称,不是显示名称
  37. }

7、打开服务/关闭服务(输入显示名称)

打开服务

  1. //首先根据服务状态决定打开/关闭服务
  2. if (ServiceStatus.Text == "不存在此服务")
  3. {
  4. MessageBox.Show("此服务未存在");
  5. }
  6. if (ServiceStatus.Text == "此服务已启动")
  7. {
  8. MessageBox.Show("服务已启动");
  9. }
  10. if (ServiceStatus.Text == "此服务已关闭")
  11. {
  12. // ServiceController sc = new ServiceController("SequenceScanner1.0");
  13. ServiceController sc = new ServiceController("IP Helper"); //测试服务
  14. if(!sc.CanStop)
  15. {
  16. sc.Start(); //启动服务,不传递参数
  17. sc.WaitForStatus(ServiceControllerStatus.Running); //服务当前状态
  18. ServiceStatus.Text = ("此服务已启动");
  19. }
  20. sc.Close(); //释放对该服务的控制权以及相应的资源
  21. }
  22. }

关闭服务

  1. private void closeService_Click(object sender, EventArgs e) //关闭服务
  2. {
  3. if (ServiceStatus.Text == "不存在此服务")
  4. {
  5. MessageBox.Show("此服务未存在");
  6. }
  7. if (ServiceStatus.Text == "此服务已关闭")
  8. {
  9. MessageBox.Show("服务已关闭");
  10. }
  11. if (ServiceStatus.Text == "此服务已启动")
  12. {
  13. // ServiceController sc = new ServiceController("SequenceScanner1.0");
  14. ServiceController sc = new ServiceController("IP Helper"); //测试服务
  15. if (sc.CanStop)
  16. {
  17. sc.Stop(); //停止服务
  18. sc.WaitForStatus(ServiceControllerStatus.Stopped); //服务当前状态已停止
  19. ServiceStatus.Text = ("此服务已关闭");
  20. }
  21. sc.Close(); //释放对该服务的控制权以及相应的资源
  22. }
  23. }

8、鼠标悬停在帮助按键,显示提示功能

  1. private void help_Click(object sender, EventArgs e) //当鼠标悬停时,启动提示
  2. {
  3. ToolTip ttpSetting = new ToolTip(); //提示控件
  4. ttpSetting.InitialDelay = 50;
  5. ttpSetting.AutoPopDelay = 10 * 1000; //提示可见时间
  6. // ttpSetting.ReshowDelay = 100;
  7. ttpSetting.ShowAlways = true; //是否总显示提示窗口
  8. // ttpSetting.IsBalloon = true;
  9. ttpSetting.SetToolTip(this.help, "联系电话:18914250037"); //显示提示内容
  10. }

附件列表

     

    posted @ 2017-07-23 09:29  sky七月凉  阅读(215)  评论(0编辑  收藏  举报