孟子e章給出的vb代碼,現(xiàn)在從這個(gè)代碼翻譯成c#,給習(xí)慣c#的人參考
需要注意的是圖片一定需要是ico格式,否則可能會(huì)導(dǎo)致托盤的不顯示
using system;
namespace ubiserialscontroller
{
/// <summary>
/// class1 的摘要說明。
/// </summary>
public class class1
{
static system.serviceprocess.servicecontroller sc;
static system.windows.forms.notifyicon ni;
static system.windows.forms.contextmenu cm;
static system.timers.timer timer;
public class1()
{
//
// todo: 在此處添加構(gòu)造函數(shù)邏輯
//
}
static void main()
{
try
{
sc=new system.serviceprocess.servicecontroller("ubiserials");
ni=new system.windows.forms.notifyicon();
ni.visible=false;
cm=new system.windows.forms.contextmenu();
cm.menuitems.add(new system.windows.forms.menuitem("停止",new eventhandler(stopservice)));
cm.menuitems.add(new system.windows.forms.menuitem("暫停",new eventhandler(pauseservice)));
cm.menuitems.add(new system.windows.forms.menuitem("繼續(xù)",new eventhandler(contiuneservice)));
cm.menuitems.add(new system.windows.forms.menuitem("開始",new eventhandler(startservice)));
cm.menuitems.add("-");
cm.menuitems.add(new system.windows.forms.menuitem("關(guān)于",new eventhandler(about)));
cm.menuitems.add(new system.windows.forms.menuitem("退出",new eventhandler(exit)));
ni.contextmenu=cm;
ni.visible=true;
setuptimer();
system.windows.forms.application.run();
}
catch(system.exception ex)
{
system.windows.forms.messagebox.show(ex.message.tostring());
}
}
private static void stopservice(object sender,system.eventargs e)
{
if(sc.status==system.serviceprocess.servicecontrollerstatus.running&&sc.canstop==true)
{
try
{
sc.stop();
}
catch(system.exception ex)
{
system.windows.forms.messagebox.show(ex.message.tostring());
}
}
}
private static void pauseservice(object sender,system.eventargs e)
{
if(sc.status!=system.serviceprocess.servicecontrollerstatus.paused&&sc.canpauseandcontinue==true)
{
try
{
sc.pause();
}
catch(system.exception ex)
{
system.windows.forms.messagebox.show(ex.message.tostring());
}
}
}
private static void contiuneservice(object sender,system.eventargs e)
{
if(sc.status==system.serviceprocess.servicecontrollerstatus.paused&&sc.canpauseandcontinue==true)
{
try
{
sc.continue();
}
catch(system.exception ex)
{
system.windows.forms.messagebox.show(ex.message.tostring());
}
}
}
private static void startservice(object sender,system.eventargs e)
{
if(sc.status==system.serviceprocess.servicecontrollerstatus.stopped)
{
try
{
sc.start();
}
catch(system.exception ex)
{
system.windows.forms.messagebox.show(ex.message.tostring());
}
}
}
private static void about(object sender,system.eventargs e)
{
system.windows.forms.messagebox.show("2005.4.3","關(guān)于");
}
private static void exit(object sender,system.eventargs e)
{
try
{
timer.dispose();
sc.dispose();
cm.dispose();
ni.dispose();
system.windows.forms.application.exit();
}
catch(system.exception ex)
{
system.windows.forms.messagebox.show(ex.message.tostring());
}
}
private static void timer_elapsed(object sender, system.timers.elapsedeventargs e)
{
getservicestate();
}
private static void getservicestate()
{
sc.refresh();
switch(sc.status)
{
case system.serviceprocess.servicecontrollerstatus.stopped:
{
ni.icon=new system.drawing.icon("stopped.ico");
cm.menuitems[0].enabled=false;
cm.menuitems[1].enabled=false;
cm.menuitems[2].enabled=false;
cm.menuitems[3].enabled=true;
break;
}
case system.serviceprocess.servicecontrollerstatus.running:
{
ni.icon=new system.drawing.icon("started.ico");
cm.menuitems[0].enabled=true;
cm.menuitems[1].enabled=true;
cm.menuitems[2].enabled=false;
cm.menuitems[3].enabled=false;
break;
}
case system.serviceprocess.servicecontrollerstatus.paused:
{
ni.icon=new system.drawing.icon("paused.ico");
cm.menuitems[0].enabled=false;
cm.menuitems[1].enabled=false;
cm.menuitems[2].enabled=true;
cm.menuitems[3].enabled=false;
break;
}
default:
{
ni.icon=new system.drawing.icon("paused.ico");
break;
}
}
}
private static void setuptimer()
{
timer=new system.timers.timer();
timer.interval=500;
timer.elapsed += new system.timers.elapsedeventhandler(timer_elapsed);
timer.start();
}
}
}
新聞熱點(diǎn)
疑難解答
圖片精選