欢迎光临
我们一直在努力

Visaul C#托盘程序制作心得-.NET教程,C#语言

建站超值云服务器,限时71元/月

首先,当然要引入notifyicon控件。

private system.windows.forms.notifyicon notifyiconserver;

this.notifyiconserver = new system.windows.forms.notifyicon(this.components);

接下来设置控件的各项属性:

//

// notifyiconserver

//

this.notifyiconserver.contextmenu = this.contextmenutray;//指定上下文菜单

this.notifyiconserver.icon = ((system.drawing.icon)(resources.getobject("notifyiconserver.icon")));//指定图标

this.notifyiconserver.text = "my server";//指定鼠标悬停显示

this.notifyiconserver.mousedown += new system.windows.forms.mouseeventhandler(this.notifyiconserver_mousedown);

this.notifyiconserver.doubleclick += new system.eventhandler(this.notifyiconserver_doubleclick);

//

// contextmenutray 上下文菜单

//

this.contextmenutray.menuitems.addrange(new system.windows.forms.menuitem[] {

this.menuitem1,

this.menuitem2});

//

// menuitem1

//

this.menuitem1.index = 0;

this.menuitem1.text = "打开 chat server";

this.menuitem1.click += new system.eventhandler(this.menuitem1_click);

//

// menuitem2

//

this.menuitem2.index = 1;

this.menuitem2.text = "退出程序";

this.menuitem2.click += new system.eventhandler(this.menuitem2_click);

用户点击窗体的“关闭”小按钮时,并不真正关闭窗体,而是将程序放到系统托盘。

private void chatform_closing(object sender, system.componentmodel.canceleventargs e)

{

e.cancel = true; // 取消关闭窗体

this.hide();

this.showintaskbar = false;

this.notifyiconserver.visible = true;//显示托盘图标

}

notifyicon的双击事件,可以恢复程序窗体:

private void notifyiconserver_doubleclick(object sender, system.eventargs e)

{

this.show();

if (this.windowstate == formwindowstate.minimized)

this.windowstate = formwindowstate.normal;

this.activate();

}

附加要求:单击鼠标左键也可调出一菜单。

解决方案如下:

首先声明一个上下文菜单:

//

// contextmenuleft 左键菜单

//

this.contextmenuleft.menuitems.addrange(new system.windows.forms.menuitem[] {

this.menuitem3});

//

// menuitem3

//

this.menuitem3.index = 0;

this.menuitem3.text = "关于……";

由于不能在notifyicon上直接显示上下文菜单,只有创建一个control作为容器,这是权宜之计,应该有更好的方法吧。

private void notifyiconserver_mousedown(object sender, system.windows.forms.mouseeventargs e)

{

if(e.button==mousebuttons.left)

{

control control = new control(null,control.mouseposition.x,control.mouseposition.y,1,1);

control.visible = true;

control.createcontrol();

point pos = new point(0,0);//这里的两个数字要根据你的上下文菜单大小适当地调整

this.contextmenuleft.show(control,pos);

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Visaul C#托盘程序制作心得-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章