Click here to Skip to main content
15,884,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please I need to control my extended monitor and to show two different application one on main and another on the external monitor, how can i do it using c# code I need to show specific window on the main , and another one on the extended thank you in advanced
Posted

1 solution

Hi there.

This is an extension method to move a form onto the first non-primary screen.
It returns a Nullable<bool> to tell whether the form have been moved, not moved or an error did occur.

C#
namespace System.Windows.Forms
{
    public static class FormExtensions
    {
        public static bool? SetScreenToFirstNonPrimary(this Form self)
        {
            try
            {
                // Retrieves the collection of available screens (monitors)
                var aScreens = Screen.AllScreens;
                // If count is not greater than 1 then exit
                if (aScreens.Length <= 1)
                    return false; // screen kept original

                // Saves current screen reference
                var screenOld = Screen.FromControl(self);
                foreach (var screen in aScreens)
                {
                    // Skips primary and current screen
                    if (screen.Primary || screen.Equals(screenOld))
                        continue;
    
                    var boundsScreen = screen.Bounds;
    
                    var oldState = self.WindowState;
                    // If form is currently maximized ...
                    if (oldState == FormWindowState.Maximized)
                    {
                        self.WindowState = FormWindowState.Normal;
                        self.StartPosition = FormStartPosition.Manual;
                        self.Location = boundsScreen.Location;
                        self.WindowState = FormWindowState.Maximized;
                    }
                    else
                    {
                        self.StartPosition = FormStartPosition.Manual;
                        // Center into new screen
			var sizeGaps = boundsScreen.Size - self.Size;
			var x = boundsScreen.Left + (sizeGaps.Width / 2);
			var y = boundsScreen.Top + (sizeGaps.Height / 2);
                        self.Location = new Point(x, y);
                    }

                    return true; // screen has been changed
                }
        
                return false; // screen kept original
            }
            catch (Exception ex)
            {
                // process exception ex
                // ...

                return null; // error occurred
            }
        }
    }
}


You can call it from the form's constructor just after InitializeComponent(); as
this.SetScreenToFirstNonPrimary();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900