1.新建Form窗体,向其中添加ContextMenuStrip控件
2.点击ContextMenuStrip控件属性Items,添加MenuItem或Separator组件
3.点击一级菜单,添加MenuItem或Separator组件,形成二级菜单
4.Demo代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Contextmenu { public partial class frmContextmenu : Form { public frmContextmenu() { InitializeComponent(); } private void tSMICenter_Click(object sender, EventArgs e) { int height =SystemInformation.WorkingArea.Height; int width = SystemInformation.WorkingArea.Width; int formheight = this.Size.Height; int formwidth = this.Size.Width; int newformx = width / 2 - formwidth / 2; int newformy = height / 2 - formheight / 2; this.SetDesktopLocation(newformx, newformy); } private void tSMILargen_Click(object sender, EventArgs e) { this.Width =this.Width+100; this.Height = this.Height + 100; } private void tSMISmaller_Click(object sender, EventArgs e) { this.Width = this.Width - 100; this.Height = this.Height- 100; } private void frmContextmenu_MouseClick(object sender, MouseEventArgs e) { if (e.Button==MouseButtons.Right) { contextMenuStrip.Show(MousePosition.X,MousePosition.Y); } } } }
namespace Contextmenu { partial class frmContextmenu { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.labelX1 = new DevComponents.DotNetBar.LabelX(); this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components); this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.tSMICenter = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem(); this.tSMILargen = new System.Windows.Forms.ToolStripMenuItem(); this.tSMISmaller = new System.Windows.Forms.ToolStripMenuItem(); this.contextMenuStrip.SuspendLayout(); this.SuspendLayout(); // // labelX1 // // // // this.labelX1.BackgroundStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square; this.labelX1.Location = new System.Drawing.Point(186, 100); this.labelX1.Name = "labelX1"; this.labelX1.Size = new System.Drawing.Size(96, 23); this.labelX1.TabIndex = 0; this.labelX1.Text = "展示右键菜单"; // // contextMenuStrip // this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItem1, this.toolStripSeparator1, this.toolStripMenuItem2}); this.contextMenuStrip.Name = "contextMenuStrip"; this.contextMenuStrip.Size = new System.Drawing.Size(149, 54); // // toolStripMenuItem1 // this.toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.tSMICenter}); this.toolStripMenuItem1.Name = "toolStripMenuItem1"; this.toolStripMenuItem1.Size = new System.Drawing.Size(148, 22); this.toolStripMenuItem1.Text = "窗体位置指令"; // // tSMICenter // this.tSMICenter.Name = "tSMICenter"; this.tSMICenter.Size = new System.Drawing.Size(180, 22); this.tSMICenter.Text = "窗体居中"; this.tSMICenter.Click += new System.EventHandler(this.tSMICenter_Click); // // toolStripSeparator1 // this.toolStripSeparator1.Name = "toolStripSeparator1"; this.toolStripSeparator1.Size = new System.Drawing.Size(145, 6); // // toolStripMenuItem2 // this.toolStripMenuItem2.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.tSMILargen, this.tSMISmaller}); this.toolStripMenuItem2.Name = "toolStripMenuItem2"; this.toolStripMenuItem2.Size = new System.Drawing.Size(148, 22); this.toolStripMenuItem2.Text = "窗体大小指令"; // // tSMILargen // this.tSMILargen.Name = "tSMILargen"; this.tSMILargen.Size = new System.Drawing.Size(124, 22); this.tSMILargen.Text = "窗体变大"; this.tSMILargen.Click += new System.EventHandler(this.tSMILargen_Click); // // tSMISmaller // this.tSMISmaller.Name = "tSMISmaller"; this.tSMISmaller.Size = new System.Drawing.Size(124, 22); this.tSMISmaller.Text = "窗体变小"; this.tSMISmaller.Click += new System.EventHandler(this.tSMISmaller_Click); // // frmContextmenu // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(484, 261); this.Controls.Add(this.labelX1); this.MaximizeBox = false; this.Name = "frmContextmenu"; this.Text = "右键菜单"; this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.frmContextmenu_MouseClick); this.contextMenuStrip.ResumeLayout(false); this.ResumeLayout(false); } #endregion private DevComponents.DotNetBar.LabelX labelX1; private System.Windows.Forms.ContextMenuStrip contextMenuStrip; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1; private System.Windows.Forms.ToolStripMenuItem tSMICenter; private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2; private System.Windows.Forms.ToolStripMenuItem tSMILargen; private System.Windows.Forms.ToolStripMenuItem tSMISmaller; } }