Hello everybody, I have a problem. I can create owner draw menu but only MainMenu. Contextmenu doesn't want to be created. Can anybody write sample of it
I believe you have to set the OwnerDraw property of each menu item to True. Then what I usually do is create one sub to handle all of the items for example:
Private Sub MeasureMenuItem(ByVal sender As Object, ByVal e As MeasureItemEventArgs) Handles menuItem1.MeasureItem, menuItem2.MeasureItem
Dim mnuItem As MenuItem = DirectCast(sender, MenuItem) Dim fnt As New Font(FontFamily, FontSize, FontStyle, GraphicsUnit) Dim sze As SizeF = e.Graphics.MeasureString(mnuItem.Text, fnt)
e.ItemHeight = sze.Height e.ItemWidth = sze.Width
End Sub
Then similar to draw menu:
Private Sub DrawMenuItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles menuItem1.DrawItem, menuItem2.DrawItem
Dim mnuItem As MenuItem = DirectCast(sender, MenuItem) Dim brush As SolidBrush Dim txtFont As New Font(FontFamily, FontSize, FontStyle, GraphicsUnit) Dim txtBrush As New SolidBrush(Color.FromKnownColor(KnownColor.MenuText))
If e.State And DrawItemState.Selected Then
brush = New SolidBrush(Color.FromKnownColor(KnownColor.Highlight)) e.Graphics.FillRectangle(brush, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
Else
brush = New SolidBrush(Color.FromKnownColor(KnownColor.Menu)) e.Graphics.FillRectangle(brush, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
Owner Draw ContextMenu
Brett M
jkc
It's really works.
Otter Sr
I believe you have to set the OwnerDraw property of each menu item to True. Then what I usually do is create one sub to handle all of the items for example:
Private Sub MeasureMenuItem(ByVal sender As Object, ByVal e As MeasureItemEventArgs) Handles menuItem1.MeasureItem, menuItem2.MeasureItem
Dim mnuItem As MenuItem = DirectCast(sender, MenuItem)
Dim fnt As New Font(FontFamily, FontSize, FontStyle, GraphicsUnit)
Dim sze As SizeF = e.Graphics.MeasureString(mnuItem.Text, fnt)
e.ItemHeight = sze.Height
e.ItemWidth = sze.Width
End Sub
Then similar to draw menu:
Private Sub DrawMenuItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles menuItem1.DrawItem, menuItem2.DrawItem
Dim mnuItem As MenuItem = DirectCast(sender, MenuItem)
Dim brush As SolidBrush
Dim txtFont As New Font(FontFamily, FontSize, FontStyle, GraphicsUnit)
Dim txtBrush As New SolidBrush(Color.FromKnownColor(KnownColor.MenuText))
If e.State And DrawItemState.Selected Then
brush = New SolidBrush(Color.FromKnownColor(KnownColor.Highlight))
e.Graphics.FillRectangle(brush, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
Else
brush = New SolidBrush(Color.FromKnownColor(KnownColor.Menu))
e.Graphics.FillRectangle(brush, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
End If
e.Graphics.DrawString(mnuItem.Text, txtFont, txtBrush, e.Bounds.X , e.Bounds.Y)
brush.Dispose()
txtFont.Dispose()
txtBrush.Dispose()
End Sub
Hope that is what you are looking for.
-Joe