C# 如何在当前屏幕上显示表单?
我想在调用它的同一窗口中显示一个新表单。 我知道一种通过类似于下面的代码在主屏幕或虚拟屏幕上显示此表单的方法:
MyForm.Location = Screen.PrimaryScreen.Bounds.Location;
但我想在当前屏幕上显示它。 有没有办法找出并在当前屏幕上显示它?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我做了这样的事情来显示我的表单以当前屏幕为中心:
I have done something like this to show my form centered to the current screen:
您可以使用相同的技术,但不使用 PrimaryScreen,而是使用 Screen.FromPoint 和 光标位置:
You can use the same technique, but instead of using the PrimaryScreen, grab the screen using Screen.FromPoint and Cursor.Position:
听起来您没有将 StartPosition 设置为手动。
It sounds like you aren't setting the StartPosition to Manual.
如果您已经有一个父窗体并希望在同一屏幕上打开一个新窗体,请为 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.这应该会在活动屏幕上打开表单。参考
此 StartPosition 的更多值。
This should open up the form on the active screen. Refer
this for more values of StartPosition.
我知道这已经晚了,但仍然发布我的答案希望对其他人有所帮助。经过多次尝试,我用 3 台显示器完成了这项工作
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
许多年后,但没有任何项目被标记为适当的答案,我结合了两条评论以使其发挥作用(即来自 Reed Copsey 和 Jason)。
我没有尝试过所描述的任何其他方法,因为这没有问题,并且按照预期在我的光标所在的监视器上打开了表单。
这是我的工作代码:
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: