溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么用C#Windows應(yīng)用程序開發(fā)添加菜單

發(fā)布時(shí)間:2021-07-16 12:24:58 來源:億速云 閱讀:439 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“怎么用C#Windows應(yīng)用程序開發(fā)添加菜單”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用C#Windows應(yīng)用程序開發(fā)添加菜單”吧!

C#Windows應(yīng)用程序開發(fā)之添加菜單的前言:通常windows應(yīng)用程序都有相似的特征:控件、菜單、工具條、狀態(tài)欄等等。每次我們開始作一個(gè)新的windows應(yīng)用程序時(shí)都是以相同的事情開始:建立項(xiàng)目,添加控件和事件處理器。如果我們有一個(gè)模板,那么我們就可以節(jié)約大量的時(shí)間了。

在介紹如何建立模板的過程中,將涉及大量的微軟.net framework類庫的基本知識(shí)。如果你沒有使用集成開發(fā)環(huán)境那么本文介紹的模板對(duì)你將非常有用,如果你使用了visual studio.net這樣的集成開發(fā)環(huán)境你也可以從中了解控件的工作方式,這對(duì)你也是很有用的。

C#Windows應(yīng)用程序開發(fā)之添加菜單和菜單項(xiàng)

要找到?jīng)]有菜單的windows應(yīng)用程序非常困難,菜單使訪問應(yīng)用程序的功能變得很簡單,在大多數(shù)環(huán)境下可以最小的使用控件。菜單比控件占用更少的空間,同時(shí)使應(yīng)用程序顯得更有組織。

在System.Windows.Forms名稱空間中,所有與菜單相關(guān)的控件都是menu類的子類。menu是一個(gè)抽象類,你不能直接實(shí)例化,menu類有三個(gè)子類:

ContextMenu   MainMenu   MenuItem

C#Windows應(yīng)用程序開發(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)用程序開發(fā)之添加菜單在這個(gè)模板中,我們沒有使用ContextMenu類,但我們示范了如何使用MainMenu和MenuItem類。我們首先需要在窗體中添加一個(gè)菜單,給窗體添加一個(gè)MainMenu對(duì)象。

MainMenu mainMenu = new MainMenu();

C#Windows應(yīng)用程序開發(fā)之添加菜單和菜單項(xiàng)現(xiàn)在MainMenu對(duì)象中什么都沒有,下面我們給他添加一個(gè)MenuItem對(duì)象。在List1中主菜單稱為fileMenuItem,它的text屬性是&File,&表示他后面的字母帶下劃線,是該菜單的快捷鍵。通過使用Menu對(duì)象的MenuItemCollection的add方法為MainMenu添加一個(gè)或幾個(gè)MenuItem,這個(gè)集合可以通過menu類的MenuItems屬性訪問。

MenuItem fileMenuItem = new MenuItem();   mainMenu.MenuItems.Add(fileMenuItem);

C#Windows應(yīng)用程序開發(fā)之添加菜單和菜單項(xiàng)我們?cè)趂ileMenuItem 菜單項(xiàng)中還添加了其它MenuItem,這些MenuItem是fileMenuItem的子菜單。你也可以給子菜單添加子菜單。圖2顯示了菜單的等級(jí)結(jié)構(gòu)。

怎么用C#Windows應(yīng)用程序開發(fā)添加菜單

C#Windows應(yīng)用程序開發(fā)之添加菜單項(xiàng)的聲明如下:

MenuItem fileNewMenuItem;   MenuItem fileOpenMenuItem;   MenuItem fileSaveMenuItem;   MenuItem fileSaveAsMenuItem;   MenuItem fileMenuWithSubmenu;   MenuItem submenuMenuItem;   MenuItem fileExitMenuItem;

C#Windows應(yīng)用程序開發(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方法。上面的例子中使用的分隔符是"-"。

到此,相信大家對(duì)“怎么用C#Windows應(yīng)用程序開發(fā)添加菜單”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI