6

How can I disable right click on title bar of the form and prevent showing system context menu:

enter image description here

Help me to get out of this issue

thank you

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 2
    winforms = winapi, you could [process windows messages](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.message) and prevent certain to reach `base.WndProc()`. Not sure how exactly though. – Sinatr Nov 21 '19 at 12:09
  • 2
    @moathnaji In fact the suggested duplicate has nothing to do with this question. – Reza Aghaei Nov 21 '19 at 14:06
  • @RezaAghaei Sorry its seems to me that same functionality ! – moath naji Nov 22 '19 at 15:55

3 Answers3

6

If you specifically want to disable showing system context menu on right click on window's title bar, you can handle WM_CONTEXTMENU:

const int WM_CONTEXTMENU = 0x007B;
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_CONTEXTMENU)
        m.Result = IntPtr.Zero;
    else
        base.WndProc(ref m);
}

If you also want to prevent the possibility of clicking on form's icon to show the context menu, then you can set ShowIcon property of the form to false:

this.ShowIcon = false;
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

Have you tried setting your Window Style property to 'None'? This should remove the Title Bar's Context Menu completely.

emmademontford
  • 122
  • 1
  • 11
  • but I need title bar and want to disable right click. I Managed to stop double click now just need to stop right cliick – Nishtha Tarini Nov 21 '19 at 12:02
  • You'll need to use this code (or something similar) to capture the right mouse click. You'll want to use this in the MouseClick event of your title bar. [This link](https://www.codeproject.com/Articles/7273/How-to-detect-a-left-mouse-click-on-a-Winform-titl) may help you. `if (e.Button.ToString().ToLower()=="right")` `{` `//do whatever you need.` `}` – emmademontford Nov 21 '19 at 12:10
0

You can achieve this by setting the ControlBox property of form to false.

public Form1()
{
     InitializeComponent();
     this.ControlBox = false;
}

No more Context Menu with Restore, Maximize Minimize, Close, Move will be displayed on Right Clicking on the Title Bar.

this.ControlBox = false;

Note: Above one line (this.ControlBox = false;), is the key and the Form1 Constructor with InitializeComponent() method call is shown is the sample, just to show the context.

Habeeb
  • 7,601
  • 1
  • 30
  • 33