我有一個彈出選單。彈出選單有兩列布局。因此,Buttons在兩列中對齊,分別是左列和右列。在它里面,有按鈕。
單擊其中一個按鈕后,它將打開一個下拉選單(使用 創建ContextMenuStrip)。
彈出選單及其內容如下所示:

我可以將下拉選單放在“樣式標記”按鈕的右側寬度上。當我試圖將下拉選單放在“洗掉樣式”按鈕的左側寬度時,就會出現問題。
下圖顯示了示例:
“樣式標記”按鈕右側寬度上的下拉位置可以正常作業。

“洗掉樣式”按鈕左側寬度上的下拉位置是有問題的,因為我真的不知道如何撰寫它的邏輯。
它應該如下所示:

實際上,我已經撰寫了我遇到問題的部分。但是,我不確定這是傳統的還是專業的編碼方式。此外,有時左側下拉選單與“洗掉樣式”按鈕之間會有隨機間隙。
下面顯示了代碼示例:
//--global variable--
int removeStyleStripXPos; //to store xPos of left column dropdown menu
//removeStyleStrip -> the left dropdown menu
//styleTokenStrip -> the right dropdown menu
//--global variable--
//--the mouseUp event handler of "Remove style" button--
private void button13_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
removeStyleStrip.Show(button13, new Point(removeStyleStripXPos, 0));
}
}
//--the mouseUp event handler of "Style token" button--
private void button14_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
styleTokenStrip.Show(button14, new Point(this.button14.DisplayRectangle.Right, 0));
}
}
//--the load event handler of the two column popup menu--
private void TCPopupMenuFull_Load(object sender, EventArgs e)
{
removeStyleStripXPos = -(this.removeStyleStrip.Width);
}
那么,我是否可以知道是否有更好的方法將下拉選單的位置設定到“洗掉樣式”按鈕的左端?
uj5u.com熱心網友回復:
當您不能依賴默認行為時,要相對于控制元件定位 ContextMenuStrip,您需要指定螢屏坐標。
ContextMenuStrip(或 ToolStripDropDown)的Show()方法具有允許基于絕對螢屏坐標或相對于特定控制元件的坐標指定位置的多載,在內部轉換為與ToolStripDropDownDirection引數定義的行為相關的螢屏坐標。
// Position the CMS to the absolute right of the Control
SetCMSPosition(someControl, ToolStripDropDownDirection.Right, true);
// Position the CMS to the left of the Control
SetCMSPosition(someControl, ToolStripDropDownDirection.Left, false);
// [...]
private void SetCMSPosition(Control control, ToolStripDropDownDirection direction, bool absolute)
{
Point location = Point.Empty;
if (absolute) {
switch (direction) {
case ToolStripDropDownDirection.Right:
case ToolStripDropDownDirection.AboveRight:
location = new Point(control.ClientSize.Width, 0);
break;
case ToolStripDropDownDirection.BelowRight:
location = new Point(control.ClientSize.Width, control.Height);
break;
}
}
[The ContextMenuStrip].Show(control, location, direction);
}
但您也可以指定 ContextMenuStrip 應出現的螢屏坐標,自行計算位置。例如,在控制元件的左側或右側:(
最好像以前一樣使用列舉器,而不是布林值)
// Positions the CMS to the right of a Control
SetCMSPosition2(someControl, true);
// Positions the CMS to the left of a Control
SetCMSPosition2(someControl, false);
private void SetCMSPosition2(Control control, bool toTheRight)
{
Point location = control.PointToScreen(Point.Empty);
if (toTheRight) {
location.X = control.ClientSize.Width 1;
}
else {
location.X -= [The ContextMenuStrip].Width 1;
}
[The ContextMenuStrip].Show(location);
}
請注意,在這種情況下,嵌套下拉控制元件不會自動補償。
uj5u.com熱心網友回復:
感謝您的時間和回答,@Jimi。我非常感謝您的所有幫助。
他給我分享了一個答案,效果很好。事實證明,我只需要調整打開's的按鈕的MouseUp事件處理程式下的邏輯。button13Remove styleContextMenuStrip
代碼:
//--the mouseUp event handler of "Remove style" button--
private void button13_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var screenPos = button13.PointToScreen(Point.Empty);
removeStyleStrip.Show(new Point(screenPos.X - removeStyleStrip.Width, screenPos.Y));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/450333.html
