Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void Form1_Load(object sender, EventArgs e)
- {
- this.FormBorderStyle = FormBorderStyle.None;
- this.Bounds = GetSecondaryScreen().Bounds;
- }
- private Screen GetSecondaryScreen()
- {
- foreach (Screen screen in Screen.AllScreens)
- {
- if (screen != Screen.PrimaryScreen)
- return screen;
- }
- return Screen.PrimaryScreen;
- }
- protected override void OnStateChanged(EventArgs e)
- {
- if (WindowState == WindowState.Maximized)
- {
- if (WindowStyle.None != WindowStyle)
- WindowStyle = WindowStyle.None;
- }
- else if (WindowStyle != WindowStyle.SingleBorderWindow)
- WindowStyle = WindowStyle.SingleBorderWindow;
- base.OnStateChanged(e);
- }
- this.Left = SystemParameters.PrimaryScreenWidth + 100;
- this.WindowState = System.Windows.WindowState.Maximized;
- using System.Linq;
- namespace ExtendedControls
- {
- static public class WindowExt
- {
- // NB : Best to call this function from the windows Loaded event or after showing the window
- // (otherwise window is just positioned to fill the secondary monitor rather than being maximised).
- public static void MaximizeToSecondaryMonitor(this Window window)
- {
- var secondaryScreen = System.Windows.Forms.Screen.AllScreens.Where(s => !s.Primary).FirstOrDefault();
- if (secondaryScreen != null)
- {
- if (!window.IsLoaded)
- window.WindowStartupLocation = WindowStartupLocation.Manual;
- var workingArea = secondaryScreen.WorkingArea;
- window.Left = workingArea.Left;
- window.Top = workingArea.Top;
- window.Width = workingArea.Width;
- window.Height = workingArea.Height;
- // If window isn't loaded then maxmizing will result in the window displaying on the primary monitor
- if ( window.IsLoaded )
- window.WindowState = WindowState.Maximized;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment