【问题标题】:ContextMenuStrip location is not good because of PointToScreenContextMenuStrip 位置不好,因为 PointToScreen
【发布时间】:2019-10-03 05:50:57
【问题描述】:

我在 panel2 上的 SplitContainer 中有一个 Button。 我已经动态创建了一个ContextMenuStrip,并将其附加到此按钮。我想将上下文菜单定位在此图像中的按钮下方

但我得到的不是那样。

这是我尝试过的:

private void SelectContentGroup_Click(object sender, EventArgs e) {
    ContextMenuStrip x = selectContentGroup.ContextMenuStrip;
    if (x is null) return;
        // this will show contextmenu near the mouse arrow
        //x.Show(Control.MousePosition);

        // I have tried to get MousePosition and to compare to my button location and Y is a lot of difference, about 200 pixels
        //Console.WriteLine("MousePosition: {0}, ButtonLocation: {1}", Control.MousePosition, PointToScreen(selectContentGroup.Location));
        x.Show(PointToScreen(selectContentGroup.Location));

        // I tried with e.Location also, but none of those points will give the button Left-Bottom position for contextmenu
    }

【问题讨论】:

    标签: c# winforms contextmenustrip


    【解决方案1】:

    使用包含该控件的方法,并通过该控件的高度进行调整以使其下方显示菜单:

    x.Show(SelectContentGroup, new Point(0, SelectContentGroup.Height));
    

    我假设 SelectContentGroup 是按钮的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多