0

更新
这个问题是从这里的延续。

我正在为开源文本编辑器开发自定义上下文菜单,上下文菜单是使用Form. 我将上下文菜单类命名为ContextMenu.

下面是一个函数(答案取自上一个问题),负责设置弹出表单的位置。在其中可以找到防止表单打开超出工作屏幕区域的逻辑。

//a function to prevent context menu opening beyond screen area
private Point SetPopupLocation(Screen theScreen, ContextMenu theContextMenu, Point initPosition)
{
    var p = new Point();
    var wrkArea = theScreen.WorkingArea;
    p.X = wrkArea.Width - (initPosition.X + theContextMenu.Width);
    p.Y = wrkArea.Height - (initPosition.Y + theContextMenu.Height);
    p.X = p.X < 0 ? wrkArea.Width - theContextMenu.Width : initPosition.X;
    p.Y = p.Y < 0 ? wrkArea.Height - theContextMenu.Height : initPosition.Y;
    return p;
}

其他一些相关代码,以防万一需要参考:

//the mouseup 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)
    {
        //IsMouseButtonDown(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 
        Point formLocation;

        if (canDisplay)
        {
            //obtain center mouse coordinate to context menu
            Point theMouseCoor = new Point(Cursor.Position.X, Cursor.Position.Y);

            //to process whether custom context menu was opened beyond screen area or not
            formLocation = SetPopupLocation(Screen.FromControl(this), contextMenuObj, (sender as Control).PointToScreen(e.Location));

            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!";
        }
    }
}
//to center the cursor on the context menu
private Point processContextMenuCursorLocation(ContextMenu theContextMenu, Point formLocation)
{
    int midXCoor;
    int midYCoor;

    midXCoor = (theContextMenu.Width / 2); //middle coordinate of width
    midYCoor = (theContextMenu.Height / 2); //middle coordinate of height

    Cursor.Position = new Point(formLocation.X + midXCoor, formLocation.Y + midYCoor);

    return Cursor.Position;
}
//obtain the newly set form location and mouse coordinate,
//then display the context menu
private void displayCustomContextMenu(ContextMenu theContextMenu, Point formLocation, Point cursorLocation)
{
    theContextMenu.Location = formLocation;

    //cursor location
    Cursor.Position = cursorLocation;

    //message: display context menu
    Console.WriteLine("Context menu opens via COMBINATION!");
    theContextMenu.Visible = true;
}

问题是当我尝试调用要在辅助显示屏上显示的上下文菜单时,它只在主显示屏上弹出,而在辅助显示屏上没有。

根据上一个线程给出的答案,我尝试启用称为 DpiAware 或 DpiAwareness 的东西。例如,我编辑了 app.manifest 或 app.config(我编辑的文件的参考可以在这里找到),希望能解决这个问题。但是,它仍然没有解决任何问题。

我不确定的是,DpiAware单独启用是否可以解决此问题,还是我需要添加其他内容?

请注意,与前一个线程给出的答案相比,我所做的代码实现有点不同,因为我是根据程序的需要对其进行编码的。换句话说,这是为了确保从答案中插入的新代码适应程序中的现有代码。为此,我相信它不会改变或影响上一个线程答案中的代码的工作方式。

4

0 回答 0