Skip to content

Commit 0c7bc3f

Browse files
committed
WinForms - Refactor ChromiumHostControlBase to use P/Invoke
- Avoid using using NativeMethodWrapper, hopefully this keeps the designer happy, no need to load CefSharp.Core.Runtime.dll Issue #4019
1 parent 35f3a57 commit 0c7bc3f

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

CefSharp.WinForms/Host/ChromiumHostControlBase.cs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.ComponentModel;
77
using System.Drawing;
88
using System.Runtime.CompilerServices;
9+
using System.Runtime.InteropServices;
910
using System.Windows.Forms;
1011

1112
namespace CefSharp.WinForms.Host
@@ -17,6 +18,10 @@ namespace CefSharp.WinForms.Host
1718
/// <seealso cref="System.Windows.Forms.Control" />
1819
public abstract class ChromiumHostControlBase : Control
1920
{
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+
2025
/// <summary>
2126
/// IntPtr that represents the CefBrowser Hwnd
2227
/// Used for sending messages to the browser
@@ -121,25 +126,10 @@ protected virtual void ResizeBrowser(int width, int height)
121126
{
122127
if (BrowserHwnd != IntPtr.Zero)
123128
{
124-
ResizeBrowserInternal(Width, Height);
129+
SetWindowPosition(BrowserHwnd, 0, 0, width, height);
125130
}
126131
}
127132

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-
143133
/// <summary>
144134
/// When minimized set the browser window size to 0x0 to reduce resource usage.
145135
/// https://github.com/chromiumembedded/cef/blob/c7701b8a6168f105f2c2d6b239ce3958da3e3f13/tests/cefclient/browser/browser_window_std_win.cc#L87
@@ -148,7 +138,7 @@ internal virtual void HideInternal()
148138
{
149139
if (BrowserHwnd != IntPtr.Zero)
150140
{
151-
NativeMethodWrapper.SetWindowPosition(BrowserHwnd, 0, 0, 0, 0);
141+
SetWindowPosition(BrowserHwnd, 0, 0, 0, 0);
152142
}
153143
}
154144

@@ -159,7 +149,7 @@ internal virtual void ShowInternal()
159149
{
160150
if (BrowserHwnd != IntPtr.Zero)
161151
{
162-
NativeMethodWrapper.SetWindowPosition(BrowserHwnd, 0, 0, Width, Height);
152+
SetWindowPosition(BrowserHwnd, 0, 0, Width, Height);
163153
}
164154
}
165155

@@ -183,6 +173,27 @@ internal void RaiseIsBrowserInitializedChangedEvent()
183173
IsBrowserInitializedChanged?.Invoke(this, EventArgs.Empty);
184174
}
185175

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+
186197
/// <summary>
187198
/// Gets the <see cref="ChromiumHostControl"/> or <see cref="ChromiumWebBrowser"/> associated with
188199
/// a specific <see cref="IBrowser"/> instance.

0 commit comments

Comments
 (0)