zoukankan      html  css  js  c++  java
  • 重写MenuStrip颜色方法

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            menuStrip1.Renderer = new MenuItemRenderer();
        }
    
        private void item1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("click item1");
        }
    }
    
    class MenuBarColor : ProfessionalColorTable
    {
        // menu background
        Color ManuBarCommonColor = Color.Brown;
        Color SelectedHighlightColor = Color.BurlyWood;
        // all items border
        Color MenuBorderColor = Color.Chartreuse;
        // menu bo
        Color MenuItemBorderColor = Color.Coral;
        /// <summary> 
        /// Initialize a new instance of the Visual Studio MenuBarColor class. 
        /// </summary> 
        public MenuBarColor()
        {
        }
        public override Color MenuItemPressedGradientBegin
        {
            get
            {
                return ManuBarCommonColor;
            }
        }
        public override Color MenuItemPressedGradientEnd
        {
            get
            {
                return ManuBarCommonColor;
            }
        }
        public override Color MenuItemPressedGradientMiddle
        {
            get
            {
                return ManuBarCommonColor;
            }
        }
        public override Color ButtonSelectedHighlight
        {
            get
            {
                return ManuBarCommonColor;
            }
        }
        public override Color MenuItemSelectedGradientBegin
        {
            get
            {
                return ManuBarCommonColor;
            }
        }
        public override Color MenuItemSelectedGradientEnd
        {
            get
            {
                return ManuBarCommonColor;
            }
        }
        public override Color MenuBorder
        {
            get
            {
                return MenuBorderColor;
            }
        }
        public override Color MenuItemBorder
        {
            get
            {
                return MenuItemBorderColor;
            }
        }
    }
    
    class MenuItemRenderer : ToolStripProfessionalRenderer
    {
        // text
        Font textFont = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        private Color menuItemSelectedColor = Color.Gray;
        private Color menuItemBorderColor = Color.Black;
        /// <summary> 
        /// Initialize a new instance of the Visual Studio MenuBarRenderer class. 
        /// </summary> 
        public MenuItemRenderer() : base(new MenuBarColor())
        {
            // item background
            this.menuItemSelectedColor = Color.Red;
            // item border
            this.menuItemBorderColor = Color.Blue;
        }
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            e.TextFont = textFont;
            if (e.Item.IsOnDropDown)
            {
                if (e.Item.Selected)
                {
                    e.TextColor = Color.White;
                }
                else
                {
                    e.TextColor = Color.Black;
                }
            }
            base.OnRenderItemText(e);
        }
        // Backgrounds
        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            if (e.Item.IsOnDropDown)
            {
                if (e.Item.Selected == true && e.Item.Enabled)
                {
                    DrawMenuDropDownItemHighlight(e);
                }
            }
            else
            {
                base.OnRenderMenuItemBackground(e);
            }
        }
    
        // DrawMenuDropDownItemHighlight
        private void DrawMenuDropDownItemHighlight(ToolStripItemRenderEventArgs e)
        {
            Rectangle rect = new Rectangle();
            rect = new Rectangle(2, 0, (int)e.Graphics.VisibleClipBounds.Width - 4, (int)e.Graphics.VisibleClipBounds.Height - 1);
            using (SolidBrush brush = new SolidBrush(menuItemSelectedColor))
            {
                e.Graphics.FillRectangle(brush, rect);
            }
            using (Pen pen = new Pen(menuItemBorderColor))
            {
                e.Graphics.DrawRectangle(pen, rect);
            }
        }
    }
  • 相关阅读:
    Linux软件安装【JDK安装】
    接口自动化<009>:Python 接口自动化Mock使用
    接口自动化<008>:Python 自定义装饰器
    接口自动化<007>:Python 装饰器 @functools.wraps(func)
    接口自动化<006>:Python 装饰器 @retry
    接口自动化<005>:Python中*args 和 **kwargs的用法详解
    接口自动化<004>:json.dumps()、json.loads()与json.dump()、json.load()
    接口自动化<003>:request部分参数解析
    pycharm常用设置及快捷键说明
    接口自动化<002>:session.request 会话用作前后文管理器
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/10001118.html
Copyright © 2011-2022 走看看