Click here to Skip to main content
Licence 
First Posted 14 Nov 2002
Views 133,168
Bookmarked 46 times

Adding Images to Menu

By | 14 Nov 2002 | Article
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

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalit have error Pinmemberwangqiang015123:16 4 Apr '07  
I download and run it.
myBitmap = new Bitmap(Environment.CurrentDirectory + "\\Exit1.bmp")
System.SystemException {"使用了无效参数。"} System.SystemException

Generalcontext menus Pinmembergorcq19:42 28 Feb '07  
GeneralRe: context menus Pinmembergorcq19:00 1 Mar '07  
Generala simplified method PinmemberJustin Alexander5:45 13 Sep '04  
GeneralTransparence Pinmembermatrix6660:18 6 Aug '04  
GeneralRe: Transparence Pinmemberericsmith12:27 1 Apr '05  
GeneralDr. Gui's answer Pinmembersds6:46 11 Dec '03  
GeneralImage size PinmemberWatson398:23 21 Jan '03  
GeneralRe: Image size PinmemberMarvin Tsai22:56 9 Aug '07  
GeneralUse invisible toolbar PinsussAnonymous21:09 18 Nov '02  
GeneralRe: Use invisible toolbar PinmemberSue McNaughtan0:34 19 Nov '02  
Questioncolor bitmaps? PinmemberMarc Clifton15:50 15 Nov '02  
Generalgreat job Pinmemberashley_vg14:35 15 Nov '02  
GeneralRe: great job PinmemberSeeSharp21:12 18 Nov '02  
GeneralNative C#, PinmemberJerry Maguire2:33 15 Nov '02  
GeneralRe: Native C#, Pinmembermammiebubba20:33 15 Nov '02  
GeneralRe: Native C#, PinmemberJerry Maguire4:44 16 Nov '02  
GeneralRe: Native C#, PinmemberJamie Nordmeyer5:03 18 Nov '02  

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

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

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