如何获取 ContextMenuStrip 的位置? [英] How to get the location of ContextMenuStrip?

查看:78
本文介绍了如何获取 ContextMenuStrip 的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 ContextMenuStrip 附加到 Chart 控件.

I have attached a ContextMenuStrip to a Chart control.

如何获取ContextMenuStrip出现时左上角所在的图表控件坐标?

How can I get the chart control coordinates where the top left of the ContextMenuStrip is located when it appears?

这是我想转换为图表坐标的要点:

This is the point I want to translate to chart coordinates:

推荐答案

在 ContextMenuStrip 的 Opening 事件中使用 answer 查询当前鼠标光标位置可能会在打开条带稍微延迟时传递错误的位置.

Using the answer to query the current mouse cursor position in the Opening event of the ContextMenuStrip can deliver the wrong position when opening the strip is delayed a bit.

如果需要真正点击的位置,从控件的属性中去掉ContextMenuStrip,在控件的MouseDown事件中手动打开上下文菜单.您可以在此处访问实际点击位置.

If you need the really clicked position, remove the ContextMenuStrip from the property of the control and manually open the context menu in the MouseDown event of the control. There you have access to the actual click position.

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Right)
    {
        var relativeClickedPosition = e.Location;
        var screenClickedPosition = (sender as Control).PointToScreen(relativeClickedPosition);
        contextMenuStrip1.Show(screenClickedPosition);
    }
}

relativeClickedPosition 是相对于控件的光标位置.

relativeClickedPosition is the cursor position relative to the control.

这篇关于如何获取 ContextMenuStrip 的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