简体   繁体   中英

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. 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.

The "Click" event-handler looks something like this:

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):

例

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. 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.

You can use item width, such as this example: I want to put label1 after menu items

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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