C # Custom styles for menus

Source: Internet
Author: User

Windows immutable menu style, so many developers in the pursuit of visual effects, feel very tangled, perhaps the most common method is the texture, or custom components to implement. In C #, Microsoft provides render to beautify menus and toolbars, just to be used in this, written and shared.

Create a new class, define a color configuration class, here is only a list of the color of the various parts, you can save read and other operations to achieve the effect of skin change.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Using System.Drawing;
        Namespace WindowsApplication1 {public class Colorconfig {private Color _fontcolor = Color.White;
            <summary>///Menu Font Color///</summary> public color FontColor {
            get {return this._fontcolor;}
        set {This._fontcolor = value;}
        Private Color _marginstartcolor = Color.FromArgb (113, 113, 113);
        <summary>///drop-down Menu coordinates icon area start color///</summary> public color Marginstartcolor
            {get {return this._marginstartcolor;}
        set {This._marginstartcolor = value;}
        Private Color _marginendcolor = Color.FromArgb (58, 58, 58);
            <summary>///drop-down Menu coordinates icon area end color///</summary> public color Marginendcolor { get {return This._marginendcoloR
        set {This._marginendcolor = value;}
        Private Color _dropdownitembackcolor = Color.FromArgb (34, 34, 34);
            <summary>///dropdown background color///</summary> public color Dropdownitembackcolor {
            get {return this._dropdownitembackcolor;}
        set {This._dropdownitembackcolor = value;}
        Private Color _dropdownitemstartcolor = Color.orange;
        <summary>///drop-down Items start color///</summary> public color Dropdownitemstartcolor When selected
            {get {return this._dropdownitemstartcolor;}
        set {This._dropdownitemstartcolor = value;}
        Private Color _dorpdownitemendcolor = Color.FromArgb (160,100,20);
        <summary>///the end color///</summary> public color Dropdownitemendcolor when the dropdown item is selected
            {get {return this._dorpdownitemendcolor;} set {This._dOrpdownitemendcolor = value;
        } private Color _menuitemstartcolor = Color.FromArgb (52, 106, 159);
        <summary>///start color///</summary> public color Menuitemstartcolor when the main menu item is selected
            {get {return this._menuitemstartcolor;}
        set {This._menuitemstartcolor = value;}
        Private Color _menuitemendcolor = Color.FromArgb (73, 124, 174);
            <summary>///The end color of the main menu item when selected///</summary> public color Menuitemendcolor {
            get {return this._menuitemendcolor;}
        set {This._menuitemendcolor = value;}
        Private Color _separatorcolor = Color.gray;
            <summary>///Split Line color///</summary> public color Separatorcolor {
            get {return this._separatorcolor;}
        set {This._separatorcolor = value;} } Private Color _mainmenubackcoloR = Color.Black;
            <summary>///Main Menu background color///</summary> public color Mainmenubackcolor {
            get {return this._mainmenubackcolor;}
        set {This._mainmenubackcolor = value;}
        Private Color _mainmenustartcolor = Color.FromArgb (93, 93, 93);
            <summary>///main Menu background start color///</summary> public color Mainmenustartcolor {
            get {return this._mainmenustartcolor;}
        set {This._mainmenustartcolor = value;}
        Private Color _mainmenuendcolor = Color.FromArgb (34, 34, 34);
            <summary>///main Menu background end color///</summary> public color Mainmenuendcolor {
            get {return this._mainmenuendcolor;}
        set {This._mainmenuendcolor = value;}
        Private Color _dropdownborder = Color.FromArgb (40, 96, 151); <summary>///Dropdown area borderColor///</summary> Public color Dropdownborder {get {return This._dropdownborde R
        set {This._dropdownborder = value;}
 }
    }
}

To create a new class, we inherit from Toolstripprofessionalrenderer a render

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Drawing;

Using System.Drawing.Drawing2D; Namespace WindowsApplication1 {public class Mymenurender:toolstripprofessionalrenderer {Colorconfig Co Lorconfig = new Colorconfig ()//Create Color Configuration class        ///<summary>        ///rendering 
Full background        ///</summary>        ///<param name= "E" ></param>         protected override void Onrendertoolstripbackground (ToolStripRenderEventArgs e)   &NBSP ;     {          E.toolstrip.forecolor = Colorconfig.
FontColor;            //If Drop-down             if (E.toolstrip is TOOLSTRIPDR Opdown)             {                E.graphIcs. FillRectangle (New SolidBrush (Colorconfig).
Dropdownitembackcolor), e.affectedbounds);                         //If menu item             Else if (E.toolstrip is MenuStrip)             {       
        Blend Blend = new Blend ();
                float[] fs = new Float[5] {0f, 0.3f, 0.5f, 0.8f, 1f};
                float[] f = new Float[5] {0f, 0.5f, 0.9f, 0.5f, 0f};                 blend.
positions = FS;                 blend.
Factors = f;                 filllinegradient (E.graphics, E.affectedbounds, Colorconfig. Mainmenustartcolor, Colorconfig.
Mainmenuendcolor, 90f, blend);            }     &nbsp       Else             {              &NB Sp Base.
Onrendertoolstripbackground (e);
                    {       ///<summary>        ///rendering dropdown left icon area        ///</summary>        //
        /<param name= "E" ></param> protected override void Onrenderimagemargin (ToolStripRenderEventArgs e) {filllinegradient (E.graphics, E.affectedbounds, Colorconfig.) Marginstartcolor, Colorconfig.
        Marginendcolor, 0f, NULL);         ///<summary>        ///render menu item background        /// </summary>        ///<param name= "E" ></param>         protect ed override void Onrendermenuitembackground (ToolStripItemRenderEventArgs e) &nBsp        {     ,       if (E.toolstrip is MenuStrip)             {               //If selected or pressed for           &N Bsp     if (e.item.selected | | e.item.pressed)                 {  &NBS P
                Blend Blend = new Blend ();                      float[] fs = new Float[5] {0f, 0.3f, 0.5f, 0.8f, 1f
}; 
                    float[] f = new Float[5] {0f, 0.5f, 1f, 0.5f, 0f};                     blend.
positions = FS;                     blend.
Factors = f;                     filllinegradient (e.graphics, new rectaNgle (0, 0, E.item.size.width, e.item.size.height), Colorconfig. Menuitemstartcolor, Colorconfig.
Menuitemendcolor, 90f, blend);                                 Else &NB Sp                   base.
Onrendermenuitembackground (e);                         ELSE if (E.toolstrip is Toolstripdro Pdown)             {                if (E.item.sele CTED)                 {                &N Bsp   Filllinegradient (E.graphics, New Rectangle (0, 0, E.item.size.width, e.item.size.height), Colorconfig. Dropdownitemstartcolor, Colorconfig.
Dropdownitemendcolor, 90f, NULL);                            }             else             {        & nbsp       base.
Onrendermenuitembackground (e);
                    {       ///<summary>         ///render menu single line of divider        ///</summary>        /// <param name= "E" ></param> protected override void Onrenderseparator (ToolStripSeparatorRenderEventArgs e) {e.graphics.drawline (new Pen (Colorconfig).
        Separatorcolor), 0, 2, e.item.width, 2);        ///<summary>        ///rendering frame        /// ;/summary>        ///<param name= "E" ></param> protected override void Onrende Rtoolstripborder (ToolStripRenderEventArgs e) {if (E.toolstrip is ToolSTripdropdown) {e.graphics.drawrectangle (new Pen) (Colorconfig.
            Dropdownborder), New Rectangle (0,0,e.affectedbounds.width-1,e.affectedbounds.height-1)); } else {base.
            Onrendertoolstripborder (e); }        ///<summary>        ///render arrow       &NBS P </summary>        ///<param name= "E" ></param>         Prot ected override void Onrenderarrow (Toolstriparrowrendereventargs e)         {      &NBS P     E.arrowcolor = color.red;//set to red, of course, can also draw a variety of shapes             base.
Onrenderarrow (e);        }        ///<summary>        ///filling linear gradient &nbsp ;      ///</summary>        ///<param name= "G"; Canvas </param>        ///<param name= "rect" > Fill area </param>         <param name= "StartColor" > Start color </param>        ///<param name= "EndColor" > End color </param>        ///<param name= "angle" > Angle </param>        /// <param name= "Blend" > object blending pattern </param> private void filllinegradient (Graphics g, Rectangle rect, Color St Artcolor, Color endcolor, float angle, Blend Blend) {LinearGradientBrush Linebrush = new Lineargradie
            Ntbrush (Rect, StartColor, endcolor, Angle); if (blend!= null) {Linebrush.
            Blend = Blend;
            } GraphicsPath Path = new GraphicsPath (); Path.
            AddRectangle (rect);
            G.smoothingmode = Smoothingmode.antialias;
        G.fillpath (Linebrush, path);
 }
    }
}

Create a new component, inherit from MenuStrip, as our custom menu

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Diagnostics;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;

Namespace WindowsApplication1
{public
    partial class Mymenu:menustrip
    {public
        mymenu ()
        {
            InitializeComponent ();
            This. Renderer = new Mymenurender ()//Set render
        } public

        MyMenu (IContainer container)
        {
            container. ADD (this);

            InitializeComponent ();
            This. Renderer = new Mymenurender ();//Set Render}}}

Well, compile the project, you will see the MyMenu component in the left-hand toolbar, drag one up to be able to use, in fact directly set up the MenuStrip render attribute can also, for example, set pop-up menu rendering

Contextmenustrip1.renderer = new Jrjmenurender ();


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.