@@ -56,6 +56,34 @@ private IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam) {
5656 return CallNextHookEx ( mouseHook , nCode , wParam , lParam ) ;
5757 }
5858
59+ public WINDOWPLACEMENT WindowPlacement {
60+ get {
61+ if ( ! IsHandleCreated || Handle == IntPtr . Zero ) {
62+ throw new NullReferenceException ( "Handle must not be NULL." ) ;
63+ }
64+
65+ WINDOWPLACEMENT value = new WINDOWPLACEMENT ( ) ;
66+ value . length = Marshal . SizeOf ( value ) ;
67+
68+ if ( ! GetWindowPlacement ( Handle , ref value ) ) {
69+ Marshal . ThrowExceptionForHR ( Marshal . GetHRForLastWin32Error ( ) ) ;
70+ }
71+ return value ;
72+ }
73+
74+ set {
75+ if ( ! IsHandleCreated || Handle == IntPtr . Zero ) {
76+ throw new NullReferenceException ( "Handle must not be NULL." ) ;
77+ }
78+
79+ value . length = Marshal . SizeOf ( value ) ;
80+
81+ if ( ! SetWindowPlacement ( Handle , ref value ) ) {
82+ Marshal . ThrowExceptionForHR ( Marshal . GetHRForLastWin32Error ( ) ) ;
83+ }
84+ }
85+ }
86+
5987 private const int FULLSCREEN_EXIT_LABEL_TIMER_TIME = 2500 ;
6088
6189 private System . Windows . Forms . Timer exitFullscreenLabelTimer = null ;
@@ -86,12 +114,9 @@ private bool ExitFullscreenLabelTimer {
86114
87115 private bool fullscreen = false ;
88116 private FormBorderStyle fullscreenFormBorderStyle = FormBorderStyle . Sizable ;
89- private FormWindowState fullscreenWindowState = FormWindowState . Maximized ;
90- private Point fullscreenLocation ;
91- private Size fullscreenSize ;
117+ private WINDOWPLACEMENT fullscreenWindowPlacement ;
92118
93- private Point closableWebBrowserLocation ;
94- private Size closableWebBrowserSize ;
119+ private Rectangle closableWebBrowserBounds ;
95120
96121 // be very careful modifying this property
97122 // it is very picky about the order things happen
@@ -102,6 +127,10 @@ public bool Fullscreen {
102127 }
103128
104129 set {
130+ if ( ! IsHandleCreated || Handle == IntPtr . Zero ) {
131+ return ;
132+ }
133+
105134 if ( closableWebBrowser == null ) {
106135 return ;
107136 }
@@ -115,19 +144,22 @@ public bool Fullscreen {
115144 if ( fullscreen ) {
116145 // get the original properties before modifying them
117146 fullscreenFormBorderStyle = FormBorderStyle ;
118- fullscreenWindowState = WindowState ;
119- fullscreenLocation = Location ;
120- fullscreenSize = Size ;
147+ fullscreenWindowPlacement = WindowPlacement ;
148+
149+ closableWebBrowserBounds = closableWebBrowser . Bounds ;
121150
122- closableWebBrowserLocation = closableWebBrowser . Location ;
123- closableWebBrowserSize = closableWebBrowser . Size ;
151+ WINDOWPLACEMENT windowPlacement = fullscreenWindowPlacement ;
124152
125153 // need to do this first to have an effect if starting maximized
126- WindowState = FormWindowState . Normal ;
154+ windowPlacement . showCmd = SW . SW_NORMAL ;
155+ WindowPlacement = windowPlacement ;
156+
127157 // disable resizing
128158 FormBorderStyle = FormBorderStyle . None ;
159+
129160 // enter fullscreen
130- WindowState = FormWindowState . Maximized ;
161+ windowPlacement . showCmd = SW . SW_MAXIMIZE ;
162+ WindowPlacement = windowPlacement ;
131163
132164 // make strips invisible so the Closable Web Browser can be Docked
133165 // (this must happen AFTER entering fullscreen to prevent toolbar mouseover bug)
@@ -157,11 +189,15 @@ public bool Fullscreen {
157189 }
158190 }
159191
192+ WINDOWPLACEMENT windowPlacement = fullscreenWindowPlacement ;
193+
160194 // need to do this first to reset the window to its former size
161195 FormBorderStyle = FormBorderStyle . Sizable ;
196+
162197 // exit fullscreen
163- WindowState = FormWindowState . Normal ;
164-
198+ windowPlacement . showCmd = SW . SW_NORMAL ;
199+ WindowPlacement = windowPlacement ;
200+
165201 // make strips visible so the Closable Web Browser can be Anchored
166202 fullscreenButton . Checked = false ;
167203 toolBarToolStrip . Visible = true ;
@@ -170,12 +206,9 @@ public bool Fullscreen {
170206
171207 // reset to the original properties before modifying them
172208 FormBorderStyle = fullscreenFormBorderStyle ;
173- WindowState = fullscreenWindowState ;
174- Location = fullscreenLocation ;
175- Size = fullscreenSize ;
209+ WindowPlacement = fullscreenWindowPlacement ;
176210
177- closableWebBrowser . Location = closableWebBrowserLocation ;
178- closableWebBrowser . Size = closableWebBrowserSize ;
211+ closableWebBrowser . Bounds = closableWebBrowserBounds ;
179212
180213 // commit by bringing the window to the front
181214 BringToFront ( ) ;
@@ -545,56 +578,74 @@ private void WebBrowserMode_FormClosing(object sender, FormClosingEventArgs e) {
545578 private void WebBrowserMode_Activated ( object sender , EventArgs e ) {
546579 Application . AddMessageFilter ( messageFilter ) ;
547580
548- if ( Fullscreen ) {
549- if ( WindowState == FormWindowState . Minimized ) {
550- BringToFront ( ) ;
551- }
581+ if ( ! IsHandleCreated || Handle == IntPtr . Zero ) {
582+ return ;
583+ }
584+
585+ if ( ! Fullscreen ) {
586+ return ;
587+ }
588+
589+ SW showCmd = WindowPlacement . showCmd ;
590+
591+ if ( showCmd == SW . SW_SHOWMINIMIZED
592+ || showCmd == SW . SW_MINIMIZE
593+ || showCmd == SW . SW_SHOWMINNOACTIVE ) {
594+ BringToFront ( ) ;
552595 }
553596 }
554597
555598 private void WebBrowserMode_Deactivate ( object sender , EventArgs e ) {
556599 Application . RemoveMessageFilter ( messageFilter ) ;
557600
558- if ( Fullscreen ) {
559- IntPtr foregroundWindow = GetForegroundWindow ( ) ;
560-
561- // we are the active window, because we are only now deactivating
562- // if this process has the foreground window, it'll be the active window
563- if ( Handle == foregroundWindow ) {
564- // this process opened a new window
565- if ( ! CanFocus ) {
566- // if there is a window above us in the z-order
567- IntPtr previousWindow = GetWindow ( Handle , GW . GW_HWNDPREV ) ;
568-
569- if ( previousWindow != IntPtr . Zero ) {
570- // if we own the window above us in the z-order
571- if ( Handle == GetWindow ( previousWindow , GW . GW_OWNER ) ) {
572- // the new window is a dialog that prevents focus to this window
573- return ;
574- }
575- }
576- }
601+ if ( ! IsHandleCreated || Handle == IntPtr . Zero ) {
602+ return ;
603+ }
577604
578- // the new window is not a dialog that prevents focus to this window
579- Fullscreen = false ;
580- return ;
581- }
605+ if ( ! Fullscreen ) {
606+ return ;
607+ }
582608
583- // another process opened a window
584- if ( foregroundWindow != IntPtr . Zero ) {
585- // if we own the foreground window
586- if ( Handle == GetWindow ( foregroundWindow , GW . GW_OWNER ) ) {
587- // the new window is owned by this window
588- if ( CanFocus ) {
589- // the new window is not a dialog that prevents focus to this window
590- Fullscreen = false ;
609+ IntPtr foregroundWindow = GetForegroundWindow ( ) ;
610+
611+ // we are the active window, because we are only now deactivating
612+ // if this process has the foreground window, it'll be the active window
613+ if ( Handle == foregroundWindow ) {
614+ // this process opened a new window
615+ if ( ! CanFocus ) {
616+ // if there is a window above us in the z-order
617+ IntPtr previousWindow = GetWindow ( Handle , GW . GW_HWNDPREV ) ;
618+
619+ if ( previousWindow != IntPtr . Zero ) {
620+ // if we own the window above us in the z-order
621+ if ( Handle == GetWindow ( previousWindow , GW . GW_OWNER ) ) {
622+ // the new window is a dialog that prevents focus to this window
623+ return ;
591624 }
592- return ;
593625 }
594626 }
595627
596- WindowState = FormWindowState . Minimized ;
628+ // the new window is not a dialog that prevents focus to this window
629+ Fullscreen = false ;
630+ return ;
631+ }
632+
633+ // another process opened a window
634+ if ( foregroundWindow != IntPtr . Zero ) {
635+ // if we own the foreground window
636+ if ( Handle == GetWindow ( foregroundWindow , GW . GW_OWNER ) ) {
637+ // the new window is owned by this window
638+ if ( CanFocus ) {
639+ // the new window is not a dialog that prevents focus to this window
640+ Fullscreen = false ;
641+ }
642+ return ;
643+ }
597644 }
645+
646+ WINDOWPLACEMENT windowPlacement = WindowPlacement ;
647+ windowPlacement . showCmd = SW . SW_MINIMIZE ;
648+ WindowPlacement = windowPlacement ;
598649 }
599650
600651 private void closableWebBrowser_Navigated ( object sender , WebBrowserNavigatedEventArgs e ) {
@@ -685,7 +736,7 @@ private void closableWebBrowser_WebBrowserPaint(object sender, EventArgs e) {
685736 // lame fix: browser hangs when window.open top attribute > control height (why?)
686737 // Width, Height, and WindowState changes all work here
687738 // Width/Height are less obvious and Height doesn't cause text reflow
688- if ( WindowState != FormWindowState . Maximized ) {
739+ if ( WindowPlacement . showCmd != SW . SW_SHOWMAXIMIZED ) {
689740 // add first in case it's zero
690741 Height ++ ;
691742 Height -- ;
0 commit comments