adspace
How to show a ContextMenuStrip instead of cthe default ContextMenuStrip,when you rightclick on the non client area of a window's Form or when alt+space keys are pressed
Answer Posted / Aarti Rani
To display a custom ContextMenuStrip instead of the default one in Windows Forms application when right-clicking on the non-client area (e.g., title bar) or pressing Alt+Space, you can use the PreviewMouseDown event and set the Cancel property to true if certain conditions are met. Here's an example:
1) Create a ContextMenuStrip object with your custom menu items.
2) In the Form constructor, subscribe to the PreviewMouseDown event:
```csharp
this.PreviewMouseDown += new MouseEventHandler(Form_PreviewMouseDown);
```
3) Implement the PreviewMouseDown event handler:
```csharp
private void Form_PreviewMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && !this.IsHandleCreated || (e.X >= this.ClientRectangle.Left && e.Y >= this.ClientRectangle.Top && e.X <= this.ClientRectangle.Right && e.Y <= this.ClientRectangle.Bottom))
{
if ((GetAsyncKeyState(0x12) & 0x8000) != 0) // check if Alt key is pressed (Alt+Space)
{
cts.Show(e.Location); // show the custom ContextMenuStrip
e.Handled = true; // cancel the default context menu
}
}
}
```
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
No New Questions to Answer in this Category !! You can
Post New Questions
Answer Questions in Different Category