你說的“樹形菜單”是指下拉菜單(可以有子菜單)嗎?如果是,VB.net提供了MenuStrip控件,在工具箱里能找到,直接就在對應(yīng)位置打字就可以了,完全是“所見即所得”。
網(wǎng)站建設(shè)、網(wǎng)站制作,成都做網(wǎng)站公司-成都創(chuàng)新互聯(lián)公司已向成百上千家企業(yè)提供了,網(wǎng)站設(shè)計(jì),網(wǎng)站制作,網(wǎng)絡(luò)營銷等服務(wù)!設(shè)計(jì)與技術(shù)結(jié)合,多年網(wǎng)站推廣經(jīng)驗(yàn),合理的價格為您打造企業(yè)品質(zhì)網(wǎng)站。
可以做一個基窗體作為父窗體,父窗體上有左邊的菜單欄
然后所有畫面都繼承這個父窗體,然后所有頁面都會有父窗體左邊菜單欄了,每個頁面內(nèi)容自己加控件就行 就會顯示都右邊的
不知你是否理解 不理解可追問 。。。
五.繪制個性化菜單
先執(zhí)行以下操作步驟 下列步驟是通過菜單編輯器設(shè)計(jì)一個簡單的菜單 為后面重新繪制做基礎(chǔ)
啟動Visual Studio Net
選擇菜單【文件】|【新建】|【項(xiàng)目】后 彈出【新建項(xiàng)目】對話框
將【項(xiàng)目類型】設(shè)置為【Visual Basic項(xiàng)目】
將【模板】設(shè)置為【W(wǎng)indows應(yīng)用程序】
在【名稱】文本框中輸入【自己畫菜單】
在【位置】的文本框中輸入【E:\VS NET項(xiàng)目】 然后單擊【確定】按鈕 這樣在 E:\VS NET項(xiàng)目 目錄中就產(chǎn)生了名稱為 自己畫菜單 的文件夾 并在里面創(chuàng)建了名稱為 自己畫菜單 的項(xiàng)目文件
把Visual Studio Net的當(dāng)前窗口切換到【Form vb(設(shè)計(jì))】窗口 并從【工具箱】中的【W(wǎng)indows窗體組件】選項(xiàng)卡中往Form 窗體中拖入下列組件
一個MainMenu組件 名稱為 MainMenu
選中 MainMenu 組件 單擊鼠標(biāo)右鍵 在彈出的菜單中選擇 編輯菜單 并按照圖 所示界面設(shè)計(jì)菜單
圖 【自己畫菜單】項(xiàng)目設(shè)計(jì)界面之一
此時保存上述步驟 并單擊快捷鍵F 則得到圖 所示界面
圖 【自己畫菜單】運(yùn)行界面之一
這樣通過菜單編輯器就完成了一個非常普通的菜單 下面就對此菜單進(jìn)行改造 在改造之前 要先設(shè)定項(xiàng)目中的三個MenuItem類實(shí)例的OwnerDraw屬性值為 True 因?yàn)橹挥写藢傩灾禐?True 才會觸發(fā)繪制菜單時所需要的DrawItem事件和MeasureItem事件 之后再在上面項(xiàng)目的基礎(chǔ)上執(zhí)行下一步操作
把Visual Stuido Net的當(dāng)前窗口切換到Form vb的代碼編輯窗口 并在InitializeComponent過程之后添加下列代碼 下列代碼是繪制 文件 菜單項(xiàng) 其作用是改變 文件 菜單項(xiàng)的字體 大小和菜單項(xiàng)的 其具體的繪制方法請參考下列代碼中的注釋
Private Sub MenuItem _DrawItem ( ByVal sender As Object ByVal e As System Windows Forms DrawItemEventArgs ) Handles MenuItem DrawItem Dim rfBound As RectangleF = New RectangleF ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲到RectangleF類型實(shí)例中 Dim rfBound As Rectangle = New Rectangle ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲到Rectangle類型實(shí)例中 Rectangle類型實(shí)例和RectangleF類型實(shí)例差不多 但在后面代碼中繪制菜單的函數(shù)是有區(qū)別的 e Graphics FillRectangle(New SolidBrush(Color LightGreen) rfBound) 以LightGreen色彩填充MenuItem 菜單項(xiàng)對應(yīng)的矩形區(qū)域 Dim s As MenuItem = CType ( sender MenuItem ) Dim s As String = s Text 獲得MenuItem 菜單項(xiàng)的名稱 Dim sfTemp As StringFormat = New StringFormat ( ) sfTemp Alignment = StringAlignment Center 設(shè)定要畫的菜單名稱的對齊方式 中間對齊 e Graphics DrawString ( s New Font ( 宋體 FontStyle Bold ) New SolidBrush ( Color Black ) rfBound sfTemp ) 以中間對齊方式 指定字體 大小 在指定的矩形區(qū)域重畫菜單 If e State = ( DrawItemState NoAccelerator Or DrawItemState Selected ) Then 根據(jù)菜單項(xiàng)的當(dāng)前繪制狀態(tài)來繪制菜單項(xiàng) e Graphics FillRectangle ( New SolidBrush ( Color LightYellow ) rfBound ) 對菜單項(xiàng)所在的矩形區(qū)域進(jìn)行色彩填充 e Graphics DrawString ( s New Font ( 宋體 FontStyle Bold ) New SolidBrush ( Color Black ) rfBound sfTemp ) 對菜單項(xiàng)名稱繪制 End If e DrawFocusRectangle ( ) 在 DrawItemEventArgs參數(shù)得到矩形范圍內(nèi)繪制聚焦框 e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color Black ) ) rfBound ) 對菜單項(xiàng)的矩形區(qū)域繪制矩形框End Sub
操作完成后 保存修改 此時再單擊快捷鍵F 運(yùn)行程序 可得到如圖 所示的界面
圖 【自己畫菜單】運(yùn)行界面之二
可見繪制的 文件 菜單項(xiàng)并沒有完全顯示出來 并且后面的菜單項(xiàng)也沒有顯示 這是因?yàn)椴藛雾?xiàng)的顯示區(qū)域并沒有設(shè)定 而缺省的空間又不能完全顯示造成的 設(shè)定菜單項(xiàng)的顯示區(qū)域大小是通過MeasureItem事件來完成的 具體操作是在MenuItem 的DrawItem事件后添加下列代碼 下列代碼是是定義MenuItem 的MeasureItem事件 在此事件中設(shè)定菜單項(xiàng)的寬度(當(dāng)然也可以設(shè)定高度等)
Private Sub MenuItem _MeasureItem ( ByVal sender As Object ByVal e As System Windows Forms MeasureItemEventArgs ) Handles MenuItem MeasureItem e ItemWidth = 設(shè)定菜單項(xiàng)的寬度End Sub
保存上述修改后 單擊快捷鍵F 運(yùn)行程序可得到圖 所示界面
圖 【自己畫菜單】運(yùn)行界面之三
可見 文件 菜單項(xiàng)就算繪制出來了 由于其他菜單項(xiàng)沒有繪制處理 所以也未顯示 其他菜單項(xiàng)的繪制方法和 文件 菜單項(xiàng)的繪制方法基本相似 以下是在上述完成的基礎(chǔ)上 對其他菜單項(xiàng)進(jìn)行繪制 從而得到圖 所示菜單的具體實(shí)現(xiàn)步驟
圖 【自己畫菜單】運(yùn)行界面之四
在Form vb中的MenuItem 的MeasureItem事件處理程序之后添加下列代碼 下列代碼是定義MenuItem 的DrawItem事件 其功能是對 新建 菜單項(xiàng)重新繪制
Private Sub MenuItem _DrawItem ( ByVal sender As Object ByVal e As System Windows Forms DrawItemEventArgs ) Handles MenuItem DrawItem Dim rfBound As RectangleF = New RectangleF ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲到RectangleF類型實(shí)例中 Dim rfBound As Rectangle = New Rectangle ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲到Rectangle類型實(shí)例中 Rectangle類型實(shí)例和RectangleF類型實(shí)例差不多 但在后面代碼中繪制菜單的函數(shù)是有區(qū)別的 e Graphics FillRectangle ( New SolidBrush ( Color LightGray ) rfBound ) Dim s As MenuItem = CType ( sender MenuItem ) Dim s As String = s Text 獲得菜單項(xiàng)對應(yīng)的文本名稱 Dim sfTemp As StringFormat = New StringFormat ( ) sfTemp Alignment = StringAlignment Center 設(shè)定文本在矩形區(qū)域的對齊方式 sfTemp LineAlignment = StringAlignment Center Dim rcText As RectangleF = rfBound rcText Width = e Graphics DrawString ( s New Font ( 宋體 ) New SolidBrush ( Color Blue ) rcText sfTemp ) e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color LightGray ) ) rfBound ) If e State = ( DrawItemState NoAccelerator Or DrawItemState Selected ) Thene Graphics FillRectangle ( New SolidBrush ( Color LightYellow ) rfBound ) e Graphics DrawString ( s New Font ( 宋體 FontStyle Bold Or FontStyle Underline ) New SolidBrush ( Color Red ) rcText sfTemp ) e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color Black ) ) rfBound ) e DrawFocusRectangle ( ) End IfEnd Sub
MenuItem 的DrawItem事件處理代碼之后添加下列代碼 下列代碼是定義MenuItem 的MeasureItem事件 在此事件中實(shí)現(xiàn)設(shè)定 新建 菜單項(xiàng)的長度和高度
Private Sub MenuItem _MeasureItem ( ByVal sender As Object ByVal e As System Windows Forms MeasureItemEventArgs ) Handles MenuItem MeasureItem e ItemWidth = 設(shè)定菜單項(xiàng)的寬度 e ItemHeight = 設(shè)定菜單項(xiàng)的高度End Sub
在完成上述操作步驟后 再在MenuItem 的MeasureItem事件處理程序之后添加下列代碼 下列代碼是定義MenuItem 的DrawItem事件 其功能是對 打開 菜單項(xiàng)重新繪制
Private Sub MenuItem _DrawItem ( ByVal sender As Object ByVal e As System Windows Forms DrawItemEventArgs ) Handles MenuItem DrawItemDim rfBound As RectangleF = New RectangleF ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲到RectangleF類型實(shí)例中Dim rfBound As Rectangle = New Rectangle ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲到Rectangle類型實(shí)例中 Rectangle類型實(shí)例和RectangleF類型實(shí)例差不多 但在后面代碼中繪制菜單的函數(shù)是有區(qū)別的Dim s As MenuItem = CType ( sender MenuItem ) Dim s As String = s TextDim sfTemp As StringFormat = New StringFormat ( ) sfTemp Alignment = StringAlignment CentersfTemp LineAlignment = StringAlignment CenterDim rcText As RectangleF = rfBoundrcText Width = e Graphics DrawString ( s New Font ( Veranda ) New SolidBrush ( Color Blue ) rcText sfTemp ) e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color LightGray ) ) rfBound ) If e State = ( DrawItemState NoAccelerator Or DrawItemState Selected ) Then e Graphics FillRectangle ( New SolidBrush ( Color LightYellow ) rfBound ) e Graphics DrawString ( s New Font ( Veranda FontStyle Bold Or FontStyle Underline ) New SolidBrush ( Color Red ) rcText sfTemp ) e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color Black ) ) rfBound ) e DrawFocusRectangle ( ) End IfEnd Sub
MenuItem 的DrawItem事件處理代碼之后添加下列代碼 下列代碼是定義MenuItem 的MeasureItem事件 在此事件中實(shí)現(xiàn)設(shè)定 新建 菜單項(xiàng)的長度和高度
Private Sub MenuItem _MeasureItem ( ByVal sender As Object ByVal e As System Windows Forms MeasureItemEventArgs ) Handles MenuItem MeasureItem e ItemWidth = 設(shè)定菜單項(xiàng)的寬度 e ItemHeight = 設(shè)定菜單項(xiàng)的高度End Sub
在上述步驟都正確完成后 本文介紹的手工繪制菜單就完成 此時單擊快捷鍵F 運(yùn)行 程序就可以得到圖 所示的運(yùn)行界面
六.總結(jié)
本文主要內(nèi)容是介紹VB NET設(shè)計(jì)和創(chuàng)建菜單 其中不僅介紹了使用菜單設(shè)計(jì)器來靜態(tài)設(shè)計(jì)菜單 還介紹了使用MainMenu類 MenuItem類和ContextMenu類動態(tài)創(chuàng)建菜單的實(shí)現(xiàn)方法 在動態(tài)創(chuàng)建時 首先要了解要創(chuàng)建的菜單類型 是下拉菜單 首先要創(chuàng)建一個MainMenu實(shí)例 是彈出菜單 首先要創(chuàng)建一個ContextMenu實(shí)例 然后根據(jù)菜單中的組成結(jié)構(gòu) 即菜單項(xiàng)中的父子關(guān)系 創(chuàng)建出相應(yīng)菜單 最后就是顯示出菜單 如果是下拉菜單 指派給Form的Menu屬性 如果是彈出菜單 指派給可視組件或Form的ContextMenu屬性 這樣動態(tài)創(chuàng)建菜單才能夠顯示出來 動態(tài)創(chuàng)建菜單的工作才算完成
此外還介紹了在Visual Basic Net中繪制個性化菜單的實(shí)現(xiàn)方法和注意事項(xiàng) 在繪制個性化菜單時最重要的是掌握DrawItem事件和MeasureItem事件用法 及繪制菜單時所要使用到的方法 雖然本文繪制的菜單并不美觀 但你可以通過本文介紹的方法來修改 從而實(shí)現(xiàn)更美觀 更有個性的菜單 最后請記住 在繪制菜單時 首先把菜單項(xiàng)的 OwnerDraw 屬性設(shè)定為 True
lishixinzhi/Article/program/net/201311/15454
文章題目:vb.net圖形菜單 vb的菜單控件
轉(zhuǎn)載來源:http://aaarwkj.com/article42/hhhsec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、App設(shè)計(jì)、網(wǎng)站改版、網(wǎng)站維護(hù)、網(wǎng)站制作、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)