A Windows application, which focuses mainly on its GUI
aspects, can make use of the ownerdraw properties of the various forms
components. Once such component that allows us to define our own drawing and
painting of items is the Menu component of Windows Forms.
This article will explain to you how we can draw our own menu items with our own
fonts, pictures, background color and other graphics objects.
Step 1: Create a simple windows form application.
Click File - > New -> Project - > Create new application OD_Menu.
In the designer view for the default form created, add a new "Main Menu"
component from the Windows Forms Toolbox.
Using the designer, create menu and menu items.

We need to ownerdraw not only the top-level menu items, but also all
the submenu items.
For every menu item select the "Properties" and set the OwnerDraw option to
"True".
For every owerdraw menu item the application will call 2 functions..
DrawItem: This function will do the actual painting and drawing of the intended
menu item.
Measure Item: This function is called to set the height / width of the menu
item.
We need to add these 2 events from the "Events" tab of the menu properties.
Select the properties for the "File" menu item.
Click on the Events tab.
Double Click the Draw Item option. It will add a default handler to handle the
drawing of the "File" menu item.
private sub
menuItem1_DrawItem(sender as
object ,e
as System.Windows.Forms.DrawItemEventArgs )
Double Click the MeasureItem otpion. It will add a default handler
private sub
menuItem1_MeasureItem(sender as
object ,e
as System.Windows.Forms.MeasureItemEventArgs )
end sub
Now we need to add the sametype of DrawItem and MeasureItem for the sub menu
items. We are creating a different event handler for the subitems , since we
need to do some more fancy stuff with the sub items like drawing icons or
bitmaps.
Select the first subitem "Open" and add the same 2 events for this item.
private sub
menuItem1_MeasureItem(sender as
object ,e
as System.Windows.Forms.MeasureItemEventArgs )
private sub
menuItem2_DrawItem(sender as
object ,e
as System.Windows.Forms.DrawItemEventArgs )
Important: Since we need to have a DrawItem and MeasureItem for
each and every item, it does not make sense to duplicate the events for every
menu item. We can have the mainmenu items (File / Options / Help) be handle via
a common event handler and we can have the submenu items like (Open /Close ...
About) be routed through another common event handler.
Here is how to do this.
For every other main menu item like "Options and Help", select the item in
designer.
- Click proerties. Click the Events tab.
- Click the listbox next to the DrawItem and select the menuItem1_DrawItem option from the list.
- Click the listbox next to the MeasureItem and select the menuItem1_MeasureItem option from the list.
For every other sub menu items like "Close / Exit / Security / Network / About".
- Click proerties. Click the Events tab.
- Click the listbox next to the DrawItem and select the menuItem2_DrawItem option from the list.
- Click the listbox next to the MeasureItem and select the menuItem2_MeasureItem option from the list.

Join the conversation! Your thoughts help the community grow.