Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Adding Images to Menu

0.00/5 (No votes)
14 Nov 2002 4  
Adding images to MenuItems

Introduction

This is a simple program which loads a picture to menu controls. MenuItem controls don't have an Image property to load an image. So the only way to do this is by calling Windows API functions. The following are the API functions used. All functions are from User32.dll.

[DllImport("user32.dll")]
    public static extern IntPtr GetMenu(IntPtr hwnd);
[DllImport("user32.dll")]
    public static extern IntPtr GetSubMenu(IntPtr hMenu,int nPos);
[DllImport("user32.dll")]
    public static extern IntPtr GetMenuItemID(IntPtr hMenu, int nPos);
[DllImport("user32.dll")]
    public static extern int SetMenuItemBitmaps(IntPtr hMenu, IntPtr nPosition, 
        int wFlags, IntPtr hBitmapUnchecked, IntPtr hBitmapChecked);

The Code

intptrMenu = GetMenu(this.Handle);

First, get the pointer to the current form's menu.

intptrSubMenu = GetSubMenu(intptrMenu, 0);

Then, get the pointer to the first menu's submenu list.

intptrMenuItemID = GetMenuItemID(intptrSubMenu, 0);

In the above code, 0 is the first MenuItem in the submenu list. Thus get the pointer to the first MenuItem .

intRet = SetMenuItemBitmaps(intptrMenu, intptrMenuItemID, 0 ,intp, intp);

intp is the handle to a bitmap. SetMenuItemBitmaps sets the image from the handle to the MenuItem. So when you run the code, no images are loaded.

Then when you hit the LoadImage button, the images get loaded to the menu items.

Have fun!

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here