Make your own vs.net-style right-click menu (Simple, practical)
The relevant pictures of this topic are as follows: Class MyMenuItem:System.Windows.Forms.MenuItem
{
Public Mymenuitem ()
{
It's important to set the Owerdraw to true so that you can draw the menu yourself, otherwise you'll have the operating system draw the menu, and the default is False.
This. Ownerdraw=true;
}
protected override void OnDrawItem (Sysdrawitemeventargs e)
{
To repaint the menu, there is no OnPaint method overload, only the overloaded OnDrawItem Method!
Graphics G=e.graphics;
g.smoothingmode=smoothingmode.antialias;//anti-aliasing
Font f = new Font (FONTFAMILY.GENERICSERIF, fontstyle.regular, graphicsunit.pixel);//Set menu fonts
Pen p=new Pen (color.navy,1);//This is the font for border painting
if (e.state==drawitemstate.noaccelerator)//First right click appears menu, but Mouse does not move up
{//with a white background
G.fillrectangle (brushes.whitesmoke,e.bounds.x-2,e.bounds.y-2,121,23);
}
Mouse moved up, but did not click
if ((E.state & drawitemstate.selected) ==drawitemstate.selected)
{
Lace frame and background
G.fillrectangle (brushes.lightsteelblue,e.bounds.x,e.bounds.y,109,20);
G.drawline (p,e.bounds.x,e.bounds.y,e.bounds.x,e.bounds.y+19);
G.drawline (p,e.bounds.x,e.bounds.y+19,e.bounds.x+109,e.bounds.y+19);
G.drawline (P,E.BOUNDS.X+109,E.BOUNDS.Y+19,E.BOUNDS.X+109,E.BOUNDS.Y);
G.drawline (P,E.BOUNDS.X+109,E.BOUNDS.Y,E.BOUNDS.X,E.BOUNDS.Y);
}
Display text
g.DrawString (this. TEXT,F,BRUSHES.BLACK,E.BOUNDS.X,E.BOUNDS.Y);
G.dispose ();
}
This is important, this gives you a menu that defines the size, height 20, Width 100, or your menu doesn't see anything.
protected override void OnMeasureItem (MeasureItemEventArgs e)
{
e.itemheight=20;
e.itemwidth=100;
}
}
Description: Here I do not draw the button when the look (lazy:), mainly to further improve, of course, there is no icon, but also in order to improve later, this is just a preliminary form, we see what a higher method?!
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.