C # Landscaping MenuStrip Custom MenuStrip controls

Source: Internet
Author: User


Change the appearance of the MenuStrip so that it is no longer the default appearance of the system the interface is always pleasing to the eye of the article is simple but the effect is very good for beginners like me

I have seen such an article in the Programmer's window but my own level is not able to achieve as he said the beautification cannot be fully realized. NET class Library provides an abstract class that renders the appearance of the menu, so it integrates a specialized class and overrides some of these methods to implement changes in the appearance to see the effect:

Here we customize a control to inherit from the system MenuStrip MenuStrip has a renderer property This property accepts an object of a System.Windows.Forms.ToolStripRenderer class this class defines the menu Appearance of toolbars This class is an abstract class system menu appearance is defined by the Toolstripprofessionalrenderer class Toolstripprofessionalrenderer class inherits from System.Windows.Forms.ToolStripRenderer

We don't have to inherit toolstriprenderer in order to reduce the workload. This abstract class Toolstripprofessionalrenderer defines the appearance of the menu bar toolbar we inherit from this professional class override it some ways to customize the appearance. So the main thing is to implement the class that defines the appearance

First, create a class customprofessionalrenderer inherit from System.Windows.Forms.ToolStripProfessionalRenderer

Overload a field with a theme color for this class by overloading its constructor to assign a value to a field to create a rendering object of different shades

1         private Color _color = color.red;
2 public         Customprofessionalrenderer (): Base ()
3         {
4         }
5         public Customprofessionalrenderer (color color): Base ()
6         {
7             _color = color;
8         }


Add an auxiliary function to get rounded rectangular area

1         //Get rounded rectangular area  radius= diameter
 2 public         static GraphicsPath Getroundedrectpath (Rectangle rect, int radius)
 3         {
 4             int diameter = radius;
 5             Rectangle arcrect = new Rectangle (rect. Location, new Size (diameter, diameter));
 6             GraphicsPath Path = new GraphicsPath ();
 7 
 8             //upper left-hand corner
 9             path. AddArc (Arcrect, 180,);
arcrect.x = rect in the upper right corner             . Right-diameter;             path. AddArc (Arcrect, 270,);
arcrect.y = rect in the lower right corner             . Bottom-diameter;             path. AddArc (arcrect, 0,);
arcrect.x = rect in the lower left corner             . Left;             path. AddArc (Arcrect, N,);             path. Closefigure ();
return to             path;         }


Then rewrite some methods of the base class to change the appearance

Render Background

1//rendering background includes MenuStrip background ToolStripDropDown background 2 protected override void Onrendertoolstripbackground (Toolstri
 Prendereventargs e) 3 {4 ToolStrip ToolStrip = E.toolstrip;
 5 Graphics g = e.graphics;
 6 G.smoothingmode = smoothingmode.highquality;//anti-aliasing 7 Rectangle bounds = e.affectedbounds; 8 LinearGradientBrush Lgbrush = new LinearGradientBrush (new point (0, 0), new Point (0, toolstrip.height), Color .
 FromArgb (255,color.white), Color.FromArgb (150,_color));             9 if (ToolStrip is MenuStrip) 10 {11//By MenuStrip paint method This does not do operation 12                 ' Else if ' (ToolStrip is toolstripdropdown) {int diameter = 10;//diameter 16
GraphicsPath path = new GraphicsPath ();
Rectangle rect = new Rectangle (Point.empty, toolstrip.size); Rectangle arcrect = new Rectangle (rect. Location, New Size (dIameter, diameter)); Path.
AddLine (0, 0, 10, 0); 21//Top Right-hand corner arcrect.x = rect.
Right-diameter; The path.
AddArc (Arcrect, 270, 90); 24 25//lower right corner Arcrect.y = rect.
Bottom-diameter; Path.
AddArc (arcrect, 0, 90); 28 29//lower left corner arcrect.x = rect.
Left; To path.
AddArc (Arcrect, 90, 90); The path.
Closefigure ();
Toolstrip.region = new Region (path);
G.fillpath (Lgbrush, path); More than is the base.
Onrendertoolstripbackground (e); 39} 40}


Render border

       Render borders do not draw borders
        protected override void Onrendertoolstripborder (ToolStripRenderEventArgs e)
        {
            //methods that do not call the base class Block out this method remove the border
        }

