How to show a form on current screen in C#?

2019-05-25 03:48发布

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?

5条回答
爷的心禁止访问
2楼-- · 2019-05-25 04:35
  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.

查看更多
何必那么认真
3楼-- · 2019-05-25 04:37

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;
查看更多
Evening l夕情丶
4楼-- · 2019-05-25 04:39

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;
查看更多
地球回转人心会变
5楼-- · 2019-05-25 04:49

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

查看更多
唯我独甜
6楼-- · 2019-05-25 04:51

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.

查看更多
登录 后发表回答