16

ボタンをクリックして、ボタンのContextMenuStripすぐ下に表示したい。PointToScreen上下の座標を試してみると、画面の左側に表示され続けます。

助言がありますか?

4

10 に答える 10

41

これは古い質問であることは知っていますが、他の人にも役立つと思います。次のコードは、クリックされたボタンのすぐ下にコンテキスト メニューを表示し、ボタンはドロップダウン ボタンのように見えます。

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);
}
于 2011-11-28T22:35:32.893 に答える
29

私はそれを考え出した:

layoutMenus.Show(Cursor.Position.X, Cursor.Position.Y);
于 2009-10-11T04:31:26.757 に答える
6

ボタンの下の ContexMenuName、ボタンの右側に配置 (ボタンの下と左に展開): ContexMenuName.Show(ButtonName, new Point(ButtonName.Width - ContexMenuName.Width, ButtonName.Height)); これが役に立てば幸いです :)

于 2015-03-31T20:38:53.643 に答える
3

私の知る限り、必要なコードは次のとおりです。

// ボタンの右側

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);
于 2013-07-30T12:41:15.157 に答える
1

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);
    }
于 2014-03-31T13:10:21.677 に答える
1

コンテキスト メニューを配置するときは、正しい画面座標を渡すようにしてください。親のコントロールの位置に基づいて x、y、座標を使用して、Control.PointToScreen のようなものを使用する必要があります。

于 2009-10-11T04:15:42.293 に答える
0

次の解決策は、ほとんどの制御に適用できます

contextMenuStrip1.Show(sender,sender.Location)
于 2021-05-31T04:48:18.010 に答える
-1

簡単な方法

contextMenuStrip1.Show(Button1, Button1.PointToClient(Cursor.Position));

于 2013-03-11T16:53:42.517 に答える