扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:阿虎 来源:天极开发者网络 2007年11月9日
关键字:
图07:【自己画菜单】项目设计界面之一 |
图08:【自己画菜单】运行界面之一 |
Private Sub MenuItem1_DrawItem ( ByVal sender As Object , ByVal e As System.Windows.Forms.DrawItemEventArgs ) Handles MenuItem1.DrawItem Dim rfBound As RectangleF = New RectangleF ( e.Bounds.X , e.Bounds.Y , e.Bounds.Width - 1 , e.Bounds.Height ) '根据DrawItemEventArgs参数获得菜单项矩形区域并存储到RectangleF类型实例中 Dim rfBound1 As Rectangle = New Rectangle ( e.Bounds.X , e.Bounds.Y , e.Bounds.Width - 1 , e.Bounds.Height ) '根据DrawItemEventArgs参数获得菜单项矩形区域并存储到Rectangle类型实例中 'Rectangle类型实例和RectangleF类型实例差不多,但在后面代码中绘制菜单的函数是有区别的 e.Graphics.FillRectangle(New SolidBrush(Color.LightGreen), rfBound) '以LightGreen色彩填充MenuItem1菜单项对应的矩形区域 Dim s As MenuItem = CType ( sender , MenuItem ) Dim s1 As String = s.Text '获得MenuItem1菜单项的名称 Dim sfTemp As StringFormat = New StringFormat ( ) sfTemp.Alignment = StringAlignment.Center '设定要画的菜单名称的对齐方式,中间对齐 e.Graphics.DrawString ( s1 , New Font ( "宋体" , 10 , FontStyle.Bold ) , New SolidBrush ( Color.Black ) , rfBound , sfTemp ) '以中间对齐方式、指定字体、大小,在指定的矩形区域重画菜单 If e.State = ( DrawItemState.NoAccelerator Or DrawItemState.Selected ) Then '根据菜单项的当前绘制状态来绘制菜单项 e.Graphics.FillRectangle ( New SolidBrush ( Color.LightYellow ) , rfBound ) '对菜单项所在的矩形区域进行色彩填充 e.Graphics.DrawString ( s1 , New Font ( "宋体" , 10 , FontStyle.Bold ) , New SolidBrush ( Color.Black ) , rfBound , sfTemp ) '对菜单项名称绘制 End If e.DrawFocusRectangle ( ) '在 DrawItemEventArgs参数得到矩形范围内绘制聚焦框。 e.Graphics.DrawRectangle ( New Pen ( New SolidBrush ( Color.Black ) , 1 ) , rfBound1 ) '对菜单项的矩形区域绘制矩形框 End Sub |
图09:【自己画菜单】运行界面之二 |
Private Sub MenuItem1_MeasureItem ( ByVal sender As Object , ByVal e As System.Windows.Forms.MeasureItemEventArgs ) Handles MenuItem1.MeasureItem e.ItemWidth = 60 '设定菜单项的宽度 End Sub |
图10:【自己画菜单】运行界面之三 |
图11:【自己画菜单】运行界面之四 |
Private Sub MenuItem2_DrawItem ( ByVal sender As Object , ByVal e As System.Windows.Forms.DrawItemEventArgs ) Handles MenuItem2.DrawItem Dim rfBound As RectangleF = New RectangleF ( e.Bounds.X , e.Bounds.Y , e.Bounds.Width - 1 , e.Bounds.Height ) '根据DrawItemEventArgs参数获得菜单项矩形区域并存储到RectangleF类型实例中 Dim rfBound1 As Rectangle = New Rectangle ( e.Bounds.X , e.Bounds.Y , e.Bounds.Width - 1 , e.Bounds.Height ) '根据DrawItemEventArgs参数获得菜单项矩形区域并存储到Rectangle类型实例中 'Rectangle类型实例和RectangleF类型实例差不多,但在后面代码中绘制菜单的函数是有区别的 e.Graphics.FillRectangle ( New SolidBrush ( Color.LightGray ) , rfBound ) Dim s As MenuItem = CType ( sender , MenuItem ) Dim s1 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 -= 5 e.Graphics.DrawString ( s1 , New Font ( "宋体" , 10 ) , New SolidBrush ( Color.Blue ) , rcText , sfTemp ) e.Graphics.DrawRectangle ( New Pen ( New SolidBrush ( Color.LightGray ) ) , rfBound1 ) If e.State = ( DrawItemState.NoAccelerator Or DrawItemState.Selected ) Then e.Graphics.FillRectangle ( New SolidBrush ( Color.LightYellow ) , rfBound ) e.Graphics.DrawString ( s1 , New Font ( "宋体" , 10 , FontStyle.Bold Or FontStyle.Underline ) , New SolidBrush ( Color.Red ) , rcText , sfTemp ) e.Graphics.DrawRectangle ( New Pen ( New SolidBrush ( Color.Black ) ) , rfBound1 ) e.DrawFocusRectangle ( ) End If End Sub |
Private Sub MenuItem2_MeasureItem ( ByVal sender As Object , ByVal e As System.Windows.Forms.MeasureItemEventArgs ) Handles MenuItem2.MeasureItem e.ItemWidth = 60 '设定菜单项的宽度 e.ItemHeight = 30 '设定菜单项的高度 End Sub |
Private Sub MenuItem3_DrawItem ( ByVal sender As Object , ByVal e As System.Windows.Forms.DrawItemEventArgs ) Handles MenuItem3.DrawItem Dim rfBound As RectangleF = New RectangleF ( e.Bounds.X , e.Bounds.Y , e.Bounds.Width - 1 , e.Bounds.Height ) '根据DrawItemEventArgs参数获得菜单项矩形区域并存储到RectangleF类型实例中 Dim rfBound1 As Rectangle = New Rectangle ( e.Bounds.X , e.Bounds.Y , e.Bounds.Width - 1 , e.Bounds.Height ) '根据DrawItemEventArgs参数获得菜单项矩形区域并存储到Rectangle类型实例中 'Rectangle类型实例和RectangleF类型实例差不多,但在后面代码中绘制菜单的函数是有区别的 Dim s As MenuItem = CType ( sender , MenuItem ) Dim s1 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 -= 5 e.Graphics.DrawString ( s1 , New Font ( "Veranda" , 10 ) , New SolidBrush ( Color.Blue ) , rcText , sfTemp ) e.Graphics.DrawRectangle ( New Pen ( New SolidBrush ( Color.LightGray ) ) , rfBound1 ) If e.State = ( DrawItemState.NoAccelerator Or DrawItemState.Selected ) Then e.Graphics.FillRectangle ( New SolidBrush ( Color.LightYellow ) , rfBound ) e.Graphics.DrawString ( s1 , New Font ( "Veranda" , 10 , FontStyle.Bold Or FontStyle.Underline ) , New SolidBrush ( Color.Red ) , rcText , sfTemp ) e.Graphics.DrawRectangle ( New Pen ( New SolidBrush ( Color.Black ) ) , rfBound1 ) e.DrawFocusRectangle ( ) End If End Sub |
Private Sub MenuItem3_MeasureItem ( ByVal sender As Object , ByVal e As System.Windows.Forms.MeasureItemEventArgs ) Handles MenuItem3.MeasureItem e.ItemWidth = 60 '设定菜单项的宽度 e.ItemHeight = 30 '设定菜单项的高度 End Sub |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者