欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# winform 自定义MenuStrip

程序员文章站 2022-06-08 16:03:48
...

NewMenuStrip.cs:

public partial class NewMenuStrip : MenuStrip
    {
        public static ToolStripMenuItem lastMenuItem = null;
        public static ToolStripMenuItem activeMenuItem = null;

        public NewMenuStrip()
        {
            this.Renderer = new ZHRender();
            this.ForeColor = Color.FromArgb(241, 240, 240);
            this.Height = 54;
            this.BackgroundImage = Properties.Resources.Rectangle;
            this.BackgroundImageLayout = ImageLayout.Stretch;
            this.AutoSize = false;
            this.Padding = new Padding(0, 0, 0, 0);
            this.Font = new System.Drawing.Font("微软雅黑", 13F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            if ((Screen.PrimaryScreen.Bounds.Width - 1225) / 2 > (82 + 22))
            {
                ToolStripMenuItem menuItem1 = new ToolStripMenuItem();
                menuItem1.Image = Properties.Resources.Icon;
                menuItem1.DisplayStyle = ToolStripItemDisplayStyle.Image;
                menuItem1.ImageScaling = ToolStripItemImageScaling.None;
                menuItem1.Margin = new System.Windows.Forms.Padding(22, 0, (Screen.PrimaryScreen.Bounds.Width - 1225) / 2 - 82 - 22, 0);
                menuItem1.ImageAlign = ContentAlignment.MiddleCenter;
                this.Items.Add(menuItem1);
            }            
        }

        public static ToolStripMenuItem ActiveMenuItem
        {
            get
            {
                return activeMenuItem;
            }
            set
            {
                activeMenuItem = value;
                SetImage(activeMenuItem);
            }
        }

        public ToolStripMenuItem FindCurrentItem(string text)
        {
            return FindCurrentItem(text, null);
        }

        public ToolStripMenuItem FindCurrentItem(string text, ToolStripMenuItem item)
        {
            ToolStripItemCollection tsic = this.Items;
            if (item != null)
            {
                tsic = item.DropDownItems;
            }

            foreach (ToolStripMenuItem tsmi in tsic)
            {
                if (tsmi.Text.Equals(text))
                {
                    return tsmi;
                }

                if (tsmi.DropDownItems.Count != 0)
                {
                    ToolStripMenuItem ts = FindCurrentItem(text, tsmi);
                    if (ts != null)
                    {
                        return ts;
                    }
                }
            }

            return null;
        }

        public void SetImageByText(string text)
        {
            ToolStripMenuItem tsmi = FindCurrentItem(text);
            if (tsmi != null)
            {
                ActiveMenuItem = tsmi;
            }
        }

        public static void SetImage(ToolStripMenuItem activeMenuItem)
        {
            if (activeMenuItem.DropDownItems.Count == 0)
            {
                if (lastMenuItem != null)
                {
                    GetTopItem(lastMenuItem).Image = Properties.Resources.Transparent;  
                }
                GetTopItem(activeMenuItem).Image = Properties.Resources.Triangle;             
                lastMenuItem = activeMenuItem;
            }
        }

        private static ToolStripMenuItem GetTopItem(ToolStripMenuItem item)
        {
            while (item.OwnerItem != null)
            {
                item = (ToolStripMenuItem)item.OwnerItem;
            }
            return item;
        }


    }

NewMenuStrip.Designer.cs:

partial class NewMenuStrip
    {
        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // NewMenuStrip
            // 
            this.ResumeLayout(false);
        }

        #endregion
    }