All pastes #11317 Raw Edit

InsertMenuItem

public text v1 · immutable
#11317 ·published 2005-05-09 16:47 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace InsertMenuItem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            IntPtr hmenu = UnsafeNativeMethods.GetSystemMenu(new HandleRef(this, Handle), false);

            NativeMethods.MENUITEMINFO_T newMenuItem = new NativeMethods.MENUITEMINFO_T();
            newMenuItem.fMask = NativeMethods.MIIM_ID | NativeMethods.MIIM_STATE |
                         NativeMethods.MIIM_SUBMENU | NativeMethods.MIIM_TYPE | NativeMethods.MIIM_DATA;
            newMenuItem.fType = 0;
            newMenuItem.fState = 0;
            newMenuItem.wID = myCommandId;
            newMenuItem.hbmpChecked = IntPtr.Zero;
            newMenuItem.hbmpUnchecked = IntPtr.Zero;
            newMenuItem.dwItemData = IntPtr.Zero;
            newMenuItem.dwTypeData = "My Added Menu ...";
            newMenuItem.cch = 0;

            UnsafeNativeMethods.InsertMenuItem(new HandleRef(this, hmenu), 0, true, newMenuItem);


            NativeMethods.MENUITEMINFO_T separatorItem = new NativeMethods.MENUITEMINFO_T();
            separatorItem.fMask = NativeMethods.MIIM_ID | NativeMethods.MIIM_STATE |
                         NativeMethods.MIIM_SUBMENU | NativeMethods.MIIM_TYPE | NativeMethods.MIIM_DATA;
            separatorItem.fType = NativeMethods.MFT_MENUBREAK;
            UnsafeNativeMethods.InsertMenuItem(new HandleRef(this, hmenu), 1, true, separatorItem);
            hmenu = IntPtr.Zero;
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case NativeMethods.WM_SYSCOMMAND:
                    int command = NativeMethods.Util.LOWORD(m.WParam);
                    if (command == myCommandId)
                    {
                        MessageBox.Show("In handler");
                    }
                    break;
            }
            base.WndProc(ref m);
        }

        private const int myCommandId = 0;
    }

    static class NativeMethods
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public class MENUITEMINFO_T
        {
            public int cbSize = Marshal.SizeOf(typeof(MENUITEMINFO_T));
            public int fMask;
            public int fType;
            public int fState;
            public int wID;
            public IntPtr hSubMenu;
            public IntPtr hbmpChecked;
            public IntPtr hbmpUnchecked;
            public IntPtr dwItemData;
            public string dwTypeData;
            public int cch;
        }

        public const int MIIM_STATE = 0x00000001,
                         MIIM_ID = 0x00000002,
                         MIIM_SUBMENU = 0x00000004,
                         MIIM_TYPE = 0x00000010,
                         MIIM_DATA = 0x00000020,
                         MFT_MENUBREAK = 0x00000040,
                         WM_SYSCOMMAND = 0x0112;

        public static class Util
        {
            public static int LOWORD(int n)
            {
                return n & 0xffff;
            }
            public static int LOWORD(IntPtr n)
            {
                return LOWORD(unchecked((int)(long)n));
            }
        }
    }

    static class UnsafeNativeMethods
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public extern static bool InsertMenuItem(HandleRef hMenu, int uItem, bool fByPosition, NativeMethods.MENUITEMINFO_T lpmii);

        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        public static extern IntPtr GetSystemMenu(HandleRef hWnd, bool bRevert);
    }

}