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 ?!