向窗体的系统菜单添加菜单项

[System.Runtime.InteropServices.DllImport("user32")]
private static extern IntPtr GetSystemMenu(IntPtr hwnd,bool bRevert);

[System.Runtime.InteropServices.DllImport("user32")]
private static extern IntPtr AppendMenu(IntPtr hMenu,int wFlags,IntPtr wIDNewItem,string lpNewItem);

const int MF_POPUP = 0x0010;
const int MF_SEPARATOR = 0x0800;

将 mainMenu1 菜单添加到系统菜单

private void Form1_Load(object sender, System.EventArgs e)
{
    IntPtr mnuSystem;
    mnuSystem=GetSystemMenu(this.Handle,false);
    AppendMenu(mnuSystem, MF_SEPARATOR, (IntPtr)0, "");

    for(int i= 0;i<this.mainMenu1.MenuItems.Count;i++)
    {
        AppendMenu(mnuSystem,MF_POPUP,this.mainMenu1.MenuItems[i].Handle,this.mainMenu1.MenuItems[i].Text);
    }
}
////////////////////////////////////////////////////////////////
// MSDN Magazine — January 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET on Windows XP. Tab size=3.
//
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using TraceWin;

//////////////////
// This class shows how to add items to the system menu in C#/.NET
//
public class SysMenu : Form
{
    // menu flags (from WinUser.h)
    public enum MenuFlags {
        MF_INSERT = 0x00000000,
        MF_CHANGE = 0x00000080,
        MF_APPEND = 0x00000100,
        MF_DELETE = 0x00000200,
        MF_REMOVE = 0x00001000,
        MF_BYCOMMAND = 0x00000000,
        MF_BYPOSITION = 0x00000400,
        MF_SEPARATOR = 0x00000800,
        MF_ENABLED = 0x00000000,
        MF_GRAYED = 0x00000001,
        MF_DISABLED = 0x00000002,
        MF_UNCHECKED = 0x00000000,
        MF_CHECKED = 0x00000008,
        MF_USECHECKBITMAPS = 0x00000200,
        MF_STRING = 0x00000000,
        MF_BITMAP = 0x00000004,
        MF_OWNERDRAW = 0x00000100,
        MF_POPUP = 0x00000010,
        MF_MENUBARBREAK = 0x00000020,
        MF_MENUBREAK = 0x00000040,
        MF_UNHILITE = 0x00000000,
        MF_HILITE = 0x00000080,
    }

    // WM_SYSCOMAND value from WinUser.h
    const int WM_SYSCOMMAND = 0x0112;

    // Windows API fns imported
    [DllImport("user32.dll")]
    private static extern IntPtr GetSystemMenu(IntPtr hwnd, int bRevert);

    [DllImport("user32.dll")]
    private static extern bool AppendMenu(IntPtr hMenu, MenuFlags uFlags, uint uIDNewItem, String lpNewItem);

    // My new command ID.
    const int IDC_MYCOMMAND = 100;

    public SysMenu()
    {
        this.Text = "Check out the system menu, dude.";
        Debug.Listeners.Add(new TraceWinListener());

        IntPtr hSysMenu = GetSystemMenu(this.Handle, 0);  // get handle to 
                                                        // system menu 
        AppendMenu(hSysMenu, MenuFlags.MF_SEPARATOR, 0, null);
        AppendMenu(hSysMenu,
            MenuFlags.MF_BYCOMMAND|MenuFlags.MF_STRING|MenuFlags.MF_CHECKED,
            IDC_MYCOMMAND, "Do you like interop?");
    }

    [STAThread]
    static void Main()
    {
        Application.Run(new SysMenu());
    }

    protected override void WndProc(ref Message msg)
    {
        if (msg.Msg==WM_SYSCOMMAND) {
            if(msg.WParam.ToInt32() == IDC_MYCOMMAND) {
            Trace.WriteLine("Got IDC_MYCOMMAND");
            MessageBox.Show("Yeah, baby!");
            msg.Result = IntPtr.Zero; // (not really necessary)
            return;
            }
        }
        base.WndProc(ref msg);
    }
}
Contributors: FHL