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

你。 提交于 2019-12-05 20:48:40
jreichert

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;

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;

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

  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.

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.

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();
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!