Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / XML

Adding Images to Menu

Rate me:
Please Sign up or sign in to vote.
4.17/5 (6 votes)
14 Nov 2002 230.4K   905   49   18
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.

C#
[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

C#
intptrMenu = GetMenu(this.Handle);

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

C#
intptrSubMenu = GetSubMenu(intptrMenu, 0);

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

C#
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 .

C#
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.


Written By
Web Developer
United States United States
Deva Anbu Daniel is an Application Developer with 2+ years experience in Java,VB and C#. Currently working in Insoft.com(www.insoft.com). His favorite language is C. He's a major fan of Iron Maiden and Metallica and Jethro Tull.

Comments and Discussions

 
Generalit have error Pin
wangqiang01514-Apr-07 23:16
wangqiang01514-Apr-07 23:16 
Generalcontext menus Pin
gorcq28-Feb-07 19:42
gorcq28-Feb-07 19:42 
GeneralRe: context menus Pin
gorcq1-Mar-07 19:00
gorcq1-Mar-07 19:00 
Generala simplified method Pin
Member 116381813-Sep-04 5:45
Member 116381813-Sep-04 5:45 
GeneralTransparence Pin
matrix6666-Aug-04 0:18
matrix6666-Aug-04 0:18 
GeneralRe: Transparence Pin
ericsmith1-Apr-05 12:27
ericsmith1-Apr-05 12:27 
GeneralDr. Gui's answer Pin
sds11-Dec-03 6:46
sds11-Dec-03 6:46 
GeneralImage size Pin
Johannes Wallroth21-Jan-03 8:23
Johannes Wallroth21-Jan-03 8:23 
GeneralRe: Image size Pin
Marvin Tsai9-Aug-07 22:56
Marvin Tsai9-Aug-07 22:56 
GeneralUse invisible toolbar Pin
Anonymous18-Nov-02 21:09
Anonymous18-Nov-02 21:09 
GeneralRe: Use invisible toolbar Pin
Sue McNaughtan19-Nov-02 0:34
Sue McNaughtan19-Nov-02 0:34 
Questioncolor bitmaps? Pin
Marc Clifton15-Nov-02 15:50
mvaMarc Clifton15-Nov-02 15:50 
Generalgreat job Pin
Ashley van Gerven15-Nov-02 14:35
Ashley van Gerven15-Nov-02 14:35 
GeneralRe: great job Pin
SeeSharp18-Nov-02 21:12
SeeSharp18-Nov-02 21:12 
GeneralNative C#, Pin
Chris Richner15-Nov-02 2:33
Chris Richner15-Nov-02 2:33 
GeneralRe: Native C#, Pin
mammiebubba15-Nov-02 20:33
mammiebubba15-Nov-02 20:33 
GeneralRe: Native C#, Pin
Chris Richner16-Nov-02 4:44
Chris Richner16-Nov-02 4:44 
GeneralRe: Native C#, Pin
Jamie Nordmeyer18-Nov-02 5:03
Jamie Nordmeyer18-Nov-02 5:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.