Menu Item Bitmap

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

I did not find such thing in .NET, while there was one in Win32 API.
(See MENUITEMINFO, SetMenuItemBitmaps, etc,... help topics in Platform SDK
documentation)

Am I missing something?
 
Here is the implementation of Menu Item Bitmap using SetMenuItemInfo API
function.
I have MyNamespace.MenuItem class derived from Windows.Forms.MenuItem.

It implements two additional properties. The public Bitmap protperty is to
set and retrieve MenuItem's bitmap (get not tested).

The code is unsafe.

Menu containing the menu item should be created BEFORE adding the bitmap.

I had a problem displaying transparent images. The transparent pixels are
show as something blue.

Identifiers starting with WinUser WinGDI and WinBase refer api stuff mapped
from the Platform SDK *.h files with same names. I will post the mappings if
anybody will be interested.


public class MenuItem : System.Windows.Forms.MenuItem
{
/// <summary>
/// Default constructor.
/// Initializes menu item instance.
/// Calls base default constructor.
/// </summary>
public MenuItem()
:base()
{
//
// TODO: Add constructor logic here
//
}

unsafe IntPtr HBitmap
{
get
{
WinUser.tagMENUITEMINFO mii;
mii.cbSize = (uint)sizeof(WinUser.tagMENUITEMINFO);
mii.fMask = WinUser.MIIM.BITMAP;
mii.wID = (uint)this.MenuID;
mii.hbmpItem = (IntPtr)null;
int iResult =
WinUser.GetMenuItemInfoW(this.Parent.Handle,(uint)this.Index,1,(IntPtr)(&mii
..cbSize));
return mii.hbmpItem;
}
set
{
WinUser.tagMENUITEMINFO mii;
mii.cbSize = (uint)sizeof(WinUser.tagMENUITEMINFO);
mii.hbmpItem = (IntPtr)(null);
mii.fMask = WinUser.MIIM.BITMAP;
int iResult =
WinUser.GetMenuItemInfoW(this.Parent.Handle,(uint)this.Index,1,(IntPtr)(&mii
..cbSize));
if (((IntPtr)null)!=mii.hbmpItem)
{//Delete Old Bitmap, if any
WinGDI.DeleteObject(mii.hbmpItem);
}
mii.hbmpItem = value;
iResult =
WinUser.SetMenuItemInfoW(this.Parent.Handle,(uint)this.Index,1,(IntPtr)(&mii
..cbSize));
uint uiLastError = WinBase.GetLastError();
}
}
}
 
Back
Top