五.繪制個(gè)性化菜單
臨夏州網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)從2013年開始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
先執(zhí)行以下操作步驟 下列步驟是通過菜單編輯器設(shè)計(jì)一個(gè)簡單的菜單 為后面重新繪制做基礎(chǔ)
啟動(dòng)Visual Studio Net
選擇菜單【文件】|【新建】|【項(xiàng)目】后 彈出【新建項(xiàng)目】對(duì)話框
將【項(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 窗體中拖入下列組件
一個(gè)MainMenu組件 名稱為 MainMenu
選中 MainMenu 組件 單擊鼠標(biāo)右鍵 在彈出的菜單中選擇 編輯菜單 并按照?qǐng)D 所示界面設(shè)計(jì)菜單
圖 【自己畫菜單】項(xiàng)目設(shè)計(jì)界面之一
此時(shí)保存上述步驟 并單擊快捷鍵F 則得到圖 所示界面
圖 【自己畫菜單】運(yùn)行界面之一
這樣通過菜單編輯器就完成了一個(gè)非常普通的菜單 下面就對(duì)此菜單進(jìn)行改造 在改造之前 要先設(shè)定項(xiàng)目中的三個(gè)MenuItem類實(shí)例的OwnerDraw屬性值為 True 因?yàn)橹挥写藢傩灾禐?True 才會(huì)觸發(fā)繪制菜單時(shí)所需要的DrawItem事件和MeasureItem事件 之后再在上面項(xiàng)目的基礎(chǔ)上執(zhí)行下一步操作
把Visual Stuido Net的當(dāng)前窗口切換到Form vb的代碼編輯窗口 并在InitializeComponent過程之后添加下列代碼 下列代碼是繪制 文件 菜單項(xiàng) 其作用是改變 文件 菜單項(xiàng)的字體 大小和菜單項(xiàng)的 其具體的繪制方法請(qǐ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ū)域并存儲(chǔ)到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ū)域并存儲(chǔ)到Rectangle類型實(shí)例中 Rectangle類型實(shí)例和RectangleF類型實(shí)例差不多 但在后面代碼中繪制菜單的函數(shù)是有區(qū)別的 e Graphics FillRectangle(New SolidBrush(Color LightGreen) rfBound) 以LightGreen色彩填充MenuItem 菜單項(xiàng)對(duì)應(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è)定要畫的菜單名稱的對(duì)齊方式 中間對(duì)齊 e Graphics DrawString ( s New Font ( 宋體 FontStyle Bold ) New SolidBrush ( Color Black ) rfBound sfTemp ) 以中間對(duì)齊方式 指定字體 大小 在指定的矩形區(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 ) 對(duì)菜單項(xiàng)所在的矩形區(qū)域進(jìn)行色彩填充 e Graphics DrawString ( s New Font ( 宋體 FontStyle Bold ) New SolidBrush ( Color Black ) rfBound sfTemp ) 對(duì)菜單項(xiàng)名稱繪制 End If e DrawFocusRectangle ( ) 在 DrawItemEventArgs參數(shù)得到矩形范圍內(nèi)繪制聚焦框 e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color Black ) ) rfBound ) 對(duì)菜單項(xiàng)的矩形區(qū)域繪制矩形框End Sub
操作完成后 保存修改 此時(shí)再單擊快捷鍵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ǔ)上 對(duì)其他菜單項(xiàng)進(jìn)行繪制 從而得到圖 所示菜單的具體實(shí)現(xiàn)步驟
圖 【自己畫菜單】運(yùn)行界面之四
在Form vb中的MenuItem 的MeasureItem事件處理程序之后添加下列代碼 下列代碼是定義MenuItem 的DrawItem事件 其功能是對(duì) 新建 菜單項(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ū)域并存儲(chǔ)到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ū)域并存儲(chǔ)到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)對(duì)應(yīng)的文本名稱 Dim sfTemp As StringFormat = New StringFormat ( ) sfTemp Alignment = StringAlignment Center 設(shè)定文本在矩形區(qū)域的對(duì)齊方式 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事件 其功能是對(duì) 打開 菜單項(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ū)域并存儲(chǔ)到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ū)域并存儲(chǔ)到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
在上述步驟都正確完成后 本文介紹的手工繪制菜單就完成 此時(shí)單擊快捷鍵F 運(yùn)行 程序就可以得到圖 所示的運(yùn)行界面
六.總結(jié)
本文主要內(nèi)容是介紹VB NET設(shè)計(jì)和創(chuàng)建菜單 其中不僅介紹了使用菜單設(shè)計(jì)器來靜態(tài)設(shè)計(jì)菜單 還介紹了使用MainMenu類 MenuItem類和ContextMenu類動(dòng)態(tài)創(chuàng)建菜單的實(shí)現(xiàn)方法 在動(dòng)態(tài)創(chuàng)建時(shí) 首先要了解要?jiǎng)?chuàng)建的菜單類型 是下拉菜單 首先要?jiǎng)?chuàng)建一個(gè)MainMenu實(shí)例 是彈出菜單 首先要?jiǎng)?chuàng)建一個(gè)ContextMenu實(shí)例 然后根據(jù)菜單中的組成結(jié)構(gòu) 即菜單項(xiàng)中的父子關(guān)系 創(chuàng)建出相應(yīng)菜單 最后就是顯示出菜單 如果是下拉菜單 指派給Form的Menu屬性 如果是彈出菜單 指派給可視組件或Form的ContextMenu屬性 這樣動(dòng)態(tài)創(chuàng)建菜單才能夠顯示出來 動(dòng)態(tài)創(chuàng)建菜單的工作才算完成
此外還介紹了在Visual Basic Net中繪制個(gè)性化菜單的實(shí)現(xiàn)方法和注意事項(xiàng) 在繪制個(gè)性化菜單時(shí)最重要的是掌握DrawItem事件和MeasureItem事件用法 及繪制菜單時(shí)所要使用到的方法 雖然本文繪制的菜單并不美觀 但你可以通過本文介紹的方法來修改 從而實(shí)現(xiàn)更美觀 更有個(gè)性的菜單 最后請(qǐng)記住 在繪制菜單時(shí) 首先把菜單項(xiàng)的 OwnerDraw 屬性設(shè)定為 True
lishixinzhi/Article/program/net/201311/15454
假設(shè)你的右鍵菜單為mymenu(contextmenustrip對(duì)象)
Dim oM As ToolStripMenuItem
For Each oM In Me.mymenu.Items
.....
Next
添加:(先在加一個(gè)contextMenu,再它的添加子菜單的click事件編程)
Try
’使TreeView可以被編輯
TreeView1.LabelEdit = True
‘判斷你是不是選定的是不可編輯的節(jié)點(diǎn),我這里工種節(jié)點(diǎn)不可以被編輯,只有工種下級(jí)的
各個(gè)工種名稱可以被編輯
If Trim(TreeView1.SelectedNode.Text) = "工種" Then
‘添加節(jié)點(diǎn)
AddNode = New TreeNode("請(qǐng)輸入新工種名字")
TreeView1.SelectedNode.Nodes.Add(AddNode)
TreeView1.ExpandAll()
AddNode.BeginEdit()
TreeView1.LabelEdit = True
NodeAdded = True
End If
Catch err As Exception
MsgBox(err.ToString)
End Try
刪除與添加類似,只是如果你的節(jié)點(diǎn)名字從其他處(如數(shù)據(jù)庫)得來,那么你還需要更新數(shù)據(jù)庫
編輯:
Private Sub TreeView1_BeforeLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.BeforeLabelEdit
TreeView1.LabelEdit = True ‘使可以編輯
AddNode = TreeView1.SelectedNode
End Sub
Private Sub TreeView1_AfterLabelEdit(ByVal sender As Object, ByVal e As System.windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.AfterLabelEdit
Try
‘此時(shí)你改完了節(jié)點(diǎn)名字
TreeView1.SelectedNode.EndEdit(True)
If e.Label Is Nothing Then
'do nothing
ElseIf e.Node.Text = "工種" Then ‘工種不能改
e.CancelEdit() = True
‘e.Node.Text ,e.Label.ToString 一個(gè)是改前的名字一個(gè)是該后的名字,具體哪個(gè)對(duì)
哪個(gè)請(qǐng)查MSDN
ElseIf Trim(e.Node.Text) "工種" And e.Node.Text e.Label.ToString Then
If MsgBox("此操作會(huì)導(dǎo)致當(dāng)前工種中的所有人員的工種都被更改,是否確定?", MsgBoxStyle.YesNo + MsgBoxStyle.Information, "警告") = MsgBoxResult.Yes Then
。。。。 ‘我的更改
MsgBox("更改成功!", MsgBoxStyle.OKOnly, "提示")
'Call InitTree() ‘有時(shí)要重新把treeview初始化一遍,視需求定
End If
End If
Catch err As Exception
MsgBox(err.ToString)
End Try
End Sub
其他:
擋treeview得到焦點(diǎn)時(shí)你可以使用ContextMenu,反之ContextMenu禁用
Private Sub TreeView1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.GotFocus
TreeView1.ContextMenu = ContextMenu1
End Sub
Private Sub TreeView1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.LostFocus
TreeView1.ContextMenu = Nothing
End Sub
注意:這里沒有在ContextMenu菜單添加“更改”項(xiàng),而是直接更改:即左鍵單擊節(jié)點(diǎn)表示
選中,再單擊一下就可以編輯了,更改之后單擊他處就完成更改,和你在windows中更改文
件名字相似。
1、Label控件用于標(biāo)注、提示其他控件的性質(zhì)、作用、內(nèi)容等不需要用戶編輯的文本,也可以用來簡單輸出少量的文本內(nèi)容。Windows 窗體 Label 控件還可以用來為其他控件定義訪問鍵。在標(biāo)簽 (Label) 控件中定義訪問鍵時(shí),用戶可以按 ALT 鍵和指定字符將焦點(diǎn)移動(dòng)到 Tab 鍵順序中的下一個(gè)控件上。因?yàn)闃?biāo)簽無法接收焦點(diǎn),所以焦點(diǎn)自動(dòng)移動(dòng)到 Tab 鍵順序中的下一個(gè)控件上。使用該技術(shù)向文本框、組合框、列表框和數(shù)據(jù)網(wǎng)格分配訪問鍵。
2、VB.NET中菜單控件有MenuStrip和ContextMenuStrip等2種。
MenuStrip主要用于常規(guī)的菜單系統(tǒng)設(shè)計(jì)。創(chuàng)建支持高級(jí)用戶界面和布局功能的易自定義的常用菜單,例如文本和圖像排序和對(duì)齊、拖放操作、MDI、溢出和訪問菜單命令的其他模式。
ContextMenuStrip主要用于創(chuàng)建上下文菜單或右鍵菜單。
要顯示上下文菜單,或在用戶右擊鼠標(biāo)時(shí)顯示一個(gè)菜單,就使用ContextMenuStrip類。與MenuStrip一樣,ContextMenuStrip也是ToolStripMenuItems對(duì)象的容器,但它派生于ToolStripDropDownMenu。ContextMenu的創(chuàng)建與MenuStrip相同,也是添加ToolStripMenuItems,定義每一項(xiàng)的Click事件,執(zhí)行某個(gè)任務(wù)。彈出菜單應(yīng)賦予特定的控件,為此,要設(shè)置控件的ContextMenuStrip屬性。在用戶右擊該控件時(shí),就顯示該菜單。
網(wǎng)站題目:vb.net中菜單集合 vbnet listview基本操作
鏈接地址:http://vcdvsql.cn/article40/dopgdho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、企業(yè)建站、響應(yīng)式網(wǎng)站、全網(wǎng)營銷推廣、動(dòng)態(tài)網(wǎng)站、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)