Adding an entry to the upper left corner icon menu in WinForms?

I have a WinForms application, and I would like to add a menu item to the menu that opens when the user clicks on the upper left corner of the window (icon) or press ALT + SPACE.

The form shows me only MainMenu and ContextMenu, but there is no iconic menu or anything like that. Is there an easy way to change this in a WinForms application?

I'm talking about this menu, and I want to add a simple “O” entry, just so that people can check the version and URL from the app. There is no good place in the normal user interface (there is no main menu).

Alt + Space Menu http://img513.imageshack.us/img513/3162/altspacemenu.jpg

+3
source share
3 answers

This menu is added to the form when you set the FormBorderStyle to everything except "None". When the shape frame style changes, a procedure called AdjustSystemMenu is called. This procedure uses the GetSystemMenu method to retrieve SystemMenu? from somewhere. “Somewhere” is a problem. It seems that the SystemMenu object is not accessible.

EDIT: Just find the link looks like it can 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);
    }
}
+4
source

There is a set of Windows API functions that can receive and manage this menu.

In C # check this example:

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

+3
source

AFAIK .Net .

, Windows API, WinMain WndProc

0

Source: https://habr.com/ru/post/1714433/


All Articles