简体   繁体   English

相对于父级的ToolStripMenuItem位置

[英]ToolStripMenuItem location relative to parent

I am trying to extract the top left coordinate of the ToolStripMenuItem's location relative to that of its parent as it is rendered on screen. 我正在尝试提取ToolStripMenuItem的位置相对于其父项的位置的左上坐标,因为它在屏幕上呈现。 There is a Bounds property that has a Location member but it returns nonsensical (at least from my point of view) information regarding its actual location. 有一个具有Location成员的Bounds属性,但它返回有关其实际位置的荒谬(至少从我的角度来看)信息。

The "Click" event-handler looks something like this: “ 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 
}

The red dot (Paint skillz ;) on the image shows the exact position that I'd like to use (parent being the Form control named "Window 0" - it's and MDI app): 图像上的红点(Paint skillz;)显示了我想使用的确切位置(父级是名为“ Window 0”的Form控件-它是MDI应用程序):

例

Context menus have no awareness of the physical location on the object where you clicked. 上下文菜单不了解单击对象的物理位置。 That is the province of the OnClick event of the object itself. 那是对象本身的OnClick事件的省份。 So if you want your Context Menu to have access to the click coordinates, you will have to hook the OnClick event of the object being clicked, capture the coordinates of the click in some form-global variables, and then use those values in your ContextMenu_Click event. 因此,如果您希望上下文菜单可以访问单击坐标,则必须钩住被单击对象的OnClick事件,在某些表单全局变量中捕获单击的坐标,然后在ContextMenu_Click使用这些值事件。

You can use item width, such as this example: I want to put label1 after menu items 您可以使用项目宽度,例如以下示例:我想在菜单项之后放置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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM