选择第二台显示器打开表格

时间:2016-12-18 05:05:10

标签: c#

当使用此代码选择我的第二台显示器时,如果我的鼠标光标位于第一台显示器显示器上,我可以在此方法中添加哪些表格,以禁用第一台显示器上的表格打开?

private void showOnMonitor(int showOnMonitor)
        {
            Screen[] sc;
            sc = Screen.AllScreens;
            if (showOnMonitor >= sc.Length)
            {
                showOnMonitor = 0;
            }

            this.StartPosition = FormStartPosition.CenterParent;
            this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);
            this.WindowState = FormWindowState.Normal;

        }

1 个答案:

答案 0 :(得分:0)

你的问题不明确。假设您想在除主监视器之外的其他监视器上打开该窗体,如果它有鼠标指针。添加了注释,以便您可以调整代码。 将该功能称为

showOnMonitor(5, ConvertMousePointToScreenIndex(System.Windows.Forms.Cursor.Position));

private void showOnMonitor(int showOnMonitor, Point mousePoint)
{
    Screen[] sc;
    System.Drawing.Rectangle rect;
    int mouseOnMonitor = 0;

    for (int i = 1; i <= Screen.AllScreens.Count(); i++)
    {
        rect = Screen.AllScreens[i - 1].Bounds;
        if (rect.Contains(mousePoint))
        {
            mouseOnMonitor = i - 1;
            break;
        }           
    }

    sc = Screen.AllScreens;
    if (showOnMonitor >= sc.Length)
    {
        showOnMonitor = 0;
    }

    if (showOnMonitor == mouseOnMonitor && showOnMonitor == 0)
    {
        //do your logic here if monitor with mouse and showOnMonitor are same i.e. disable the form opening on the monitor with mouse,
        //if such monitor should be always 0, then add the check to if condition showOnMonitor == 0 else remove this check

        //open the form on next monitor 
        //Check if more than 1 monitor available
        if (sc.Length > 1)
        {
            showOnMonitor = showOnMonitor + 1; 
            if (showOnMonitor >= sc.Length)
                showOnMonitor = 1;
        }
        else
        {
            //throw exception as only 1 monitor and form should not be allowed to open on this monitor
        }           
    }
    else
    {
        this.StartPosition = FormStartPosition.CenterParent;
        this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);
        this.WindowState = FormWindowState.Normal;
    }
}