Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

10. Windows Forms Menus

10.6 How do I prevent the context menu from showing up on certain keyboard keys (like Keys.Apps)?


Override WndProc in your Control and do the following. You should then listen to keyup and show the context menu yourself.


[C#]
protected override void WndProc(ref Message m)
{
     if(m.Msg == 0x7b /*WM_CONTEXTMENU*/ )
     {
          return;
     }
     if(m.Msg == 0x101 /*WM_KEYUP*/)
     {
          Keys keys = (Keys)m.WParam.ToInt32();
          // Prevent this key from being processed.
          if(keys == Keys.Apps)
               return;
     }
     base.WndProc(ref m);
}



[VB.Net]
Protected Overrides Sub WndProc(ByRef m As Message)
     If m.Msg = 0x7b Then 'WM_CONTEXTMENU
          Return
     End If
     If m.Msg = 0x101 Then 'WM_KEYUP
          Dim keys As Keys = CType(m.WParam.ToInt32(), Keys)
          ' Prevent this key from being processed.
          If keys = Keys.Apps Then
               Return
          End If
     End If
     MyBase.WndProc( m)
End Sub