Render Arrow Color

1         //render arrows change arrow color
2         protected override void Onrenderarrow (Toolstriparrowrendereventargs e)
3         {
4             e.arrowcolor = _color;
5             base. Onrenderarrow (e);
6         }


Render menu item

 1//render item does not call the base class with the same name Method 2 protected override void Onrendermenuitembackground (ToolStripItemRenderEventArgs e)
 3 {4 Graphics g = e.graphics;
 5 ToolStripItem item = E.item;
 6 ToolStrip ToolStrip = E.toolstrip;                 7 8 9//Render Top level ToolStrip (MenuStrip) 11 {12 LinearGradientBrush Lgbrush = new LinearGradientBrush (0, 0), new Point (0, item.
Height), Color.FromArgb (Color.White), Color.FromArgb (0, Color.White));
SolidBrush brush=new SolidBrush (Color.FromArgb (255,color.white)); e.item.selected GraphicsPath GP = Getroundedrectpath (n EW Rectangle (new Point (0, 0), item.
Size), 5);
G.fillpath (Lgbrush, GP); if (item.         Pressed) 20 {21////creates a rectangular path above the left and right 2 rounded corners 22            GraphicsPath path = new GraphicsPath ();
//int diameter = 8; //rectangle rect = new Rectangle (Point.empty, item.
Size); //rectangle arcrect = new Rectangle (rect.
Location, new Size (diameter, diameter)); 26////, upper left corner,//path.
AddArc (Arcrect, 180, 90); 28////on the upper right corner of//arcrect.x = rect.
Right-diameter; //path.
AddArc (Arcrect, 270, 90); //path. AddLine (new Point (rect). Width, Rect. Height), new Point (0, Rect.
Height)); //path.
Closefigure ();
33////Fill Path//g.fillpath (brush, path); G.fillrectangle (brushes.white,new Rectangle (Point.empty,item).
Size));             36} 37} 38//Render dropdown item ' else if ' (ToolStrip is ToolStripDropDown) 40 {G.smootHingmode = smoothingmode.highquality; LinearGradientBrush Lgbrush = new LinearGradientBrush (0, 0), new Point (item.
Width, 0), Color.FromArgb (_color), Color.FromArgb (0, Color.White)); if (item. Selected) GraphicsPath GP = Getroundedrectpath (new Rectangle (0, 0, item. Width, item.
Height), 10);
G.fillpath (Lgbrush, GP); The more than {.
Onrendermenuitembackground (e); 52} 53}


Rendering boundaries

1         protected override void Onrenderseparator (ToolStripSeparatorRenderEventArgs e)
2         {
3             Graphics g = e.graphics;
4 
5             LinearGradientBrush lgbrush = new LinearGradientBrush (new point (0, 0), New Point (e.item.width, 0), _color, Co Lor. FromArgb (0, _color));
6             G.fillrectangle (Lgbrush, New Rectangle (3,e.item.height/2,e.item.width,1));
7             //base. Onrenderseparator (e);
8         }


The left picture area of the render Drop-down menu

1         ///Render Picture Area dropdown menu to the left of the picture area
2         protected override void Onrenderimagemargin (ToolStripRenderEventArgs e)
3         {
4             //base. Onrenderimagemargin (e);
5             //shield off left picture vertical bar
6         }


To this main method of rewriting is almost the other way, you can also control other appearance such as the small hook on the left of the item in the selected state, etc.

Next, create a control to inherit from MenuStrip and design a ThemeColor property API for changing theme colors externally

1 public partial class Customcontrls_menustrip:menustrip
 2 {
 3     private Color _themecolor = Color.gray;
 4 public     Customcontrls_menustrip ()
 5     {
 6         InitializeComponent ();
 7 this         . Renderer = new Customprofessionalrenderer (_themecolor);
 8     }
 9 public     Color themecolor
{         _themecolor}         set         {             _themecolor = value;
This             . Renderer = new Customprofessionalrenderer (_themecolor);
}
18}


This makes our custom MenuStrip control, which retains all the functionality of the System menu control and adds an attribute to replace the menu style. Drag customcontrls_menustrip onto a form in the IDE to start using


A small amount of modification to achieve a more beautiful appearance is the most sensible choice without putting too much effort into the interface

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.