Help Center

CWebView2.Constructorconstructor

Creates an instance of the `CWebView2`class.

WebViewconstructordocumented

Syntax

CONSTRUCTOR CWebView2 (BYVAL hWin AS HWND, BYVAL pUserDataFolder AS WSTRING PTR = NULL)
CONSTRUCTOR CWebView2 (BYVAL hWin AS HWND, BYREF wszUserDataFolder AS WSTRING)

Parameters

NameDescription
hWin[in] Handle of the window that will host the WebView control.
pUserDataFolder[in] Path of the folder where WebView2 will create the profile files. If You don't specify a folder, WebView2 will create one with the name of the application and the WebView2 suffix, e.g. MyApp.exe.WebView2.
wszUserDataFolder[in] Path of the folder where WebView2 will create the profile files.

Description

Creates an instance of the CWebView2class.

Examples

[Application]

Calls CWebView2(hWnd, userDataFolder)
v

[CWebView2 Constructor]

-> Calls CreateCoreWebView2EnvironmentWithOptions(...)
with a pointer to the CWebView2CreateCoreWebView2EnvironmentCompletedHandlerInternal completion handler
v

[CWebView2CreateCoreWebView2EnvironmentCompletedHandlerInternal .Invoke]

Receives pointer to ICoreWebView2Environment
-> Stores it in m_pEnv
-> Calls CreateCoreWebView2Controller(...)
with apointer to the CWebView2CreateCoreWebView2ControllerCompletedHandlerInternal completion handler
v

[CWebView2CreateCoreWebView2ControllerCompletedHandlerInternal.Invoke]

Receives pointer to ICoreWebView2Controller
-> Stores it in m_pController
-> Obtains a pointer to the ICoreWebView2 interface and stores it in m_pWebView
v

[CWebView2 Ready]

Programmer can now call:
.Navigate("https://...")
.MoveFocus(...)
.ExecuteScript(...)
v

[Normal WebView Usage]

#include once "AfxNova/CWindow.inc" #include once "AfxNova/CWebView2.inc" USING AfxNova

DECLARE FUNCTION wWinMain (BYVAL hInstance AS HINSTANCE, _

BYVAL hPrevInstance AS HINSTANCE, _
                       BYVAL pwszCmdLine AS WSTRING PTR, _
                       BYVAL nCmdShow AS LONG) AS LONG

END wWinMain(GetModuleHandleW(NULL), NULL, wCommand(), SW_NORMAL)

' // Forward declaration DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

' ######################################################################################## ' CWebView2NavigationCompletedEventHandlerImpl class ' Implementation of the ICoreWebView2NavigationCompletedEventHandler callback interface. ' ######################################################################################## TYPE CWebView2NavigationCompletedEventHandlerImpl EXTENDS CWebView2NavigationCompletedEventHandler

DECLARE FUNCTION Invoke (BYVAL sender AS Afx_ICoreWebView2 PTR, BYVAL args AS Afx_ICoreWebView2NavigationCompletedEventArgs PTR) AS HRESULT DECLARE CONSTRUCTOR (BYVAL pWebView2 AS CWebView2 PTR) DECLARE DESTRUCTOR

m_pWebView2 AS CWebView2 PTR m_token AS EventRegistrationToken

END TYPE ' ########################################################################################

' ======================================================================================== ' Main ' ======================================================================================== FUNCTION wWinMain (BYVAL hInstance AS HINSTANCE, _

BYVAL hPrevInstance AS HINSTANCE, _
               BYVAL pwszCmdLine AS WSTRING PTR, _
               BYVAL nCmdShow AS LONG) AS LONG

' // Set process DPI aware SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE) ' // Enable visual styles without including a manifest file AfxEnableVisualStyles

' // Create the main window DIM pWindow AS CWindow DIM hWin AS HWND = pWindow.Create(NULL, "WebView2", @WndProc) ' // Set its client size pWindow.SetClientSize(1050, 500) ' // Center the window pWindow.Center

' // Create an instance of WebView DIM pWebView2 AS CWebView2 = CWebView2(hWin, ExePath) ' // Wait until is ready IF pWebView2.IsReady THEN

' // Set a navigation handler
  DIM pEnv AS CWebView2NavigationCompletedEventHandlerImpl PTR
  pEnv = NEW CWebView2NavigationCompletedEventHandlerImpl(@pWebView2)
  ' // Set an event handler for NewWindow
  DIM pNewWindowHandler AS CWebView2NewWindowRequestedEventHandlerImpl PTR
  pNewWindowHandler = NEW CWebView2NewWindowRequestedEventHandlerImpl(@pWebView2, TRUE)
  ' // Navigate to a web page
  pWebView2.Navigate("https://www.planetsquires.com/protect/forum/index.php")
  ' // Set the focus in the web page
  pWebView2.MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC)

END IF

' // Dispatch Windows messages FUNCTION = pWindow.DoEvents(nCmdShow)

END FUNCTION ' ========================================================================================

' ======================================================================================== ' Main window callback procedure ' ======================================================================================== FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

STATIC hFocus AS HWND

SELECT CASE uMsg

CASE WM_COMMAND
     
     SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)

        ' // If ESC key pressed, close the application sending an WM_CLOSE message
        CASE IDCANCEL
           IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
              SendMessageW hwnd, WM_CLOSE, 0, 0
              EXIT FUNCTION
           END IF

     END SELECT

  CASE WM_SIZE
     ' // Resize the WebView
     DIM pWebView2 AS CWebView2 PTR = AfxCWebView2Ptr(hWnd)
     IF pWebView2 THEN
        DIM rc AS RECT
        IF GetClientRect(hWnd, @rc) THEN
           pWebView2->SetBounds(rc)
        ENDIF
     END IF

CASE WM_DESTROY

' // End the application by sending an WM_QUIT message
     PostQuitMessage(0)
     EXIT FUNCTION

END SELECT

' // Default processing of Windows messages FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)

END FUNCTION ' ========================================================================================

' ######################################################################################## ' Implementation of the CWebView2NavigationCompletedEventHandlerInternal class ' ########################################################################################

' ======================================================================================== CONSTRUCTOR CWebView2NavigationCompletedEventHandlerInternal (BYVAL pWebView2 AS CWebView2 PTR) IF pWebView2 THEN

m_pWebView2 = pWebView2
  pWebView2->AddNavigationCompleted(cast(ANY PTR, @this), @m_token)

END IF END CONSTRUCTOR ' ======================================================================================== ' ======================================================================================== DESTRUCTOR CWebView2NavigationCompletedEventHandlerInternal IF m_pWebView2 THEN m_pWebView2->RemoveNavigationCompleted(m_token) END DESTRUCTOR ' ======================================================================================== ' ======================================================================================== FUNCTION CWebView2NavigationCompletedEventHandlerInternal.Invoke (BYVAL sender AS Afx_ICoreWebView2 PTR, BYVAL args AS Afx_ICoreWebView2NavigationCompletedEventArgs PTR) AS HRESULT DIM IsSuccess AS WINBOOL IF args THEN args->get_IsSuccess(@IsSuccess) DIM WebErrorStatus AS COREWEBVIEW2_WEB_ERROR_STATUS IF args THEN args->get_WebErrorStatus(@WebErrorStatus) DIM NavigationId AS UINT64 IF args THEN args->get_NavigationId(@NavigationId) RETURN S_OK END FUNCTION ' ========================================================================================

Reference

  • Defined in AfxNova/CWebView2.inc:208
  • Documented in WebView/CWebWiew2 Class.md
  • Topic: CWebView2 Class