现在的位置: 首页 > 综合 > 正文

点击按钮显示上下文菜单或者自定义控件

2012年01月27日 ⁄ 综合 ⁄ 共 1565字 ⁄ 字号 评论关闭
        private void button1_Click(object sender, EventArgs e) {
            Form2 form = new Form2();

            Point leftBottom = this.PointToScreen(new Point(button1.Left, button1.Bottom));
            int left = leftBottom.X;
            int bottom = leftBottom.Y;

            Point rightTop = this.PointToScreen(new Point(button1.Right, button1.Top));
            int right = rightTop.X;
            int top = rightTop.Y;

            Point location = new Point();
            if (left + form.Width > Screen.PrimaryScreen.Bounds.Width) {
                location.X = right - form.Width;
            } else {
                location.X = left;
            }
            if (bottom + form.Height > Screen.PrimaryScreen.Bounds.Height) {
                location.Y = top - form.Height;
            } else {
                location.Y = bottom;
            }

            form.Location = location;
            form.Show();

        }

        private void button2_Click(object sender, EventArgs e) {
            Point leftBottom = this.PointToScreen(new Point(button2.Left, button2.Bottom));
            int left = leftBottom.X;
            int bottom = leftBottom.Y;

            Point rightTop = this.PointToScreen(new Point(button2.Right, button2.Top));
            int right = rightTop.X;
            int top = rightTop.Y;

            Point location = new Point();
            if (left + contextMenuStrip1.Width > Screen.PrimaryScreen.Bounds.Width) {
                location.X = right - contextMenuStrip1.Width;
            } else {
                location.X = left;
            }
            if (bottom + contextMenuStrip1.Height > Screen.PrimaryScreen.Bounds.Height) {
                location.Y = top - contextMenuStrip1.Height;
            } else {
                location.Y = bottom;
            }

            contextMenuStrip1.Show(location);
        }

抱歉!评论已关闭.