The SetBounds method updates the position and size of the
embedded browser window relative to the client area of the host window. It
calls the put_Bounds method on the underlying WebView2 controller to
resize the child window that renders the web content. The specified coordinates
and dimensions must fall within the client area of the host window associated
with the browser instance. Attempting to specify bounds outside of the host
window will result in clipping or the control being partially hidden.
The nWidth and nHeight parameters are device units,
the same pixel units that the Win32 API uses with functions like
MoveWindow and SetWindowPos. If the host window was created
as a per-monitor DPI-aware window, the values your application gets
with a WM_SIZE message are already scaled for the current DPI
and you should just pass those values directly to this method. If your
application is not DPI-aware, Windows will attempt to scale the values
for you through DPI virtualization. It is important to note that these
values are not logical units like Twips and they are not independent
of DPI scaling.
Typically this method is called from within a WM_SIZE or
WM_MOVE message handler so that the browser control resizes
and repositions itself along with the parent window. You can also call this
function directly to dock the browser into a specific portion of the host
window's client area, such as aligning it within a child frame or panel.
In most applications it is sufficient to call this method in response
to a WM_SIZE message so the browser control resizes along with
the client area of the host window. However, in a per-monitor DPI-aware
application it may also be useful to call this method when handling
WM_MOVE. This is because moving a top-level window between
monitors can trigger a DPI change even if the window size itself does not
change. Reapplying the bounds in response to WM_MOVE ensures
the control is correctly adjusted to the new scaling factor. For
applications that are not DPI-aware, handling WM_MOVE is
generally unnecessary since the client area origin remains at (0,0) and
Windows applies DPI virtualization automatically.
Calling SetBounds does not affect the visibility of the
browser window. To hide or show the control, use the Hide or
Show methods, and to query its visibility, use IsVisible.