User context menu

I would like to create a custom context menu. The idea is to create a panel with a textBox button and a list of shortcuts and be able to show it with the right mouse button and make it behave exactly like contextMenu. Maybe I can use a form without borders, but I thought there might be a class that I can extract from this to help me handle the position in the context menu and the shading. Any ideas? thank you

Edit: an example to clear a few ideas: let's say you have a shortcut in your form, when you right-click (or even left-click) a menu appears. This menu is NOT a classic context menu, but a user panel with the controls that I created personnaly. An example is a search box from top to top with a list of items. When you enter letters, the list is adjusted to the corresponding elements, and when the element is clicked, the context menu disappears and the selected value is taken on the label that we first clicked on.

+4
source share
2 answers

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

+3
source

The easiest way (since this is not the actual menu) is to create a borderless form and add a 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; } } } 

As for the situation, this is not enough. Just check Cursor.Position or set the coordinates using the arguments in the MouseUp event MouseUp .

The full code will look something like this:

 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); } } 
+3
source

Source: https://habr.com/ru/post/1398996/


All Articles