public class Form1 : System.Windows.Forms.Form
{
private ContextMenu myContextMenu;
private MenuItem menuFile,menuOpen,menuClose,menuChecked,menuGray,menuBar;
private void InitializeComponent()
{
//初始化ContextMenu
myContextMenu=new ContextMenu();
menuFile=new MenuItem("文件");
menuOpen=new MenuItem("打开...",new EventHandler(menuOpen_Click),Shortcut.CtrlO );
menuClose=new MenuItem("关闭",new EventHandler(menuClose_Click),Shortcut.CtrlX);
menuChecked=new MenuItem("选中的菜单");
menuChecked.Checked=true;
menuGray=new MenuItem("禁用的菜单");
menuGray.Enabled=false;
menuBar=new MenuItem("-");
//向ContextMenu里加入菜单
myContextMenu.MenuItems.Add(menuFile);
myContextMenu.MenuItems.Add(menuChecked);
myContextMenu.MenuItems.Add(menuOpen);
myContextMenu.MenuItems.Add(menuBar);
myContextMenu.MenuItems.Add(menuClose);
myContextMenu.MenuItems.Add(menuGray);
//设定本窗体的ContextMenu
//设置窗体关联的快捷菜单
this.ContextMenu=myContextMenu;
//设置窗体工作区域大小
this.ClientSize=new System.Drawing.Size(400,200);
this.StartPosition=FormStartPosition.CenterScreen;
private void menuOpen_Click(object sender,System.EventArgs e)
{
OpenFileDialog OpenFile=new OpenFileDialog();
OpenFile.ShowDialog();
}
private void menuClose_Click(object sender,System.EventArgs e)
{
Application.Exit();
}