6
6
using System . ComponentModel ;
7
7
using System . Drawing ;
8
8
using System . Runtime . CompilerServices ;
9
+ using System . Runtime . InteropServices ;
9
10
using System . Windows . Forms ;
10
11
11
12
namespace CefSharp . WinForms . Host
@@ -17,6 +18,10 @@ namespace CefSharp.WinForms.Host
17
18
/// <seealso cref="System.Windows.Forms.Control" />
18
19
public abstract class ChromiumHostControlBase : Control
19
20
{
21
+ [ DllImport ( "user32.dll" , SetLastError = true ) ]
22
+ [ return : MarshalAs ( UnmanagedType . Bool ) ]
23
+ private static extern bool SetWindowPos ( IntPtr hWnd , IntPtr hWndInsertAfter , int X , int Y , int cx , int cy , uint uFlags ) ;
24
+
20
25
/// <summary>
21
26
/// IntPtr that represents the CefBrowser Hwnd
22
27
/// Used for sending messages to the browser
@@ -121,25 +126,10 @@ protected virtual void ResizeBrowser(int width, int height)
121
126
{
122
127
if ( BrowserHwnd != IntPtr . Zero )
123
128
{
124
- ResizeBrowserInternal ( Width , Height ) ;
129
+ SetWindowPosition ( BrowserHwnd , 0 , 0 , width , height ) ;
125
130
}
126
131
}
127
132
128
- /// <summary>
129
- /// Resizes the browser.
130
- /// </summary>
131
- /// <param name="width">width</param>
132
- /// <param name="height">height</param>
133
- /// <remarks>
134
- /// To avoid the Designer trying to load CefSharp.Core.Runtime we explicitly
135
- /// ask for NoInlining.
136
- /// </remarks>
137
- [ MethodImpl ( MethodImplOptions . NoInlining ) ]
138
- private void ResizeBrowserInternal ( int width , int height )
139
- {
140
- NativeMethodWrapper . SetWindowPosition ( BrowserHwnd , 0 , 0 , width , height ) ;
141
- }
142
-
143
133
/// <summary>
144
134
/// When minimized set the browser window size to 0x0 to reduce resource usage.
145
135
/// https://github.com/chromiumembedded/cef/blob/c7701b8a6168f105f2c2d6b239ce3958da3e3f13/tests/cefclient/browser/browser_window_std_win.cc#L87
@@ -148,7 +138,7 @@ internal virtual void HideInternal()
148
138
{
149
139
if ( BrowserHwnd != IntPtr . Zero )
150
140
{
151
- NativeMethodWrapper . SetWindowPosition ( BrowserHwnd , 0 , 0 , 0 , 0 ) ;
141
+ SetWindowPosition ( BrowserHwnd , 0 , 0 , 0 , 0 ) ;
152
142
}
153
143
}
154
144
@@ -159,7 +149,7 @@ internal virtual void ShowInternal()
159
149
{
160
150
if ( BrowserHwnd != IntPtr . Zero )
161
151
{
162
- NativeMethodWrapper . SetWindowPosition ( BrowserHwnd , 0 , 0 , Width , Height ) ;
152
+ SetWindowPosition ( BrowserHwnd , 0 , 0 , Width , Height ) ;
163
153
}
164
154
}
165
155
@@ -183,6 +173,27 @@ internal void RaiseIsBrowserInitializedChangedEvent()
183
173
IsBrowserInitializedChanged ? . Invoke ( this , EventArgs . Empty ) ;
184
174
}
185
175
176
+ private void SetWindowPosition ( IntPtr handle , int x , int y , int width , int height )
177
+ {
178
+ const uint SWP_NOMOVE = 0x0002 ;
179
+ const uint SWP_NOZORDER = 0x0004 ;
180
+ const uint SWP_NOACTIVATE = 0x0010 ;
181
+
182
+ if ( handle != IntPtr . Zero )
183
+ {
184
+ if ( width == 0 && height == 0 )
185
+ {
186
+ // For windowed browsers when the frame window is minimized set the
187
+ // browser window size to 0x0 to reduce resource usage.
188
+ SetWindowPos ( handle , IntPtr . Zero , 0 , 0 , 0 , 0 , SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE ) ;
189
+ }
190
+ else
191
+ {
192
+ SetWindowPos ( handle , IntPtr . Zero , x , y , width , height , SWP_NOZORDER ) ;
193
+ }
194
+ }
195
+ }
196
+
186
197
/// <summary>
187
198
/// Gets the <see cref="ChromiumHostControl"/> or <see cref="ChromiumWebBrowser"/> associated with
188
199
/// a specific <see cref="IBrowser"/> instance.
0 commit comments