如何确保表单显示在“附加”页面上在双显示器场景中进行显示器?

发布于 2024-08-27 18:29:56 字数 133 浏览 9 评论 0原文

我有一个应用程序,其中有一个我想在第二个屏幕上显示的表单。

意思是如果应用程序在屏幕 A 上运行,当我单击菜单显示表单时,它应该显示在屏幕 B 上 如果应用程序在屏幕 B 上运行,并且当我单击菜单显示表单时,它应该显示在屏幕 A 上。

I have an application in which there is a form which I want to show on second screen.

Mean If application is running on screen A and when I click on menu to show Form it should display on Screen B
and same with if application is running on screen B and when I click on menu to show Form it should display on Screen A.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逐鹿 2024-09-03 18:29:57

您需要使用屏幕 类来查找原始表单不在的屏幕,然后根据该屏幕的 Bounds 设置第二个表单的 Location 属性。

例如:

var myScreen = Screen.FromControl(originalForm);
var otherScreen = Screen.AllScreens.FirstOrDefault(s => !s.Equals(myScreen)) 
               ?? myScreen;
otherForm.Left = otherScreen.WorkingArea.Left + 120;
otherForm.Top = otherScreen.WorkingArea.Top + 120;

这适用于任意数量的屏幕。

请注意,显卡可能会被配置为使 Windows 看到一个大屏幕而不是两个较小的屏幕,在这种情况下,这会变得更加困难。

You need to use the Screen class to find a screen that the original form is not on, then set the second form's Location property based on that screen's Bounds.

For example:

var myScreen = Screen.FromControl(originalForm);
var otherScreen = Screen.AllScreens.FirstOrDefault(s => !s.Equals(myScreen)) 
               ?? myScreen;
otherForm.Left = otherScreen.WorkingArea.Left + 120;
otherForm.Top = otherScreen.WorkingArea.Top + 120;

This will work for any number of screens.

Note that it is possible that the video card is configured so that Windows sees one large screen instead of two smaller ones, in which case this becomes much more difficult.

北方的韩爷 2024-09-03 18:29:57

下面的函数允许您在任何监视器上显示表单。对于您当前的场景,您可以调用此 showOnMonitor(1);

本质上,您必须从 Screen.AllScreens 获取屏幕信息,然后获取每个屏幕的尺寸,然后将表单放置在您需要的位置

function void showOnMonitor(int showOnMonitor) 
{ 
    Screen[] sc; 
    sc = Screen.AllScreens; 

    Form2 f = new Form2(); 

    f.FormBorderStyle = FormBorderStyle.None; 
    f.Left = sc[showOnMonitor].Bounds.Left; 
    f.Top = sc[showOnMonitor].Bounds.Top; 
    f.StartPosition = FormStartPosition.Manual; 

    f.Show(); 
}

注意不要忘记进行验证以确保您实际上有两个屏幕等否则访问 sc[showOnMonitor] 将会抛出异常

Below is a function allowing you to display a form on any monitor. For your current scenario you can call this showOnMonitor(1);.

Essentially you have to get screen information from Screen.AllScreens and then get the dimensions of each, then place your form where you need it

function void showOnMonitor(int showOnMonitor) 
{ 
    Screen[] sc; 
    sc = Screen.AllScreens; 

    Form2 f = new Form2(); 

    f.FormBorderStyle = FormBorderStyle.None; 
    f.Left = sc[showOnMonitor].Bounds.Left; 
    f.Top = sc[showOnMonitor].Bounds.Top; 
    f.StartPosition = FormStartPosition.Manual; 

    f.Show(); 
}

Note don't forget to do validation to ensure you actually have two screens etc else an exception will be thrown for accessing sc[showOnMonitor]

冬天旳寂寞 2024-09-03 18:29:57

在 OnLoad 方法中更改窗口的位置。

protected void Form1_OnLoad(...) {
    showOnMonitor(1);
}

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

    this.StartPosition = FormStartPosition.Manual; 
    this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);
    // If you intend the form to be maximized, change it to normal then maximized.
    this.WindowState = FormWindowState.Normal;
    this.WindowState = FormWindowState.Maximized;
}

On the OnLoad method change the Location of the window.

protected void Form1_OnLoad(...) {
    showOnMonitor(1);
}

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

    this.StartPosition = FormStartPosition.Manual; 
    this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);
    // If you intend the form to be maximized, change it to normal then maximized.
    this.WindowState = FormWindowState.Normal;
    this.WindowState = FormWindowState.Maximized;
}
_蜘蛛 2024-09-03 18:29:57

我将其用于 XNA 4 双屏应用程序(全屏 XNA 游戏窗口 + WinForm)

在 Form_Load() 方法中,放置以下代码:

var primaryDisplay = Screen.AllScreens.ElementAtOrDefault(0);  
var extendedDisplay = Screen.AllScreens.FirstOrDefault(s => s != primaryDisplay) ?? primaryDisplay;

this.Left = extendedDisplay.WorkingArea.Left + (extendedDisplay.Bounds.Size.Width / 2) - (this.Size.Width / 2);
this.Top = extendedDisplay.WorkingArea.Top + (extendedDisplay.Bounds.Size.Height / 2) - (this.Size.Height / 2);

I used this for an XNA 4 Dual Screen Application (Full Screen XNA Game Window + WinForm)

In the Form_Load() method, place the following code:

var primaryDisplay = Screen.AllScreens.ElementAtOrDefault(0);  
var extendedDisplay = Screen.AllScreens.FirstOrDefault(s => s != primaryDisplay) ?? primaryDisplay;

this.Left = extendedDisplay.WorkingArea.Left + (extendedDisplay.Bounds.Size.Width / 2) - (this.Size.Width / 2);
this.Top = extendedDisplay.WorkingArea.Top + (extendedDisplay.Bounds.Size.Height / 2) - (this.Size.Height / 2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文