C#Windows應(yīng)用程序開(kāi)發(fā)之添加菜單
C#Windows應(yīng)用程序開(kāi)發(fā)之添加菜單的前言:通常windows應(yīng)用程序都有相似的特征:控件、菜單、工具條、狀態(tài)欄等等。每次我們開(kāi)始作一個(gè)新的windows應(yīng)用程序時(shí)都是以相同的事情開(kāi)始:建立項(xiàng)目,添加控件和事件處理器。如果我們有一個(gè)模板,那么我們就可以節(jié)約大量的時(shí)間了。
在介紹如何建立模板的過(guò)程中,將涉及大量的微軟.net framework類庫(kù)的基本知識(shí)。如果你沒(méi)有使用集成開(kāi)發(fā)環(huán)境那么本文介紹的模板對(duì)你將非常有用,如果你使用了visual studio.net這樣的集成開(kāi)發(fā)環(huán)境你也可以從中了解控件的工作方式,這對(duì)你也是很有用的。
C#Windows應(yīng)用程序開(kāi)發(fā)之添加菜單和菜單項(xiàng)
要找到?jīng)]有菜單的windows應(yīng)用程序非常困難,菜單使訪問(wèn)應(yīng)用程序的功能變得很簡(jiǎn)單,在大多數(shù)環(huán)境下可以最小的使用控件。菜單比控件占用更少的空間,同時(shí)使應(yīng)用程序顯得更有組織。
在System.Windows.Forms名稱空間中,所有與菜單相關(guān)的控件都是menu類的子類。menu是一個(gè)抽象類,你不能直接實(shí)例化,menu類有三個(gè)子類:
- ContextMenu
- MainMenu
- MenuItem
C#Windows應(yīng)用程序開(kāi)發(fā)之添加菜單之ContextMenu類表示快捷菜單,在控件或窗體區(qū)域點(diǎn)擊鼠標(biāo)右鍵時(shí)顯示??旖莶藛纬S糜诮M合窗體mainmenu類的菜單項(xiàng)給出應(yīng)用程序的上下文,這對(duì)于用戶時(shí)非常有用的。
MainMenu表示傳統(tǒng)的位于窗體頂部的菜單,你可以把它看成窗體菜單結(jié)構(gòu)的容器。一個(gè)菜單是由MenuItem表示的菜單項(xiàng)組成的,對(duì)于應(yīng)用程序而言每一個(gè)菜單項(xiàng)是一個(gè)命令或其它子菜單項(xiàng)的父菜單。form類都有一個(gè)menu屬性,采用將mainmenu對(duì)象賦給menu屬性的方式將mainmenu對(duì)象綁定到窗體。
C#Windows應(yīng)用程序開(kāi)發(fā)之添加菜單在這個(gè)模板中,我們沒(méi)有使用ContextMenu類,但我們示范了如何使用MainMenu和MenuItem類。我們首先需要在窗體中添加一個(gè)菜單,給窗體添加一個(gè)MainMenu對(duì)象。
MainMenu mainMenu = new MainMenu();
C#Windows應(yīng)用程序開(kāi)發(fā)之添加菜單和菜單項(xiàng)現(xiàn)在MainMenu對(duì)象中什么都沒(méi)有,下面我們給他添加一個(gè)MenuItem對(duì)象。在List1中主菜單稱為fileMenuItem,它的text屬性是&File,&表示他后面的字母帶下劃線,是該菜單的快捷鍵。通過(guò)使用Menu對(duì)象的MenuItemCollection的add方法為MainMenu添加一個(gè)或幾個(gè)MenuItem,這個(gè)集合可以通過(guò)menu類的MenuItems屬性訪問(wèn)。
- MenuItem fileMenuItem = new MenuItem();
- mainMenu.MenuItems.Add(fileMenuItem);
C#Windows應(yīng)用程序開(kāi)發(fā)之添加菜單和菜單項(xiàng)我們?cè)趂ileMenuItem 菜單項(xiàng)中還添加了其它MenuItem,這些MenuItem是fileMenuItem的子菜單。你也可以給子菜單添加子菜單。圖2顯示了菜單的等級(jí)結(jié)構(gòu)。
C#Windows應(yīng)用程序開(kāi)發(fā)之添加菜單項(xiàng)的聲明如下:
- MenuItem fileNewMenuItem;
- MenuItem fileOpenMenuItem;
- MenuItem fileSaveMenuItem;
- MenuItem fileSaveAsMenuItem;
- MenuItem fileMenuWithSubmenu;
- MenuItem submenuMenuItem;
- MenuItem fileExitMenuItem;
C#Windows應(yīng)用程序開(kāi)發(fā)之添加菜單和菜單項(xiàng)之MenuItem類有幾個(gè)構(gòu)造函數(shù),使用這些構(gòu)造函數(shù)實(shí)例化你的 MenuItem對(duì)象,并添加一個(gè)事件處理器。
- // the following constructor is the same as:
- // menuItem fileNewMenuItem = new MenuItem();
- // fileNewMenuItem.Text = "&New";
- // fileNewMenuItem.Shortcut = Shortcut.CtrlN;
- // fileNewMenuItem.Click += new
- // System.EventHandler(this.fileNewMenuItem_Click);
- fileNewMenuItem = new MenuItem("&New",
- new System.EventHandler(this.fileNewMenuItem_Click), Shortcut.CtrlN);
- fileOpenMenuItem = new MenuItem("&Open",
- new System.EventHandler(this.fileOpenMenuItem_Click), Shortcut.CtrlO);
- fileSaveMenuItem = new MenuItem("&Save",
- new System.EventHandler(this.fileSaveMenuItem_Click), Shortcut.CtrlS);
- fileSaveAsMenuItem = new MenuItem("Save &As",
- new System.EventHandler(this.fileSaveAsMenuItem_Click));
- fileMenuWithSubmenu = new MenuItem("&With Submenu");
- submenuMenuItem = new MenuItem("Su&bmenu",
- new System.EventHandler(this.submenuMenuItem_Click));
- fileExitMenuItem = new MenuItem("E&xit",
- new System.EventHandler(this.fileExitMenuItem_Click));
- Event handling is discussed further in the section "Adding Event Handlers."
- As mentioned, the menu items are added to the fileMenuItem control.
- fileMenuItem.MenuItems.Add(fileNewMenuItem);
- fileMenuItem.MenuItems.Add(fileOpenMenuItem);
- fileMenuItem.MenuItems.Add(fileSaveMenuItem);
- fileMenuItem.MenuItems.Add(fileSaveAsMenuItem);
- fileMenuItem.MenuItems.Add(fileMenuWithSubmenu);
- fileMenuWithSubmenu.MenuItems.Add(submenuMenuItem);
- fileMenuItem.MenuItems.Add("-"); // add a separator
- fileMenuItem.MenuItems.Add(fileExitMenuItem);
注意在菜單項(xiàng)之間可以創(chuàng)建一個(gè)分隔符,方法是使用menu類的MenuItems集合的add方法。上面的例子中使用的分隔符是"-"。
C#Windows應(yīng)用程序開(kāi)發(fā)之添加菜單和菜單項(xiàng)的基本情況就向你介紹到這里,希望對(duì)你學(xué)習(xí)和了解C#Windows應(yīng)用程序開(kāi)發(fā)之添加菜單和菜單項(xiàng)有所幫助。
【編輯推薦】