Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Displaying Bitmap Images in Menu Control

Author: DevASP
Download Source Code :

This article is about how to show bitmap images in the main menu in your application.

Start visual studio and create a new window application. 

Drop an instance of main menu in your form designer and design the menu which will contains the following menu.



            Name = “mnuFile”

            Text = “&File”

  • Now create sub menus of the File menu and set their properties as below:



            Name = “mnuNew”

            Text = “&New”



            Name = “mnuOpen”

Text = “&Open



            Name = “mnuSave”

            Text = “&Save”



            Name = “mnuExit”

            Text = “&Exit”

  • Now drop an image list on your form and set its following properties.


TransparentColor = “Aqua”

Imagesize = “20, 19”

Images = “Brows and add the images you want to assign to the menu”


  • Open your code window and declare the following variables and a font object.


Dim MaxMenuWidth As Integer

Dim aFont As New Font("Tahoma", 8)

  • Now write the following code in the MeasureItem event of mnuNew menu.


Dim mi As MenuItem = CType(sender, MenuItem)

Dim textSize As Size

Dim textFormat As New StringFormat()

textSize = e.Graphics.MeasureString(mi.Text, aFont).ToSize

MaxMenuWidth = Math.Max(MaxMenuWidth, textSize.Width + 20)

textSize = e.Graphics.MeasureString(mi.Text, aFont, New PointF(0, 0), textFormat).ToSize()

e.ItemHeight = Math.Max(textSize.Height + 2, SystemInformation.SmallIconSize.Height + 2)

e.ItemWidth = MaxMenuWidth

  • Now write the following code in the DrawItem event of mnuNew menu.


Dim mi As MenuItem = CType(sender, MenuItem)

Dim iImage As Integer

Dim textBounds As New RectangleF(e.Bounds.Left + 20, e.Bounds.Top + 2, e.Bounds.Right, e.Bounds.Bottom - 2)

Dim textFormat As New StringFormat()

Dim textColor As Color = SystemColors.MenuText

Dim selected As Boolean = False

textFormat.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show

selected = ((e.State And DrawItemState.Selected) = DrawItemState.Selected)

If selected = True Then


textColor = SystemColors.HighlightText


e.Graphics.FillRectangle(SystemBrushes.Menu, e.Bounds)

End If

Select Case mi.Text

Case "&New" : iImage = 0

Case "&Open" : iImage = 1

Case "&Save" : iImage = 2

Case Else

iImage = -1

End Select

If iImage <> -1 Then

e.Graphics.DrawImageUnscaled(ImageList.Images(iImage), e.Bounds.Left + 1, e.Bounds.Top + 1)

End If

e.Graphics.DrawString(mi.Text, aFont, New SolidBrush(textColor), textBounds, textFormat)


In the click event of mnuExit write the following code to end the application.



Add Article Comment:
Name :
Email Address :
Comments :
<< How to get the Visual Representation of Angle into Radian or Degree

Disclaimer - Privacy
© 2002-2017