ContextMenuStrip在指定的Control上方绘制不正确

时间:2014-06-12 11:39:31

标签: c# .net winforms contextmenustrip

我正在尝试为我的应用程序创建一个专门的工具栏,它包含几个按钮,点击它们时应该在它们上方显示ContextMenuStrip并有几个选项。似乎我无法将此菜单绘制在正确的位置。

下面是一个重现问题的SSCCE,下面是一些显示问题的图片。

有关如何解决此问题的任何想法?

enter image description here

Form1.cs的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ContextMenuTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.testToolbar1 = new ContextMenuTest.TestToolbar();
            this.SuspendLayout();
            // 
            // testToolbar1
            // 
            this.testToolbar1.Location = new System.Drawing.Point(12, 12);
            this.testToolbar1.Name = "testToolbar1";
            this.testToolbar1.Size = new System.Drawing.Size(321, 23);
            this.testToolbar1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(352, 51);
            this.Controls.Add(this.testToolbar1);
            this.IsMdiContainer = true;
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private TestToolbar testToolbar1;
    }
}

TestToolbar.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ContextMenuTest
{
    public partial class TestToolbar : UserControl
    {
        public TestToolbar()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            curType = contectType.One;
            fromControl = button1;
            showContextStrip();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            curType = contectType.Two;
            fromControl = button2;
            showContextStrip();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            curType = contectType.Three;
            fromControl = button3;
            showContextStrip();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            curType = contectType.Four;
            fromControl = button4;
            showContextStrip();
        }

        contectType curType;
        Control fromControl;
        enum contectType { One, Two, Three, Four }

        private void showContextStrip()
        {
            int numShow = 0;
            switch (curType)
            {
                case contectType.One:
                    {
                        numShow = 1;
                    } break;
                case contectType.Two:
                    {
                        numShow = 2;
                    } break;
                case contectType.Three:
                    {
                        numShow = 3;
                    } break;
                case contectType.Four:
                    {
                        numShow = 4;
                    } break;
            }

            contextMenuStrip1.Items.Clear();

            for (int i = 0; i < numShow; i++)
            {
                ToolStripMenuItem newItem = new ToolStripMenuItem();
                newItem.Text = i.ToString();
                contextMenuStrip1.Items.Add(newItem);
            }

            Console.Out.WriteLine("1Pos: " + button1.Location);
            Console.Out.WriteLine("2Pos: " + button2.Location);
            Console.Out.WriteLine("3Pos: " + button3.Location);
            Console.Out.WriteLine("4Pos: " + button4.Location);
            Console.Out.WriteLine("Ctrl: " + fromControl.Location);

            contextMenuStrip1.Show(fromControl, fromControl.Location, ToolStripDropDownDirection.AboveRight);
        }

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(3, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(84, 0);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(165, 0);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 2;
            this.button3.Text = "button3";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(246, 0);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 3;
            this.button4.Text = "button4";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            // 
            // TestToolbar
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "TestToolbar";
            this.Size = new System.Drawing.Size(321, 23);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
    }
}

1 个答案:

答案 0 :(得分:1)

啊,看来我的英语知识让我误入歧途。

  

将ToolStripDropDown relative 定位到指定的控件位置   指定的位置和指定的方向相对于   父控制。 (继承自ToolStripDropDown。)

此:

contextMenuStrip1.Show(fromControl, fromControl.Location, ToolStripDropDownDirection.AboveRight);

应该是这样的:

Point origin = new Point(0, 0);
contextMenuStrip1.Show(fromControl, origin, ToolStripDropDownDirection.AboveRight);