自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

分享如何用C# Button實(shí)現(xiàn)下拉菜單

開發(fā) 后端
C# Button實(shí)現(xiàn)下拉菜單為實(shí)現(xiàn)這個功能, 花費(fèi)的時間太長了, 但是總結(jié)了很多的經(jīng)驗(yàn),拿出來和大家一起分享,希望大家能夠喜歡。

本文為你講解了C# Button下拉菜單實(shí)現(xiàn)的思路,步驟及代碼!筆者講述的很清楚,很有條理,實(shí)用性很強(qiáng)的。主要的思路還是在于要把ContextMenuStrip控件實(shí)例與按鈕關(guān)聯(lián),并取消按鈕的右擊事件。

C# Button實(shí)現(xiàn)下拉菜單為實(shí)現(xiàn)這個功能, 花費(fèi)的時間太長了, 覺得本人真夠笨. 回過頭來看, 其實(shí)很簡單的東西!

在項(xiàng)目中,要用到按鈕實(shí)現(xiàn)下拉菜單的功能,而且是在MDI窗體中。當(dāng)菜單的顯示范疇超出MDI窗體的工做區(qū)時,就要換另一顯示方式,不至于顯示混亂。如圖:

實(shí)現(xiàn)C# Button下拉菜單
實(shí)現(xiàn)C# Button下拉菜單

實(shí)現(xiàn)C# Button下拉菜單
實(shí)現(xiàn)C# Button下拉菜單

(發(fā)覺一問題,如果把Form1拉到像Form3的大小,還會出現(xiàn)圖一的情況??蛻魶]這么邪吧)

C# Button下拉菜單實(shí)現(xiàn)思路:

1、要把ContextMenuStrip控件實(shí)例與按鈕關(guān)聯(lián)

2、取得MDI工做區(qū)的大小

3、取消按鈕的右擊事件(因?yàn)榕cContextMenuStrip相關(guān)系的控件右鍵都會響應(yīng)且顯示)

4、鼠標(biāo)單擊時設(shè)置菜單顯示位置

C# Button下拉菜單實(shí)現(xiàn)步驟:

1、創(chuàng)建用戶控件,且用戶控件承繼自Button類

2、定義ContextMenuStrip對象

3、定義顯示ContextMenuStrip對象立標(biāo)point

4、重寫按鈕單擊事件和ContextMenuStrip屬性(設(shè)置與之關(guān)聯(lián)的ContextMenuStrip實(shí)例用到),還有重寫鼠標(biāo)右擊事件,使其不響應(yīng)任何操做

