C# 如何在当前屏幕上显示表单?

发布于 2024-09-06 05:21:44 字数 185 浏览 9 评论 0 原文

我想在调用它的同一窗口中显示一个新表单。 我知道一种通过类似于下面的代码在主屏幕或虚拟屏幕上显示此表单的方法:

MyForm.Location = Screen.PrimaryScreen.Bounds.Location;

但我想在当前屏幕上显示它。 有没有办法找出并在当前屏幕上显示它?

I want to show a new form in the same window from where it was invoked.
I know a way to show this form on PrimaryScreen or Virtual Screen by code similar to as below:

MyForm.Location = Screen.PrimaryScreen.Bounds.Location;

But i want to show it on current screen.
Is there a way to find out and show it on current screen?

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

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

发布评论

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

评论(7

我不咬妳我踢妳 2024-09-13 05:21:44

我做了这样的事情来显示我的表单以当前屏幕为中心:

var screen = Screen.FromPoint(Cursor.Position);
myForm.StartPosition = FormStartPosition.Manual;
myForm.Left = screen.Bounds.Left + screen.Bounds.Width / 2 - myForm.Width / 2;
myForm.Top = screen.Bounds.Top + screen.Bounds.Height / 2 - myForm.Height / 2;

I have done something like this to show my form centered to the current screen:

var screen = Screen.FromPoint(Cursor.Position);
myForm.StartPosition = FormStartPosition.Manual;
myForm.Left = screen.Bounds.Left + screen.Bounds.Width / 2 - myForm.Width / 2;
myForm.Top = screen.Bounds.Top + screen.Bounds.Height / 2 - myForm.Height / 2;
对你而言 2024-09-13 05:21:44

您可以使用相同的技术,但不使用 PrimaryScreen,而是使用 Screen.FromPoint光标位置

Screen screen = Screen.FromPoint(Cursor.Position);
MyForm.Location = screen.Bounds.Location;

You can use the same technique, but instead of using the PrimaryScreen, grab the screen using Screen.FromPoint and Cursor.Position:

Screen screen = Screen.FromPoint(Cursor.Position);
MyForm.Location = screen.Bounds.Location;
呆萌少年 2024-09-13 05:21:44

听起来您没有将 StartPosition 设置为手动。

It sounds like you aren't setting the StartPosition to Manual.

诗笺 2024-09-13 05:21:44

如果您已经有一个父窗体并希望在同一屏幕上打开一个新窗体,请为 ShowDialog 方法提供对父窗体的引用: newForm.ShowDialog(this); 不带所有者参数 ("< code>this") 即使您的父表单位于另一个屏幕上,新表单也可能在主屏幕上打开。

If you already have a parent form and want to open a new form on the same screen, give the ShowDialog method a reference to the parent form: newForm.ShowDialog(this); Without owner parameter ("this") the new form may open on the main screen even when your parent form is on another screen.

半透明的墙 2024-09-13 05:21:44
  1. 单击设计模式下的表单。
  2. 将 StartPosition 属性更改为 CenterScreen 。

这应该会在活动屏幕上打开表单。参考
StartPosition 的更多值。

  1. Click on the Form in design mode.
  2. Change the StartPosition property to CenterScreen .

This should open up the form on the active screen. Refer
this for more values of StartPosition.

甚是思念 2024-09-13 05:21:44

我知道这已经晚了,但仍然发布我的答案希望对其他人有所帮助。经过多次尝试,我用 3 台显示器完成了这项工作

var currentScreen = Screen.FromControl(this);
        if (!currentScreen.Primary)
        {
            var hCenter = currentScreen.Bounds.Left + (((currentScreen.Bounds.Right - currentScreen.Bounds.Left) / 2) - ((Width) / 2));

            var vCenter = (currentScreen.Bounds.Bottom / 2) - ((Height) / 2);
            StartPosition = FormStartPosition.Manual;
            Location = new Point(hCenter, vCenter);
        }
        else
        {
            CenterToScreen();
        }

I know this is late, but still, post my answer hope that it will help someone else. After several tries, I got this work with 3 monitors

var currentScreen = Screen.FromControl(this);
        if (!currentScreen.Primary)
        {
            var hCenter = currentScreen.Bounds.Left + (((currentScreen.Bounds.Right - currentScreen.Bounds.Left) / 2) - ((Width) / 2));

            var vCenter = (currentScreen.Bounds.Bottom / 2) - ((Height) / 2);
            StartPosition = FormStartPosition.Manual;
            Location = new Point(hCenter, vCenter);
        }
        else
        {
            CenterToScreen();
        }
煞人兵器 2024-09-13 05:21:44

许多年后,但没有任何项目被标记为适当的答案,我结合了两条评论以使其发挥作用(即来自 Reed Copsey 和 Jason)。
我没有尝试过所描述的任何其他方法,因为这没有问题,并且按照预期在我的光标所在的监视器上打开了表单。

这是我的工作代码:

Screen screen = Screen.FromPoint(Cursor.Position);                
Application.Run(new Form1()
{ 
    StartPosition = FormStartPosition.Manual, //Summary in VS actually mentions this as needed to make use of Location
    Location = screen.Bounds.Location,
    WindowState = FormWindowState.Maximized
});

Many years later, but no item was marked as the appropriate answer and I combined two comments to get it working (namely from Reed Copsey and Jason).
I haven't tried any of the other methods described, since this worked without problem and got the Form to open on the monitor my cursor is at, as intended.

Here's my working code:

Screen screen = Screen.FromPoint(Cursor.Position);                
Application.Run(new Form1()
{ 
    StartPosition = FormStartPosition.Manual, //Summary in VS actually mentions this as needed to make use of Location
    Location = screen.Bounds.Location,
    WindowState = FormWindowState.Maximized
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文