PDA

Click to See Complete Forum and Search --> : Icons in the menus


damicatz
Jun 3rd, 2002, 01:41 PM
I recently got Visual Basic.net and so far I'm liking it.

But how do I get icons beside the menu items on the forms. I read somewhere that it has something to do with the ownerdraw property but I have no idea how to do this.

wolfofthenorth
Jun 3rd, 2002, 07:10 PM
This works with ownerdraw set to true. You'll have to change the colors to what you want, but this should be a good start. :cool:

private MyMenuText as string = "My Menu"

Private Sub MenuItem2_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MenuItem2.DrawItem

e.DrawBackground()
Dim G As Graphics = e.Graphics
Dim X as integer = 35 'Offset text past icon
Dim Y as integer = 9 'Center text in menuitem

G.FillRectangle(New SolidBrush(Color.White), e.Bounds)
G.DrawRectangle(System.Drawing.Pens.Green, e.Bounds)

G.DrawIcon(New Icon("App.ico"), 0, 0)
G.DrawString(MyMenuText, Me.Font, New SolidBrush(Color.Red), e.Bounds.X + X, e.Bounds.Y + Y)
End Sub


Private Sub MenuItem2_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles MenuItem2.MeasureItem

Dim S As SizeF = e.Graphics.MeasureString(MyMenuText, Me.Font)
Dim X as integer = 45 'Extra width for menu.
Dim Y as integer = 20 'Extra height for menu.

e.ItemHeight = CType(S.Height + Y, Integer)
e.ItemWidth = CType(S.Width + X, Integer)

End Sub