C# Button下拉菜單代碼:

  1.    ///   
  2.        /// 說明: 使用此Button時要設(shè)置ContextMenuStrip屬性值  
  3.       ///       單擊些Button的Click事件要傳入所在工做區(qū)的寬高  
  4.        ///       如果沒有所需的屬性值,則如平時所使用的Button一至  
  5.        /// 使用例子:  
  6.       ///       DropButton.WorkSizeX = 
    this.MdiParent.ClientRectangle.Width;
     
  7.        ///       DropButton.WorkSizeY = 
    this.MdiParent.ClientRectangle.Height;
     
  8.        /// 應(yīng)用:  
  9.       /// 創(chuàng)建人: lrj  
  10.     /// 創(chuàng)建日期:2008-05-22  
  11.      /// 修改人:  
  12.      /// 修改日期:  
  13.      /// 
  14.  
  15.      public partial class DropButton : Button  
  16.      {  
  17.          private ContextMenuStrip contextMenuStrip;  
  18.          private Point point;     //立標(biāo)  
  19.          private int x = 0;     //立標(biāo)x  
  20.          private int y = 0;     //立標(biāo)y  
  21.          private int workSize_x;//工做區(qū)x    
  22.          private int workSize_y;//工做區(qū)y  
  23.          public DropButton()  
  24.         {  
  25.              InitializeComponent();  
  26.             x = this.Size.Width ;  
  27.              y = 0;  
  28.          }  
  29.          ///   
  30.          /// 工做區(qū)的完  
  31.          /// 
  32.  
  33.          public int WorkSizeX  
  34.          {  
  35.              get { return workSize_x; }  
  36.              set { workSize_x = value; }  
  37.          }  
  38.          ///   
  39.          /// 工做區(qū)的高  
  40.          /// 
  41.  
  42.          public int WorkSizeY  
  43.          {  
  44.              get { return workSize_y; }  
  45.              set { workSize_y = value - 55; }  
  46.          }  
  47.     ///
  48.             
  49.          /// ContextMenuStrip菜單  
  50.          /// 
  51.  
  52.          public override ContextMenuStrip ContextMenuStrip  
  53.          {  
  54.              get { return contextMenuStrip; }  
  55.              set   
  56.              {  
  57.                  if (contextMenuStrip != null)  
  58.                  {  
  59.                      contextMenuStrip = value;  
  60.                  }  
  61.              }  
  62.         }   
  63.          //  
  64.          //重寫的單擊事件  
  65.          //  
  66.          protected override void OnClick(EventArgs e)  
  67.          {  
  68.              base.OnClick(e);  
  69.              //菜單在工做區(qū)離邊框的寬高  
  70.              int _x = this.Parent.Location.X + this.Location.X +
     
    this.Size.Width + contextMenuStrip.Size.Width;  
  71.              int _y = this.Parent.Location.Y + this.Location.Y  + 
    contextMenuStrip.Size.Height ;  
  72.     if
  73.              (_x < WorkSizeX - 8)  
  74.              {  
  75.                  x = this.Size.Width;  
  76.              }  
  77.              else 
  78.              {  
  79.                  x = 0 - contextMenuStrip.Size.Width;  
  80.              }  
  81.     if 
  82.              (_y < WorkSizeY)  
  83.              {  
  84.                  y = 0;  
  85.              }  
  86.              else 
  87.              {  
  88.                  y = 0 - contextMenuStrip.Size.Height + this.Size.Height;  
  89.              }  
  90.      point =
  91.             new Point(x, y);  
  92.              contextMenuStrip.Show(this, point);  
  93.          }  
  94.          //  
  95.          //使鼠標(biāo)右鍵失效  
  96.          //  
  97.          protected override void OnMouseDown(MouseEventArgs mevent)  
  98.         {  
  99.             base.OnMouseDown(mevent);  
  100.              if (mevent.Button.ToString() != "Right")  
  101.              {  
  102.             }  
  103.          }  
  104.      } 
以上講述了實(shí)現(xiàn)C# Button下拉菜單的思路、步驟及代碼,希望能給大家?guī)韼椭?

【編輯推薦】

  1. 淺析Silvelight中顯示多重數(shù)據(jù)模型集合
  2. C# XML解析方式實(shí)例解析
  3. 簡述C# XML解析方法的特點(diǎn)及應(yīng)用
  4. 有關(guān)事件與委托關(guān)系的思考
  5. .NET對象的XML序列化和反序列化實(shí)例詳解
責(zé)任編輯:阡陌 來源: 編程教程
相關(guān)推薦

2009-09-10 17:48:05

C# button

2013-03-18 10:01:34

jQueryJavaScript

2009-09-11 12:52:09

C# WinForm控編輯器

2009-04-02 09:08:00

下拉菜單腳本導(dǎo)航設(shè)計CSS

2020-07-20 12:31:33

UI下拉工菜單設(shè)計

2024-09-23 00:00:00

下拉菜單UI控件

2009-09-10 18:06:25

C# button快捷

2009-07-02 14:18:13

JSP JavaScr下拉菜單

2015-03-23 18:11:39

UITableViewswift下拉刷新

2009-09-11 09:59:47

2009-08-24 15:58:00

Visual C#生成

2009-08-18 11:17:37

C#添加鼠標(biāo)右鍵

2009-09-10 18:18:42

C# Button

2009-08-20 16:07:39

C#和ADO.NET訪

2009-08-17 15:34:58

C#創(chuàng)建XML

2009-08-19 14:29:33

C#代理

2015-08-05 09:30:32

C#下拉式計算器

2025-03-04 04:00:00

C++代碼windows

2009-01-19 10:03:58

C#XML動態(tài)分層菜單

2009-08-26 15:09:57

C# Hook
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號