C #. Net how to add images to the menu

Source: Internet
Author: User
Tags transparent color

Method 1:

You need to use the API
First: using system. runtime. interopservices;
Then:
[Dllimport ("USER32", entrypoint = "setmenuitembitmaps")]
Private Static extern int setmenuitembitmaps (intptr hmenu, int nposition, int wflags, intptr hbitmapunchecked, intptr hbitmapchecked );
Finally, when form_load:
Bitmap bitmap = new Bitmap ("C: \ icon. ICO ");
Int mf_byposition = 0x0400;
Setmenuitembitmaps (menufile. Handle, 0, mf_byposition, bitmap. gethbitmap (system. Drawing. color. White), bitmap. gethbitmap (system. Drawing. color. White ));

Note: This icon is white. If it is red, use system. Drawing. color. Red;
And so on.

Method 2:

You can also use. NET as follows:
/// <Summary>
/// Draw the XP style menu item
/// </Summary>
/// <Param name = "E"> draw menu events </param>
/// <Param name = "unselectbackcolor"> unselected background color </param>
/// <Param name = "selectbackcolor"> selected background color </param>
/// <Param name = "selectbackcolorend"> specifies the background transition color. If not, use a transparent color. </param>
/// <Param name = "unselectforecolor"> the foreground color is not selected. </param>
/// <Param name = "selectforecolor"> select the current foreground color </param>
/// <Param name = "menurect"> menu moment box size </param>
/// <Param name = "imgmenu"> picture on the left of the entire menu </param>
/// <Param name = "iconmenu"> menu item icon </param>
/// <Param name = "icontrancolor"> transparent color of the menu item icon </param>
/// <Param name = "smenuitemword"> menu item text </param>
Private void drawmenuitem (effece, color unselectbackcolor, color selectbackcolor, color selectbackcolorend, color unselectforecolor, color selectforecolor, image imgmenu, image iconmenu, color icontrancolor, string smenuitemword)
{
Bitmap iconbitmapmenu = new Bitmap (iconmenu );
Iconbitmapmenu. maketransparent (icontrancolor );
Rectangle rectmenu = E. bounds;

Brush selectbackbrush = new system. Drawing. solidbrush (selectbackcolor );
Brush unselectbackbrush = new system. Drawing. solidbrush (unselectbackcolor );
Brush selectforebrush = new system. Drawing. solidbrush (selectforecolor );
Brush unselectforebrush = new system. Drawing. solidbrush (unselectforecolor );

If (E. State = drawitemstate. noaccelerator)
{
E. Graphics. fillrectangle (unselectbackbrush, rectmenu );
E. Graphics. drawstring (smenuitemword, this. Font, unselectforebrush, rectmenu. x + 40, rectmenu. Y + 4 );
E. Graphics. drawimageunscaled (iconbitmapmenu, rectmenu. x + 22, rectmenu. Y + 2, iconmenu. Width, iconmenu. Height );
}
Else
{
If (selectbackcolorend! = Color. Transparent)
Selectbackbrush = new system. Drawing. drawing2d. lineargradientbrush (rectmenu, selectbackcolor, selectbackcolorend, 0f );

E. Graphics. fillrectangle (selectbackbrush, rectmenu );
E. Graphics. drawstring (smenuitemword, this. Font, selectforebrush, rectmenu. x + 40, rectmenu. Y + 4 );
E. Graphics. fillrectangle (system. Drawing. Brushes. whitesmoke, rectmenu. x + 20, rectmenu. Y, 20, 20); // icon background
E. Graphics. drawimageunscaled (iconbitmapmenu, rectmenu. x + 2 + 20, rectmenu. Y + 2, imgmenu. Width, imgmenu. Height); // icon
}
E. Graphics. drawimageunscaled (imgmenu, 0, 0 );
}

// However, you must set the ownerdraw of the menu to true before calling. The following is the call method:
Private void menuexit_drawitem (Object sender, drawitemeventargs E)
{
Drawmenuitem (E, color. whitesmoke, color. deepskyblue, color. white, color. black, color. black, picmenu. image, imagelist. images [2], color. white, "quit ");
}
Private void menuexit_measureitem (Object sender, measureitemeventargs E)
{
E. itemwidth = 150;
E. itemheight = 20;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.