- public partial class Form1 : Form
- {
- ContextMenuStrip cms;
- Bitmap bm ;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_MouseDown(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Right)
- {
- if (cms.Items.Count > 0)
- {
- cms.Items.Clear();
- }
- //带图表项
- cms.Items.Add("tubiao",bm,new EventHandler(tubiao_Handler));
- cms.Items.Add("mouseright1");
- cms.Items.Add("-");
- cms.Items.Add("mouseright2");
- cms.Items.Add("-");
- cms.Items.Add("mouseright3");
- }
- else
- {
- return;
- }
- //处理显示位置
- int w= Width-e.X;
- if (w < cms.Width)
- {
- cms.Show(this, e.X - cms.Width, e.Y);
- }
- else
- {
- cms.Show(this,e.X,e.Y);
- }
- }
- public void tubiao_Handler(object sender, EventArgs e)
- {
- MessageBox.Show("this is tubiao", "tubiao", MessageBoxButtons.YesNo);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- cms = new ContextMenuStrip();
- bm = new Bitmap("Resources/Icon1.ico");//图标存放位置
- }
- }
转载于:https://blog.51cto.com/congcong/1068095