我正在尝试提取ToolStripMenuItem在其屏幕上呈现时相对于其父级位置的左上角坐标。有一个Bounds属性有一个Location成员,但它返回有关其实际位置的无意义(至少从我的观点来看)信息。
"点击" event-handler看起来像这样:
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;)显示了我想要使用的确切位置(父级是名为&#34的Form控件;窗口0" - 它' s和MDI app):
答案 0 :(得分:1)
上下文菜单无法识别您单击的对象上的物理位置。这是对象本身的OnClick
事件的省。因此,如果您希望上下文菜单可以访问点击坐标,则必须挂钩被点击对象的OnClick
事件,捕获某些表单全局变量中的点击坐标,然后使用这些ContextMenu_Click
活动中的值。
答案 1 :(得分: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);