Guest User

Untitled

a guest
Jun 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. this.FormBorderStyle = FormBorderStyle.None;
  4. this.Bounds = GetSecondaryScreen().Bounds;
  5. }
  6.  
  7. private Screen GetSecondaryScreen()
  8. {
  9. foreach (Screen screen in Screen.AllScreens)
  10. {
  11. if (screen != Screen.PrimaryScreen)
  12. return screen;
  13. }
  14. return Screen.PrimaryScreen;
  15. }
  16.  
  17. protected override void OnStateChanged(EventArgs e)
  18. {
  19. if (WindowState == WindowState.Maximized)
  20. {
  21. if (WindowStyle.None != WindowStyle)
  22. WindowStyle = WindowStyle.None;
  23. }
  24. else if (WindowStyle != WindowStyle.SingleBorderWindow)
  25. WindowStyle = WindowStyle.SingleBorderWindow;
  26.  
  27. base.OnStateChanged(e);
  28. }
  29.  
  30. this.Left = SystemParameters.PrimaryScreenWidth + 100;
  31. this.WindowState = System.Windows.WindowState.Maximized;
  32.  
  33. using System.Linq;
  34.  
  35. namespace ExtendedControls
  36. {
  37. static public class WindowExt
  38. {
  39.  
  40. // NB : Best to call this function from the windows Loaded event or after showing the window
  41. // (otherwise window is just positioned to fill the secondary monitor rather than being maximised).
  42. public static void MaximizeToSecondaryMonitor(this Window window)
  43. {
  44. var secondaryScreen = System.Windows.Forms.Screen.AllScreens.Where(s => !s.Primary).FirstOrDefault();
  45.  
  46. if (secondaryScreen != null)
  47. {
  48. if (!window.IsLoaded)
  49. window.WindowStartupLocation = WindowStartupLocation.Manual;
  50.  
  51. var workingArea = secondaryScreen.WorkingArea;
  52. window.Left = workingArea.Left;
  53. window.Top = workingArea.Top;
  54. window.Width = workingArea.Width;
  55. window.Height = workingArea.Height;
  56. // If window isn't loaded then maxmizing will result in the window displaying on the primary monitor
  57. if ( window.IsLoaded )
  58. window.WindowState = WindowState.Maximized;
  59. }
  60. }
  61. }
  62. }
Add Comment
Please, Sign In to add comment