vb.net菜单分组 vba分组框控件( 五 )


Private Sub MenuItem _MeasureItem ( ByVal sender As Object ByVal e As System Windows Forms MeasureItemEventArgs ) Handles MenuItem MeasureItem e ItemWidth =设定菜单项的宽度End Sub
保存上述修改后 单击快捷键F 运行程序可得到图 所示界面
图 【自己画菜单】运行界面之三
可见 文件 菜单项就算绘制出来了 由于其他菜单项没有绘制处理 所以也未显示 其他菜单项的绘制方法和 文件 菜单项的绘制方法基本相似 以下是在上述完成的基础上 对其他菜单项进行绘制 从而得到图 所示菜单的具体实现步骤
图 【自己画菜单】运行界面之四
在Form vb中的MenuItem 的MeasureItem事件处理程序之后添加下列代码 下列代码是定义MenuItem 的DrawItem事件 其功能是对 新建 菜单项重新绘制
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 )根据DrawItemEventArgs参数获得菜单项矩形区域并存储到RectangleF类型实例中 Dim rfBound As Rectangle = New Rectangle ( e Bounds X e Bounds Y e Bounds Width e Bounds Height )根据DrawItemEventArgs参数获得菜单项矩形区域并存储到Rectangle类型实例中Rectangle类型实例和RectangleF类型实例差不多 但在后面代码中绘制菜单的函数是有区别的 e Graphics FillRectangle ( New SolidBrush ( Color LightGray ) rfBound )Dim s As MenuItem = CType ( sender MenuItem )Dim s As String = s Text获得菜单项对应的文本名称 Dim sfTemp As StringFormat = New StringFormat ( )sfTemp Alignment = StringAlignment Center设定文本在矩形区域的对齐方式 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事件 在此事件中实现设定 新建 菜单项的长度和高度
Private Sub MenuItem _MeasureItem ( ByVal sender As Object ByVal e As System Windows Forms MeasureItemEventArgs ) Handles MenuItem MeasureItem e ItemWidth =设定菜单项的宽度 e ItemHeight =设定菜单项的高度End Sub
在完成上述操作步骤后 再在MenuItem 的MeasureItem事件处理程序之后添加下列代码 下列代码是定义MenuItem 的DrawItem事件 其功能是对 打开 菜单项重新绘制
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 )根据DrawItemEventArgs参数获得菜单项矩形区域并存储到RectangleF类型实例中Dim rfBound As Rectangle = New Rectangle ( e Bounds X e Bounds Y e Bounds Width e Bounds Height )根据DrawItemEventArgs参数获得菜单项矩形区域并存储到Rectangle类型实例中 Rectangle类型实例和RectangleF类型实例差不多 但在后面代码中绘制菜单的函数是有区别的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

推荐阅读