我正在使用 Forms 構建一個兩列自定義背景關系選單布局。
我將自定義背景關系選單形式的類命名為ContextMenu.
我創建了一個標志函式來檢查它是否在呼叫時超過了設備螢屏尺寸。
僅超出設備螢屏的長度或僅高度的單一情況有效。
但是,當涉及到超出設備螢屏的長度和高度時,它不知何故不起作用。我試圖在控制臺中列印值以進行檢查(示例)。列印的值是正確的,但由于某種原因,它沒有輸入它的switch大小寫。
我錯過了代碼中的任何地方嗎?
下面是標志功能。case 3不起作用(示例)。
//exceed screen flag function
private Point processContextMenuFormLocation(ContextMenu theContextMenu, int screenExceedFlag, Point mouseCoor)
{
//local form coordinate
int formXCoor;
int formYCoor;
switch (screenExceedFlag)
{
case 1: //if exceed right boundary
formXCoor = (mouseCoor.X) - (contextMenuObj.Width); //move context menu to the left
formYCoor = mouseCoor.Y; //no need changes
//after exceedFlag, set where context menu position is
theContextMenu.Location = new Point(formXCoor, formYCoor);
break;
case 2: //if exceed bottom boundary
formXCoor = (mouseCoor.X); //no need changes
formYCoor = (mouseCoor.Y) - (contextMenuObj.Height); //move context menu to the top
theContextMenu.Location = new Point(formXCoor, formYCoor);
break;
case 3: //if exceed right & bottom boundary
formXCoor = (mouseCoor.X) - (contextMenuObj.Width); //move context menu to the left
formYCoor = (mouseCoor.Y) - (contextMenuObj.Height); //move context menu to the top
theContextMenu.Location = new Point(formXCoor, formYCoor);
break;
case -1: //if exceeded nothing
theContextMenu.Location = new Point(mouseCoor.X, mouseCoor.Y);
break;
}
//return the new location of context menu
return theContextMenu.Location;
}
下面是事件處理程式,它呼叫上述函式:
//mouse up event handler
private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
//set default CM closed so that won't open concurrently
richContextStrip.Visible = false;
if (e.Button == MouseButtons.Right)
{
IsKeyUp((int)MouseButtons.Right); //set rmbIsUp = true
//here because the rmb event handler is above this
bool canDisplay = contextMenuDisplayFlag(ctrlIsDown, rmbIsUp); //get ctrl and rmb flag status
if (canDisplay)
{
//to process whether custom context menu was opened beyond screen area or not
exceedScreenFlag = isBeyondScreen(Cursor.Position.X, Cursor.Position.Y, contextMenuObj.Width, contextMenuObj.Height);
//obtain mouse coordinates
Point theMouseCoor = new Point(Cursor.Position.X, Cursor.Position.Y);
//exceed screen flag function
Point formLocation = processContextMenuFormLocation(contextMenuObj, exceedScreenFlag, theMouseCoor);
//center the cursor at context menu
Point cursorLocation = processContextMenuCursorLocation(contextMenuObj, formLocation);
displayCustomContextMenu(contextMenuObj, formLocation, cursorLocation);
toolStripStatusLabel1.Text = "Custom context menu opened!";
}
else //if ctrl key is not pressed
{
richContextStrip.Visible = true;
toolStripStatusLabel1.Text = "Default context menu opened!";
}
}
}
uj5u.com熱心網友回復:
您可能需要考慮當前滑鼠指標位置是由MouseEventArgs.Location. 您只需使用[Control].PointToScreen()方法將此值轉換為螢屏坐標。
然后將初始位置 - 加上您的彈出視窗的和 - 與 Screen.FromControl([Control]).WorkingArea 回傳的值進行Width比較,并驗證彈出視窗是否包含在此范圍內。
如果不是,則從左右邊界中減去彈出視窗和/或。HeightWidthHeightWorkingArea
像這樣:
請注意,這些措施不包括表單的不可見邊框。與螢屏兩側的距離為 14~16 像素。如果您喜歡更緊的位置,請調整它
private void someControl_MouseUp(object sender, MouseEventArgs e)
{
var f = new Form() { Width = 400, Height = 400, StartPosition = FormStartPosition.Manual };
f.Location = SetPopupLocation(Screen.FromControl(this), f, (sender as Control).PointToScreen(e.Location));
f.Show();
}
private Point SetPopupLocation(Screen screen, Form form, Point initPosition)
{
var p = new Point();
var wrkArea = screen.WorkingArea;
p.X = wrkArea.Left wrkArea.Width - (initPosition.X form.Width);
p.Y = wrkArea.Top wrkArea.Height - (initPosition.Y form.Height);
p.X = p.X < 0 ? wrkArea.Width - form.Width : initPosition.X;
p.Y = p.Y < 0 ? wrkArea.Height - form.Height : initPosition.Y;
return p;
}
您的應用需要 DpiAware 才能接收可靠的測量值和坐標。請參閱將SetWindowPos 與多個監視器一起使用
中的說明
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/429718.html
上一篇:如何以更新的順序保存重新排序的DataGridView列?
下一篇:檢測子表單何時關閉
