Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an entry to the System Menu (Left-Upper-Corner Icon Menu) in WinForms?

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).

like image 910
Michael Stum Avatar asked Aug 05 '09 19:08

Michael Stum


1 Answers

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);
    }
}
like image 67
Stewbob Avatar answered Sep 29 '22 07:09

Stewbob