none
GetSystemMenu() method returns zero.

    Question

  • Hi,

    I have created a custom form by deriving it from System.Windows.Forms.Form class.

    I have override the WndProc(ref Message m) method to process the window messages as per my requirement.

    Now i'm in need of displaying a context menu(with the options Restore, Maximize, Minimize, Move, Size, Close) on the title bar of the form when right clicked.

    For that I have tried the below method.

     void On_Wm_ContextMenu(ref Message m)
            {
                IntPtr menu = GetSystemMenu(this.Handle, false);
                if (menu != IntPtr.Zero)
                {
                    int cmd = (int)TrackPopupMenu(menu, 0x0100, 10, 10, 0, this.Handle, IntPtr.Zero);
                    if (cmd != 0)
                        SendMessage(m.HWnd, 274, cmd, m.LParam);
                }
            }

    But this code doesn't work. The GetSystemMenu(this.Handle, false) method always return zero.

    I have passed the window handle of the custom form as argument.

    I don't know whether it is the correct way. 

    I have derived the control from System.Windows.Forms.Form. Why its default behavior doesn't holds for its derived class.

    Please share your valuable suggestions.

    Thanks in advance

    Wednesday, May 3, 2017 12:41 PM

Answers

  • Probably your custom form does not have the ControlBox property. Maybe try this: initially set ControlBox=True. Then handle the Load event: get the menu and keep it in some member, then set ControlBox=False. Later use the member to show the menu.




    Wednesday, May 3, 2017 7:27 PM

All replies

  • I created a derived class from Form to test

    and the context menu is present by default (Windows 10, VS 2015)

    Wednesday, May 3, 2017 1:34 PM
  • Probably your custom form does not have the ControlBox property. Maybe try this: initially set ControlBox=True. Then handle the Load event: get the menu and keep it in some member, then set ControlBox=False. Later use the member to show the menu.




    Wednesday, May 3, 2017 7:27 PM
  • Thank you so much. I have tried as you said. It works now perfectly. 

    Thursday, May 4, 2017 3:50 AM