PDA

Click to See Complete Forum and Search --> : Get Icon from ImageList


Edneeis
Jul 1st, 2002, 01:16 PM
Nevermind there was no need I just used DrawImage instead, here is the somewhat final code:

Private Function AppendShortcut(ByVal mItem As MenuItem) As String
Dim s As String
s = mItem.Text
' Check to see if you have a shortcut If so, append it to your existing text
If mItem.ShowShortcut And mItem.Shortcut <> Shortcut.None Then
Dim k As Keys = CType(mItem.Shortcut, Keys)
s = s & Convert.ToChar(9) & _
System.ComponentModel.TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)
End If
Return s
End Function

Private Sub DrawIMenu(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 = 25 'Offset text past icon
Dim Y As Integer = 1 '9 'Center text in menuitem
Dim brhText As New SolidBrush(SystemColors.MenuText)
Dim brhBack As New SolidBrush(SystemColors.Menu)
Dim sf As StringFormat
sf = New StringFormat()
sf.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show

'test for selected
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
brhText.Color = SystemColors.HighlightText
brhBack.Color = SystemColors.Highlight
End If

G.FillRectangle(brhBack, e.Bounds)
G.DrawRectangle(System.Drawing.Pens.Transparent, e.Bounds)
G.DrawImage(icoList.Images(1), 3, 1)
G.DrawString(AppendShortcut(sender), SystemInformation.MenuFont, brhText, e.Bounds.X + X, e.Bounds.Y + Y, sf)
brhText.Dispose()
brhBack.Dispose()
sf.Dispose()
End Sub