Click here to Skip to main content
Click here to Skip to main content

Adding Images to Menu

By , 14 Nov 2002
 

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

About the Author

SeeSharp
Web Developer
United States United States
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalit have errormemberwangqiang01514 Apr '07 - 23:16 
Generalcontext menusmembergorcq28 Feb '07 - 19:42 
GeneralRe: context menusmembergorcq1 Mar '07 - 19:00 
Generala simplified methodmemberJustin Alexander13 Sep '04 - 5:45 
GeneralTransparencemembermatrix6666 Aug '04 - 0:18 
GeneralRe: Transparencememberericsmith1 Apr '05 - 12:27 
GeneralDr. Gui's answermembersds11 Dec '03 - 6:46 
GeneralImage sizememberWatson3921 Jan '03 - 8:23 
GeneralRe: Image sizememberMarvin Tsai9 Aug '07 - 22:56 
GeneralUse invisible toolbarsussAnonymous18 Nov '02 - 21:09 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 15 Nov 2002
Article Copyright 2002 by SeeSharp
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid