1

我正在尝试提取 ToolStripMenuItem 在屏幕上呈现时相对于其父级位置的左上角坐标。有一个 Bounds 属性,它有一个 Location 成员,但它返回有关其实际位置的无意义(至少从我的角度来看)信息。

“Click”事件处理程序如下所示:

private void newShapeOption_Click(object sender, EventArgs e)
{
    ToolStripMenuItem item = sender as ToolStripMenuItem;
    Point point = item.Bounds.Location; // returns {X = 0, Y = 2} every time for some reason
    // the rest is unimportant 
}

图像上的红点 (Paint Skillz ;) 显示了我想要使用的确切位置(父级是名为“Window 0”的表单控件 - 它是 MDI 应用程序):

例子

4

2 回答 2

1

上下文菜单不知道您单击的对象上的物理位置。那是OnClick对象本身的事件的范围。因此,如果您希望上下文菜单能够访问单击坐标,则必须挂钩OnClick被单击对象的事件,在某些表单全局变量中捕获单击坐标,然后在ContextMenu_Click事件中使用这些值。

于 2014-11-30T19:04:25.893 回答
0

你可以使用项目宽度,比如这个例子:我想把label1放在菜单项之后

 int w = 0;
 for (int i = 0; i < menuStrip1.Items.Count; i++)
 {
     w += menuStrip1.Items[i].Width;
 }
 label1.Width = (int)(this.Width - w);
 label1.Location = new Point(w, label1.Location.Y);
于 2017-10-01T21:37:37.023 回答