using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Sysmenu
{
public partial class Form1 : Form
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct MENUITEMINFO
{
public uint cbSize;
public uint fMask;
public uint fType;
public uint fState;
public uint wID;
public IntPtr hSubMenu;
public IntPtr hbmpChecked;
public IntPtr hbmpUnchecked;
public IntPtr dwItemData;
public string dwTypeData;
public uint cch;
public IntPtr hbmpItem;
}
[DllImport("user32", ExactSpelling = true)]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32", CharSet = CharSet.Auto)]
private static extern bool InsertMenuItem(IntPtr hMenu, uint uItem, bool fByPosition, ref MENUITEMINFO lpmii);
private const uint MenuID = 0x1111;
public Form1()
{
InitializeComponent();
}
protected override void OnCreateControl()
{
base.OnCreateControl();
IntPtr hSysMenu = GetSystemMenu(this.Handle, false);
MENUITEMINFO item = new MENUITEMINFO();
item.cbSize = (uint)Marshal.SizeOf(item);
item.fMask = 0x42; // MIIM_STRING | MIIM_ID
item.wID = MenuID;
item.dwTypeData = "新しく追加した項目";
InsertMenuItem(hSysMenu, 0, false, ref item);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0112) // WM_SYSCOMMAND
{
if ((m.WParam.ToInt32() & 0xfffF) == MenuID)
{
MessageBox.Show("新しく追加した項目が選択されました。");
}
}
}
}
}