C#(Win)Button实现下拉菜单

在项目中,要用到按钮实现下拉菜单的功能,而且是在MDI窗体中。当菜单的显示范围超出MDI窗体的工作区时,就要换另一显示方式,不至于显示混乱。如图:

(发现一问题,如果把Form1拉到像Form3的大小,还会出现图一的情况。客户没这么邪吧


实现思路:
    1、要把ContextMenuStrip控件实例与按钮关联
    2、取得MDI工作区的大小
    3、取消按钮的右击事件(因为与ContextMenuStrip相关系的控件右键都会响应且显示)
    4、鼠标单击时设置菜单显示位置

实现步骤:
    1、创建用户控件,且用户控件继承自Button类
    2、定义ContextMenuStrip对象
    3、定义显示ContextMenuStrip对象坐标point
    4、重写按钮单击事件和ContextMenuStrip属性(设置与之关联的ContextMenuStrip实例用到),还有重写鼠标右击
        事件,使其不响应任何操作

/// <summary>

  2    /// 说明: 使用此Button时要设置ContextMenuStrip属性值

  3    ///      单击些Button的Click事件要传入所在工作区的宽高

  4    ///      如果没有所需的属性值,则如平时所使用的Button一至

  5    /// 使用例子:

  6    ///      DropButton.WorkSizeX = this.MdiParent.ClientRectangle.Width;

  7    ///      DropButton.WorkSizeY = this.MdiParent.ClientRectangle.Height;

  8    /// 应用:

  9    /// 创建人: lrj

10    /// 创建日期:2008-05-22

11    /// 修改人:

12    /// 修改日期:

13    /// </summary>

14    public partial class DropButton : Button

15    {

16        private ContextMenuStrip contextMenuStrip;

17        private Point point;    //坐标

18        private int x = 0;    //坐标x

19        private int y = 0;    //坐标y

20        private int workSize_x;//工作区x 

21        private int workSize_y;//工作区y

22

23        public DropButton()

24        {

25            InitializeComponent();

26            x = this.Size.Width ;

27            y = 0;

28        }

29        /// <summary>

30        /// 工作区的完

31        /// </summary>

32        public int WorkSizeX

33        {

34            get { return workSize_x; }

35            set { workSize_x = value; }

36        }

37        /// <summary>

38        /// 工作区的高

39        /// </summary>

40        public int WorkSizeY

41        {

42            get { return workSize_y; }

43            set { workSize_y = value - 55; }

44        }

45

46        /// <summary>

47        /// ContextMenuStrip菜单

48        /// </summary>

49        public override ContextMenuStrip ContextMenuStrip

50        {

51            get { return contextMenuStrip; }

52            set

53            {

54                if (contextMenuStrip != null)

55                {

56                    contextMenuStrip = value;

57                }

58            }

59        }

60

61        //

62        //重写的单击事件

63        //

64        protected override void OnClick(EventArgs e)

65        {

66            base.OnClick(e);

67            //菜单在工作区离边框的宽高

68            int _x = this.Parent.Location.X + this.Location.X + this.Size.Width + contextMenuStrip.Size.Width;

69            int _y = this.Parent.Location.Y + this.Location.Y  + contextMenuStrip.Size.Height ;

70

71            if (_x < WorkSizeX - 8)

72            {

73                x = this.Size.Width;

74            }

75            else

76            {

77                x = 0 - contextMenuStrip.Size.Width;

78            }

79

80            if (_y < WorkSizeY)

81            {

82                y = 0;

83            }

84            else

85            {

86                y = 0 - contextMenuStrip.Size.Height + this.Size.Height;

87            }

88

89            point = new Point(x, y);

90            contextMenuStrip.Show(this, point);

91        }

92

93        //

94        //使鼠标右键失效

95        //

96        protected override void OnMouseDown(MouseEventArgs mevent)

97        {

98            base.OnMouseDown(mevent);

99            if (mevent.Button.ToString() != "Right")

100            {

101

102            }

103        }

104    }


原文出处:http://www.cnblogs.com/RongCha-040/