C#-----Winform界⾯实现右键菜单功能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()
{
thisponents = new System.ComponentModel.Container();
this.labelX1 = new DevComponents.DotNetBar.LabelX();
this.tSMICenter = new System.Windows.Forms.ToolStripMenuItem();
this.tSMILargen = new System.Windows.Forms.ToolStripMenuItem();
this.tSMISmaller = new System.Windows.Forms.ToolStripMenuItem();
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);
menustrip和toolstripthis.labelX1.TabIndex = 0;
this.labelX1.Text = "展⽰右键菜单";
//
// contextMenuStrip
//
//
// toolStripMenuItem1
//
//
// 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
//
/
/
// toolStripMenuItem2
//
this.tSMISmaller});
//
// 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.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;
}
}