Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#

Super Context Menu Strip

Rate me:
Please Sign up or sign in to vote.
4.77/5 (51 votes)
5 Oct 2009CPOL2 min read 195.8K   10K   224  
How to build a context menu that displays any group of controls
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace MENU
{
    public class SuperContextMenuStrip : ContextMenuStrip
    {
        private Control m_contents = null;


        public SuperContextMenuStrip(Control c)
        {
            // the control
            m_contents = c;
            m_contents.Location = Point.Empty;
            
            

            //the host
            ToolStripControlHost host = new ToolStripControlHost(m_contents);
            host.AutoSize = false;
            host.Padding = new Padding(0, 0, 0, 0);
            host.Margin = new Padding(0, 0, 0, 0);


            //the context menu
            this.ShowImageMargin = false;
            this.ShowCheckMargin = false;
            this.Padding = new Padding(0, 0, 0, 0);
            this.Margin = new Padding(0, 0, 0, 0);
            this.BackColor = c.BackColor;


            
            //done!
            this.Items.Add(host);
        }





        protected override bool ProcessDialogKey(Keys keyData)
        {
            //prevent alt from closing it and allow alt+menumonic to work
            if ((keyData & Keys.Alt) == Keys.Alt)
                return false;

            // solve the tab problem
            if (Keys.Tab == keyData)
            {
                Control lastControl = null;
                foreach (Control c in m_contents.Controls)
                {
                    if (c.Focused)
                        lastControl = c;
                }

                m_contents.SelectNextControl(lastControl, true, true, true, true);
            }


            if ((keyData & Keys.Shift) == Keys.Shift)
                if ((keyData & Keys.Tab) == Keys.Tab)
                {
                    Control firstControl = null;
                    foreach (Control c in m_contents.Controls)
                    {
                        if (c.Focused)
                            firstControl = c;
                    }

                    m_contents.SelectNextControl(firstControl, false, true, true, true);
                }

            return base.ProcessDialogKey(keyData);
        }


        //set the focus to it, so the user will start using it.
        protected override void OnOpened(EventArgs e)
        {
            m_contents.Focus();

            base.OnOpened(e);
        }



        //fade it while displaying it
        private bool m_fade = true;
        private const int frames = 5;
        private const int totalduration = 200;
        private const int frameduration = totalduration / frames;
        protected override void SetVisibleCore(bool visible)
        {
            double opacity = Opacity;
            if (visible && m_fade) Opacity = 0;
            base.SetVisibleCore(visible);
            if (!visible || !m_fade) return;
            for (int i = 1; i <= frames; i++)
            {
                if (i > 1)
                {
                    System.Threading.Thread.Sleep(frameduration);
                }
                Opacity = opacity * (double)i / (double)frames;
            }
            Opacity = opacity;
        }







    
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions