I'd like to create a custom context menu. The idea is to create a panel with a textBox a button and a list of labels and be able to show it on right click and make it behave exactly like a contextMenu. I can probably use a form without borders but I was thinking there might be a class I can derive from that would help me handle the positionnig of the context menu and the shading. Any ideas? Thank you
Edit: An example to clear a few ideas: Say you have a label on your form, when you right click on it (or even left click) a menu appears. This menu is NOT the classic context menu but rather a custom panel with controls that I created personnaly. An example is search box ont top with a list of items. As you enter letters the list is trimmed to the matching items and when an item is clicked the context menu disappears and the value selected is wrtitten in the label we first clicked on.
custom context menu here. If we right-click on this page, the default menu will pop up. JavaScript code is used to block this default menu and then we will create our custom context menu. To block the default menu, we will add an event handler for processing right-click events on the webpage.
Edit the registryPress the Windows key and R simultaneously, type regedit and press Enter. Navigate to HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers and you will see a series of keys that related to existing menu entries.
A context menu is a menu that appears upon user interaction, such as a right-click. HTML now allows us to customize this menu. Here are some implementation examples, including nested menus.
When we click the right mouse button on our desktop, a menu-like box appears and this box is called the context menu. In JavaScript, a context menu event runs when a user tries to open a context menu. This can be done by clicking the right mouse button.
You can use the method described here:
http://www.codeproject.com/Articles/22780/Super-Context-Menu-Strip
Since it uses ContextMenuStrip you can set its position:
contextMenuStrip1.Show(Cursor.Position);
and shadow effect:
http://msdn.microsoft.com/en-us/library/system.windows.controls.contextmenu.hasdropshadow.aspx
The simplest way (since this doesn't appear to be an actual menu) would be to create a borderless form and add shadow to it:
public class ShadowForm : Form
{
// Define the CS_DROPSHADOW constant
private const int CS_DROPSHADOW = 0x00020000;
// Override the CreateParams property
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
}
Regarding position, there is not much to it. Just check Cursor.Position
or set coordinates using the arguments in your MouseUp
event handler.
Complete code would look something like:
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
protected override OnMouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var menu = new CustomMenu();
menu.Location = PointToScreen(e.Location);
menu.Show(this);
}
}
}
and for the "menu" form:
public partial class CustomMenu : Form
{
public CustomMenu()
{
InitializeComponent();
this.StartPosition = FormStartPosition.Manual;
}
private const int CS_DROPSHADOW = 0x00020000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
protected override void OnLostFocus(EventArgs e)
{
this.Close();
base.OnLostFocus(e);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With