I found a thread on MSDN that shows how to add an item to the context menu of a winform title bar. Unfortunately it does not show how to register an event with the custom menu item and I have been unable to figure out how to do it. Below is a sample app that can be copied and pasted into a new Windows Forms Application. I would appreciate it if someone could complete the sample for me. Thanks
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
IntPtr hMenu = GetSystemMenu(Handle, false);
if (hMenu != IntPtr.Zero)
{
var menuInfo = new MENUITEMINFO
{
cbSize = (uint) Marshal.SizeOf(typeof (MENUITEMINFO)),
cch = 255,
dwTypeData = "Test Item",
fMask = 0x1 | 0x2 | 0x10,
fState = 0,
fType = 0x0
};
InsertMenuItem(hMenu, 0, true, ref menuInfo);
DrawMenuBar(Handle);
}
}
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
static extern bool DrawMenuBar(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool InsertMenuItem(IntPtr hMenu, uint uItem,
bool fByPosition, [In] ref MENUITEMINFO lpmii);
[StructLayout(LayoutKind.Sequential)]
public 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;
}
}
}
You must override WndProc Method and intercept the id of your new menu.
try this
For a separator just add:
and in Form_load:
I went ahead and just added the necessary elements to the sample code to register the WndProc. This answers the basic question of registering the WndProc without altering the code as much as the previous solution. (It keeps the added menu on top of the system menu).
}