Winform最小化托盤

2022-01-13 12:41:47 字數 3165 閱讀 3793

1.設定winform窗體屬性showintask=false

2.加notifyicon控制項notifyicon1,為控制項notifyicon1的屬性icon新增乙個icon圖示。

3.新增窗體最小化事件(首先需要新增事件引用):

this.sizechanged += new system.eventhandler(this.form1_sizechanged); 

//上面一行是主窗體initializecomponent()方法中需要新增的引用

private

void form1_sizechanged(object sender, eventargs e)

}

4.新增點選圖示事件(首先需要新增事件引用):

private

void notifyicon1_click(object sender, eventargs e)

5.可以給notifyicon新增右鍵選單:

主窗體中拖入乙個contextmenu控制項nicontextmenu,點中控制項,在上下文選單中新增選單,notifyicon1的contextmenu行為中選中nicontextmenu 作為上下文選單。

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

this.nicontextmenu = new system.windows.forms.contextmenu();

this.menuitem_hide = new system.windows.forms.menuitem();

this.menuitem_show = new system.windows.forms.menuitem();

this.menuitem_aubot = new system.windows.forms.menuitem();

this.menuitem_exit = new system.windows.forms.menuitem();

this.notifyicon1.contextmenu = this.nicontextmenu;

this.notifyicon1.icon = ((system.drawing.icon)(resources.getobject( "

notifyicon.icon

")));

this.notifyicon1.text = "

"; this.notifyicon1.visible = true;

this.notifyicon1.doubleclick += new system.eventhandler(this.notifyicon1_doubleclick);

this.notifyicon1.click += new system.eventhandler(this.notifyicon1_click);

this.nicontextmenu.menuitems.addrange(

new system.windows.forms.menuitem

); ////

menuitem_hide

= 0;

this.menuitem_hide.text = "

隱藏 ";

this.menuitem_hide.click += new system.eventhandler(this.menuitem_hide_click);

////

menuitem_show

= 1;

this.menuitem_show.text = "

顯示 ";

this.menuitem_show.click += new system.eventhandler(this.menuitem_show_click);

////

menuitem_aubot

= 2;

this.menuitem_aubot.text = "

關於 ";

this.menuitem_aubot.click += new system.eventhandler(this.menuitem_aubot_click);

////

menuitem_exit

= 3;

this.menuitem_exit.text = "

退出 ";

this.menuitem_exit.click += new system.eventhandler(this.menuitem_exit_click);

protected

override

void onclosing(canceleventargs e)

protected

override

void onclosing(canceleventargs e)

private

void hidectiserver()

private

void showctiserver()

private

void ctimaniform_closing(object sender, system.componentmodel.canceleventargs e)

private

void menuitem_show_click(object sender, system.eventargs e)

private

void menuitem_aubot_click(object sender, system.eventargs e)

private

void menuitem_exit_click(object sender, system.eventargs e)

private

void menuitem_hide_click(object sender, system.eventargs e)

private

void ctimaniform_sizechanged(object sender, system.eventargs e) }

private

void notifyicon1_doubleclick(object sender,system.eventargs e)

winform最小化到托盤

做winform程式的經常會用想qq那樣把視窗最小化到右下角工作列中,並顯示小圖示。當單擊或者雙擊小圖示的時候需要把視窗恢復。一 設定最小化 需要用到乙個控制項notifyicon,直接從工具箱拖到頁面即可。最小化和雙擊恢復,需要新增兩個事件 最小化的話是新增resize事件到form窗體,而雙擊恢...

WinForm最小化到托盤以及托盤右鍵選單

首先,先拖乙個notifyicon到主窗體,然後設定notifyicon的圖示,不然等下最小化後,都找不到那個程式了,還有那個text也是,不寫名字,就預設是notifyicon了.如下圖 然後雙擊notifyicon,如下 新增雙擊托盤圖示事件 雙擊顯示視窗 private void notify...

WinForm最小化到托盤以及托盤右鍵選單

首先,先拖乙個notifyicon到主窗體,然後設定notifyicon的圖示,不然等下最小化後,都找不到那個程式了,還有那個text也是,不寫名字,就預設是notifyicon了.如下圖 然後雙擊notifyicon,如下 新增雙擊托盤圖示事件 雙擊顯示視窗 private void notify...