5

I have a WinForms app, and I would like to add a menu entry to the menu that opens when the user clicks on the upper left corner of a window (the Icon) or presses ALT+SPACE.

Form only shows me a MainMenu and a ContextMenu, but no SystemMenu or something like that. Is there a simple way to modify this on a WinForms app?

I want to add a simple "About" entry, just for people to check the version and URL from within the application. There is no good place in the usual UI for this (no Main Menu).

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • You should have pasted an image of a modified system menu to illustrate the point better and guide the answers. – Vinko Vrsalovic Aug 05 '09 at 20:04
  • I was looking for one, although I could not find one now. Did not want to use Chrome because it's modified UI – Michael Stum Aug 05 '09 at 20:07
  • possible duplicate of [How can I customize the system menu of a Windows Form?](http://stackoverflow.com/questions/4615940/how-can-i-customize-the-system-menu-of-a-windows-form) – Cody Gray - on strike May 20 '12 at 08:56

3 Answers3

8

This menu is added to the form when you set the FormBorderStyle to anything except 'None'. When the form border style is changed, a routine called AdjustSystemMenu is called. This routine uses a GetSystemMenu method to retrieve a SystemMenu? from somewhere. The 'somewhere' is the problem. There does not appear to be a SystemMenu object anywhere that can be accessed.

EDIT: Just found this link, it looks like it might do what you want.

public partial class Form1 : Form
{
    #region Win32 API Stuff

    // Define the Win32 API methods we are going to use
    [DllImport("user32.dll")]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

    [DllImport("user32.dll")]
    private static extern bool InsertMenu(IntPtr hMenu, Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, string lpNewItem);

    /// Define our Constants we will use
    public const Int32 WM_SYSCOMMAND = 0x112;
    public const Int32 MF_SEPARATOR = 0x800;
    public const Int32 MF_BYPOSITION = 0x400;
    public const Int32 MF_STRING = 0x0;

    #endregion

    // The constants we'll use to identify our custom system menu items
    public const Int32 _SettingsSysMenuID = 1000;
    public const Int32 _AboutSysMenuID = 1001;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        /// Get the Handle for the Forms System Menu
        IntPtr systemMenuHandle = GetSystemMenu(this.Handle, false);

        /// Create our new System Menu items just before the Close menu item
        InsertMenu(systemMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty); // <-- Add a menu seperator
        InsertMenu(systemMenuHandle, 6, MF_BYPOSITION, _SettingsSysMenuID, "Settings...");
        InsertMenu(systemMenuHandle, 7, MF_BYPOSITION, _AboutSysMenuID, "About...");
    }

    protected override void WndProc(ref Message m)
    {
        // Check if a System Command has been executed
        if (m.Msg == WM_SYSCOMMAND)
        {
            // Execute the appropriate code for the System Menu item that was clicked
            switch (m.WParam.ToInt32())
            {
                case _SettingsSysMenuID:
                    MessageBox.Show("\"Settings\" was clicked");
                    break;
                case _AboutSysMenuID:
                    MessageBox.Show("\"About\" was clicked");
                    break;
            }
        }

        base.WndProc(ref m);
    }
}
Jim Fell
  • 13,750
  • 36
  • 127
  • 202
Stewbob
  • 16,759
  • 9
  • 63
  • 107
3

There's a Windows API set of functions that can get and manipulate that menu.

For C# check this example:

http://www.codeguru.com/csharp/csharp/cs_misc/userinterface/article.php/c9327

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
0

AFAIK there is no .Net way to do this.

To enable you do this you'd have to drop to the Windows API and I would suggest looking at WinMain and WndProc

Nathan
  • 931
  • 1
  • 12
  • 26