ボタンをクリックして、ボタンのContextMenuStrip
すぐ下に表示したい。PointToScreen
上下の座標を試してみると、画面の左側に表示され続けます。
助言がありますか?
ボタンをクリックして、ボタンのContextMenuStrip
すぐ下に表示したい。PointToScreen
上下の座標を試してみると、画面の左側に表示され続けます。
助言がありますか?
これは古い質問であることは知っていますが、他の人にも役立つと思います。次のコードは、クリックされたボタンのすぐ下にコンテキスト メニューを表示し、ボタンはドロップダウン ボタンのように見えます。
private void Button1_Click(object sender, EventArgs e)
{
Button btnSender = (Button)sender;
Point ptLowerLeft = new Point(0, btnSender.Height);
ptLowerLeft = btnSender.PointToScreen(ptLowerLeft);
ctMenuStrip.Show(ptLowerLeft);
}
私はそれを考え出した:
layoutMenus.Show(Cursor.Position.X, Cursor.Position.Y);
ボタンの下の ContexMenuName、ボタンの右側に配置 (ボタンの下と左に展開):
ContexMenuName.Show(ButtonName, new Point(ButtonName.Width - ContexMenuName.Width, ButtonName.Height));
これが役に立てば幸いです :)
私の知る限り、必要なコードは次のとおりです。
// ボタンの右側
ContextMenuName.Show(ButtonName.Left + ButtonName.Width + this.Left, ButtonName.Top + this.Top);
ボタンの下部に
ContextMenuName.Show(ButtonName.Left + this.Left, ButtonName.Top + ButtonName.Height + this.Top);
ボタンの右下に
ContextMenuName.Show(ButtonName.Left + ButtonName.Width + this.Left, ButtonName.Top + ButtonName.Height + this.Top);
toolstripDropDown があり、toolstripDropDown ボタンをクリックした後、コンテキスト メニューを表示したいと思いました。上記のコメントから、toolStripDropDown_Openining イベントのコードを次のように変更しました。それは正常に動作します。
void toolStripDropDownButton_DropDownOpening(object sender, EventArgs e)
{
ToolStripDropDownButton btnSender = (ToolStripDropDownButton)sender;
Point ptLowerRight = new Point(btnSender.Bounds.Right, btnSender.Bounds.Bottom);
ptLowerRight = PointToScreen(ptLowerRight);
contextMenuStrip.Show(ptLowerRight);
}
コンテキスト メニューを配置するときは、正しい画面座標を渡すようにしてください。親のコントロールの位置に基づいて x、y、座標を使用して、Control.PointToScreen のようなものを使用する必要があります。
次の解決策は、ほとんどの制御に適用できます
contextMenuStrip1.Show(sender,sender.Location)
簡単な方法
contextMenuStrip1.Show(Button1, Button1.PointToClient(Cursor.Position));