Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 11-Nov-12 5:25am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
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();
  Permalink  

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

  Print Answers RSS
0 Peter Leow 375
1 Sergey Alexandrovich Kryukov 270
2 Zoltán Zörgő 258
3 CHill60 195
4 TheRealSteveJudge 180
0 Sergey Alexandrovich Kryukov 8,853
1 OriginalGriff 6,631
2 Peter Leow 4,102
3 Zoltán Zörgő 3,714
4 Kornfeld Eliyahu Peter 2,525


Advertise | Privacy | Mobile
Web04 | 2.8.150123.1 | Last Updated 12 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100