由于 PointToScreen,ContextMenuStrip 位置不好
ContextMenuStrip location is not good because of PointToScreen
我在 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
}
使用包含控件的方法,调整该控件的高度,使菜单显示在其下方:
x.Show(SelectContentGroup, new Point(0, SelectContentGroup.Height));
我假设 SelectContentGroup 是按钮的名称。
我在 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
}
使用包含控件的方法,调整该控件的高度,使菜单显示在其下方:
x.Show(SelectContentGroup, new Point(0, SelectContentGroup.Height));
我假设 SelectContentGroup 是按钮的名称。