大家过来探讨vbnet 的菜单自画
lyouj 2003-09-03 11:34:00 将OwnerDraw设成true后有什么注意事项,请高手作答。我碰到一个问题:
我写了如下函数:
Public Class MyMenuItem
Inherits MenuItem
Sub New()
MyBase.New()
Me.OwnerDraw = True
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
MyBase.OnDrawItem(e)
Dim MyCaption As String = Me.Text
Dim MyBrush As System.Drawing.Brush = System.Drawing.Brushes.Black
Dim MyFont As New Font(FontFamily.GenericSerif, 10, FontStyle.Regular, GraphicsUnit.Pixel)
Dim MySizeF As SizeF = e.Graphics.MeasureString(MyCaption, MyFont)
'Draw the item, and then draw a Rectangle around it.
MyBase.OnDrawItem(e)
e.DrawBackground()
Select Case e.State
Case DrawItemState.HotLight + DrawItemState.NoAccelerator
e.Graphics.DrawRectangle(Drawing.Pens.Black, New Rectangle(e.Bounds.X, e.Bounds.Y, MySizeF.Width, MySizeF.Height))
End Select
e.Graphics.DrawString(MyCaption, MyFont, MyBrush, e.Bounds.X, e.Bounds.Y)
End Sub
但运行后顶级菜单项重叠,下拉菜单不出来。不知除了重写 OnDrawItem,还要干什么。