using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Windows.Forms;
namespace DIYService
{
public partial class FormServices : Form
{
#region 自定义对象
private readonly Stopwatch stopWatch = new Stopwatch();
private readonly ServiceController currentService = new ServiceController();
#endregion
#region AnimateWindow
[DllImport("user32.dll", EntryPoint = "AnimateWindow")]
private static extern bool AnimateWindow(IntPtr handle, int ms, int flags);
#endregion
public FormServices()
{
#region
InitializeComponent();
this.TopMost = true; // 前端显示。
this.ShowInTaskbar = false; // 在 Windows 任务栏中隐藏窗体。
this.HelpButton = true; // 显示“帮助”按钮。
this.MaximizeBox = false; // 隐藏“最大化”按钮。
this.MinimizeBox = false; // 隐藏“最小化”按钮。
this.AutoSizeMode = AutoSizeMode.GrowAndShrink; // 禁用手动调整大小。
this.SizeGripStyle = SizeGripStyle.Hide; // 隐藏调整大小手柄。
this.FormBorderStyle = FormBorderStyle.Sizable; // 可调整大小的边框。
this.StartPosition = FormStartPosition.Manual; // 窗体的位置由 Location 属性确定。
this.DesktopLocation = new Point(Screen.GetWorkingArea(this).Size - this.Size);
notifyService.Text = AppDomain.CurrentDomain.FriendlyName;
currentService.MachineName = Environment.MachineName;
var query = from service in ServiceController.GetServices()
let DN = service.DisplayName
let SN = service.ServiceName
let ST = (service.ServiceType == ServiceType.Win32OwnProcess)
where ST
orderby DN ascending
select new { DN, SN };
cmbServiceName.Sorted = false; // 绑定数据时,禁用排序。
cmbServiceName.ValueMember = "SN"; // 绑定数据之前赋值。
cmbServiceName.DataSource = query.ToList();
cmbServiceName.DisplayMember = "DN";
cmbMachineName.SelectedIndex = cmbMachineName.Items.Add(Environment.MachineName);
#endregion
}
#region Service.Refresh
private void timerStatus_Tick(object sender, EventArgs e)
{
currentService.Refresh(); // 刷新当前服务属性值。
switch (currentService.Status)
{
case ServiceControllerStatus.Running:
stopWatch.Stop();
btnStart.Enabled = false;
btnPause.Enabled = currentService.CanPauseAndContinue;
btnStop.Enabled = currentService.CanStop;
btnPause.Text = "暂停(&P)";
break;
case ServiceControllerStatus.Paused:
stopWatch.Stop();
btnStart.Enabled = false;
btnPause.Enabled = true;
btnStop.Enabled = false;
btnPause.Text = "继续(&C)";
break;
case ServiceControllerStatus.Stopped:
stopWatch.Stop();
btnStart.Enabled = true;
btnPause.Enabled = false;
btnStop.Enabled = false;
break;
}
labelStatus.Text = string.Format("Status:{0}", currentService.Status);
labelTime.Text = string.Format("in {0:#,##0} ms", stopWatch.ElapsedMilliseconds);
}
#endregion
#region Service.ReServiceName
private void cmbServiceName_SelectedIndexChanged(object sender, EventArgs e)
{
timerStatus.Stop();
currentService.ServiceName = Convert.ToString(cmbServiceName.SelectedValue);
timerStatus.Start();
}
#endregion
#region Service.ReStatus
private void buttonReStatus_Click(object sender, EventArgs e)
{
stopWatch.Reset();
stopWatch.Start();
currentService.Refresh();
switch (currentService.Status)
{
case ServiceControllerStatus.Stopped:
try
{
currentService.Start(); // net start ServiceName
}
catch (Exception se)
{
MessageBox.Show(this, se.Message, currentService.DisplayName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
break;
case ServiceControllerStatus.Running:
if (btnPause.Equals(sender))
currentService.Pause(); // net pause ServiceName
else
currentService.Stop(); // net stop ServiceName
break;
case ServiceControllerStatus.Paused:
currentService.Continue(); // net continue ServiceName
break;
}
}
#endregion
#region FormVisible
private void visibleMenuItem_Click(object sender, EventArgs e)
{
this.Visible = !this.Visible;
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (this.Visible)
{
AnimateWindow(this.Handle, 1000, 0x20010); // 居中逐渐显示。
this.Activate(); // 激活窗体并给予它焦点。
}
else
{
AnimateWindow(this.Handle, 1000, 0x10010); // 居中逐渐隐藏。
notifyService.ShowBalloonTip(10, notifyService.Text, " 在系统托盘中运行", ToolTipIcon.Info);
}
}
#endregion
#region FormExit
private void exitMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show(this, "您真的要退出“服务控制器”系统吗?", "系统询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
Application.Exit();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
switch (e.CloseReason)
{
case CloseReason.ApplicationExitCall:
AnimateWindow(this.Handle, 1000, 0x10010); // 居中逐渐隐藏。
notifyService.Visible = false;
currentService.Close();
break;
case CloseReason.UserClosing:
e.Cancel = true;
this.Visible = false;
break;
}
}
#endregion
#region HelpButton
protected override void OnHelpButtonClicked(System.ComponentModel.CancelEventArgs e)
{
base.OnHelpButtonClicked(e);
e.Cancel = true;
Process.Start("services.msc").Close();
}
#endregion
#region WndProc
protected override void WndProc(ref Message m)
{
if (m.Msg == 0xA1 && m.WParam.ToInt32() == 2)
return;
base.WndProc(ref m);
}
#endregion
}
}