Right-click the Vs. Net style menu by yourself

Source: Internet
Author: User
Right-click the Vs. Net style menu by yourself (simple and practical)

This topic is related to the following image: Class mymenuitem: system. Windows. Forms. menuitem

{
Public mymenuitem ()
{
// It is important to set owerdraw to true so that you can draw menus by yourself. Otherwise, the operating system will draw menus. The default value is false.
This. ownerdraw = true;
}
Protected override void ondrawitem (sysdrawitemeventargs E)
{
// To re-draw a menu, there is no onpaint method overload, and only the ondrawitem method is reloaded!
Graphics G = E. graphics;
G. smoothingmode = smoothingmode. antialias; // anti-aliasing
Font F = new font (fontfamily. genericserif, 12, fontstyle. Regular, graphicsunit. pixel); // set the font of the menu
Pen P = new pen (color. Navy, 1); // This is the font of the border.
If (E. State = drawitemstate. noaccelerator) // right-click at the beginning, and the menu appears, but the mouse does not move up.
{// Use a white background color
G. fillrectangle (brushes. whitesmoke, E. bounds. X-2, E. bounds. Y-2, 121,23 );
}
// Move the mouse up without clicking
If (E. State & drawitemstate. Selected) = drawitemstate. Selected)
{
// Border and background color
G. fillrectangle (brushes. lightsteelblue, E. bounds. X, E. bounds. Y, 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 very important. This defines the size of your menu, which is 20 in height and 100 in width. Otherwise, no menu can be seen.
Protected override void onmeasureitem (measureitemeventargs E)
{
E. itemheight = 20;
E. itemwidth = 100;
}
}

 

Note: Here I didn't draw the button when I pressed it (lazy :), mainly for further improvement). Of course, I didn't draw any icons, but also for future improvement. This is just a preliminary form, let's take a look at some more advanced methods ?!

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.