Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

My QuickMouseMenu

0.00/5 (No votes)
11 Aug 2007 1  
My QuickMouseMenu

QuickMouseMenu1.jpgQuickMouseMenu2.jpg

Introduction

I am developing an Integrated System for a company, with lot of functionality (CRM, Invoicing, Store, Warranty Managemant, Treasury, etc.). So the application has lot of forms.

The first version of the app had a ToolStripMenu, with about 8-10 root menus and many submenus. Of course, the main features had shortcut keys, but not everybody likes shortcuts. Maybe we could use toolbars, but I don't like them, because you need to do toolbaritems for every menuitem, and it's so circumstantial...i think :)

I use DevExpress components in my applications, and they have a fantastic control: RibbonControl (Like new MS office 2007). I think it is a very-very usefull control with application menu, and statusbar and etc. (its usefull for users and usefull for developer, becaus u can add the same "Item" (same object with same handlers) to menu,to AppMenu, to Statusbar,etc...

But... the same problem with shortcuts and context menu... so i decided to make my own 'contextmenu', and it can work with DevExpress ribbon items ( not with all, but can with menus). For the 'pilot' project i made it for 2.0 MenuStrip...

Of course u can use it without menustrip!!!!

Using the code

Because it is a pilot project ( a pilot beta project) I have not made design support, error handling,etc for the Control. Really, it is not a Control, it is a Form, but if you need a UserControl or a Component, u can simply do it, with desing support...

The QuickMenuItem (class QuickMouseMenuItem)

Its a simple class to store a menuitem.

Properties:

private Image menuimage;

/// <summary>
/// menu image
/// 
public Image MenuImage
{
    get { return menuimage; }
    set { menuimage = value; }
}

private string description = "";

/// <summary>
/// menudescription to tooltip
/// 
public string Description
{
    get { return description; }
    set { description = value; }
}

private string caption = "";

/// <summary>
/// menucaption to tooltip
/// 
public string Caption
{
    get { return caption; }
    set { caption = value; }
}

private string key;

/// <summary>
/// the key to identify the menuitem
/// 
public string Key
{
    get { return key; }
    set { key = value; }
}

How can u use it?!? Its very easy: the Control (class QuickMouseMenuUC)) have one method(with some overrides) to add root items:

public QuickMouseMenuItem AddQuickMouseMenuItem(string key, Image img,
                          string Caption, string Description)
{
    QuickMouseMenuItem newitem = new QuickMouseMenuItem(img, Caption,
        Description);
    newitem.Key = key;
    lstRootItems.Add(newitem);

    return newitem;
}

And a QuickMouseMenuItem have two method to add child items:

//
/// <summary>
/// Add child menuItem
/// </summary>
/// <param name="childitem" />
/// <returns>
public QuickMouseMenuItem AddChildQuickMouseMenuItem(
                          QuickMouseMenuItem childitem)
{
    lstSubQuickMouseMenuItems.Add(childitem);
    childitem.ParentQuickMouseMenuItem = this;
    return childitem;
}

/// <summary>
/// Add child menu from Menustrip with Click event!!!
/// </summary>
/// </returns>
/// <param name="menustrip">
/// <returns>
public QuickMouseMenuItem CreateChildQuickMouseMenuItemFromMenuStrip(
       System.Windows.Forms.ToolStripMenuItem menustrip)
{
    QuickMouseMenuItem childitem = new QuickMouseMenuItem();
    childitem.caption = menustrip.Text;
    childitem.description = menustrip.ToolTipText;
    if (menustrip.Image == null)
    {                    
        childitem.menuimage = resQMM.defaultIMAGE;
    }
    else
    {
        childitem.menuimage = menustrip.Image;
    }
    tsmenuitem = menustrip;

    lstSubQuickMouseMenuItems.Add(childitem);
    childitem.ParentQuickMouseMenuItem = this;
    return childitem;
}

If you want to use it with MenuStrip you need to use the second method, and if you want to use it with another menu, or toolbar, or etc, write your own method!!!

And the only one thing remains to use it, you need to add the control(Form) to your application, for example:

//create quickmenu
QMM.QuickMouseMenuUC quickMouseMenuUC1 = new QMM.QuickMouseMenuUC();

quickMouseMenuUC1.Size = new Size(200, 200);

//setttings
//hide when leave, if false close than back click on top level
quickMouseMenuUC1.HideOnMouseLeave = true;

//if have submenu than navigate
quickMouseMenuUC1.NavigateOnHover = true;
foreach (Control act in this.Controls)
{
    //i add the handler to mdifrom mdiclient area
    if (act is MdiClient)
    {
        // add to mouse down event
        // do it for that controls you want...
        act.MouseDown += new MouseEventHandler(
            quickMouseMenuUC1.Parent_MouseDown);
        break;
    }
}

//QuickMenuItem click event...
quickMouseMenuUC1.QuickMenuItemClicked +=new MouseEventHandler(
     quickMouseMenuUC1_QuickMenuItemClicked);

The sample application contains a MDI app. demo with some demo menu, and it appears when the user click the mdiclient area. Some demo menuitems:

//add some test menu
quickMouseMenuUC1.AddQuickMouseMenuItem(respic.addd, "Add",
    "Adding new value...");
quickMouseMenuUC1.AddQuickMouseMenuItem(respic.cancel, "Cancel",
    "Cancel last operation...");
QMM.QuickMouseMenuItem file_menu = 
    quickMouseMenuUC1.AddQuickMouseMenuItem(respic.AppMenu32, "File",
    "File menu");
//add some test menu from toolstripitem
file_menu.CreateChildQuickMouseMenuItemFromMenuStrip(newToolStripMenuItem);
file_menu.CreateChildQuickMouseMenuItemFromMenuStrip(closeToolStripMenuItem);

//some menu with sub menu...
quickMouseMenuUC1.AddQuickMouseMenuItem(respic.user, "Users", "Useres form");
QMM.QuickMouseMenuItem qmm_eamil = quickMouseMenuUC1.AddQuickMouseMenuItem(
    respic.email2, "email");

QMM.QuickMouseMenuItem qmm_print = qmm_eamil.AddChildQuickMouseMenuItem(
    new QMM.QuickMouseMenuItem(respic.print, "Print", ""));

qmm_eamil.AddChildQuickMouseMenuItem(new QMM.QuickMouseMenuItem(respic.sync,
    "Sync", ""));

qmm_print.AddChildQuickMouseMenuItem(new QMM.QuickMouseMenuItem(respic.copy,
    "Copy", ""));
qmm_print.AddChildQuickMouseMenuItem(new QMM.QuickMouseMenuItem(respic.addd,
     "Add printer", "add printer"));

Now its Done!!!

So its a simple "control", in a simple demo, if you like it, download and customize it, anyway you want, and your application needs!!!

I do this, in my application and with devexpress controls (http://www.devexpress.com/Products/NET/WinForms/Index.xml).

Enjoy it and vote.

History

Beta 1.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here