Make a MenuStrip Category Item

All tutorials created in C# to be posted in here.
2 posts Page 1 of 1
Contributors
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Make a MenuStrip Category Item
XTechVB
In this tutorial you will learn how to make a MenuStrip Category Item like the one in the picture bellow.
MenuStripCategoryItem.PNG
First start by creating a new class named CCategoryItem

Now add this Namespaces and Inherit ToolStripMenuItem
Code: Select all
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
Next we must write the Constructor and Properties
Constructor
Code: Select all
        #region Constructor
        public CCategoryItem()
        {
            this.AutoSize = false;
            this.Dock = DockStyle.Fill;
            this.Font = new Font("Arial", 8, FontStyle.Bold);
        }
        #endregion
Properties
Code: Select all
#region Properties
        //Logic
        public override bool CanSelect
        {
            get
            {
                return false;
            }
        }
        //Colors
        private Color _BackgroundColor = Color.FromArgb(236, 241, 247);
        public Color BackgroundColor
        {
            get { return _BackgroundColor; }
            set { _BackgroundColor = value; this.Invalidate(); }
        }
        private Color _BorderColor = Color.FromArgb(209, 208, 206);
        public Color BorderColor
        {
            get { return _BorderColor; }
            set { _BorderColor = value; this.Invalidate(); }
        }
        private Color _TextColor = Color.FromArgb(39, 72, 103);
        public Color TextColor
        {
            get { return _TextColor; }
            set { _TextColor = value; this.Invalidate(); }
        }
        #endregion
Now the Drawing begins. First we must draw the Background and then the Text
Background
Code: Select all
        #region Draw Background
        private void DrawCategoryBackground(Graphics e)
        {
            //Get Rectangle
            Rectangle _ItemRect = new Rectangle(0, 0, this.Width, this.Height);
            //Draw
            using (SolidBrush _BackgroundBrush = new SolidBrush(_BackgroundColor))
            using (Pen _BorderPen = new Pen(_BorderColor))
            {
                //Draw Background
                e.FillRectangle(_BackgroundBrush, _ItemRect);
                //Draw Border
                e.DrawLine(_BorderPen, _ItemRect.X, _ItemRect.Height - 1, _ItemRect.X + _ItemRect.Width, _ItemRect.Height - 1);
            }
        }
        #endregion
Text
Code: Select all
        #region Draw Text
        private void DrawCategoryText(Graphics e)
        {
            //Get Text Size
            Size _TextSize = e.MeasureString(this.Text, this.Font).ToSize();
            //Draw
            using (SolidBrush _TextBrush = new SolidBrush(_TextColor))
            {
                //Draw Text
                e.DrawString(this.Text, this.Font, _TextBrush, 2, this.Height / 2 - _TextSize.Height / 2 - 1);
            }
        }
        #endregion
And finally override the System Paint
Code: Select all
        #region Override System Paint
        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e); //Ignore System Paint
            DrawCategoryBackground(e.Graphics);
            DrawCategoryText(e.Graphics);
        }
        #endregion
That's it we're finished. I hope you like this tutorial.
This code is all written by me you can use it or modify it anyway you want. Please don't share it on other websites as your own.
You do not have the required permissions to view the files attached to this post.
You can find me on Facebook or on Skype mihai_92b
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: Make a MenuStrip Category Item
Dummy1912
nice work
love it :)
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
2 posts Page 1 of 1
Return to “C-Sharp Tutorials”