winform右键菜单

 1 public partial class Form1 : Form  

 2     {  

 3         ContextMenuStrip cms;  

 4         Bitmap bm ;  

 5         public Form1()  

 6         {  

 7             InitializeComponent();  

 8         }  

 9         private void Form1_MouseDown(object sender, MouseEventArgs e)  

10         {  

11               

12             if (e.Button == MouseButtons.Right)  

13             {  

14                 if (cms.Items.Count > 0)  

15                 {  

16                     cms.Items.Clear();  

17                 }  

18                 //带图表项  

19                 cms.Items.Add("tubiao",bm,new EventHandler(tubiao_Handler));  

20                 cms.Items.Add("mouseright1");  

21                 cms.Items.Add("-");  

22                 cms.Items.Add("mouseright2");  

23                 cms.Items.Add("-");  

24                 cms.Items.Add("mouseright3");  

25             }  

26             else 

27             {  

28                 return;  

29             }  

30             //处理显示位置  

31             int w= Width-e.X;  

32             if (w < cms.Width)  

33             {  

34                 cms.Show(this, e.X - cms.Width, e.Y);  

35             }  

36             else 

37             {  

38                 cms.Show(this,e.X,e.Y);  

39             }  

40  

41         }  

42         public void tubiao_Handler(object sender, EventArgs e)  

43         {  

44             MessageBox.Show("this is tubiao", "tubiao", MessageBoxButtons.YesNo);  

45         }  

46         private void Form1_Load(object sender, EventArgs e)  

47         {  

48             cms = new ContextMenuStrip();  

49             bm = new Bitmap("Resources/Icon1.ico");//图标存放位置  

50         }  

51     }  

 

你可能感兴趣的:(WinForm)