【发布时间】:2010-12-06 04:28:38
【问题描述】:
我在 MSDN 上找到了 a thread,它展示了如何将项目添加到 Windows 窗体标题栏的上下文菜单中。
不幸的是,它没有显示如何使用自定义菜单项注册事件,我一直无法弄清楚如何去做。下面是一个示例应用程序,可以将其复制并粘贴到新的 Windows 窗体应用程序中。如何完成示例?
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;
}
}
}
【问题讨论】:
标签: c# winforms winapi winforms-interop