CWebView::SetDocumentText Method  
 
BOOL SetDocumentText(
  LPCTSTR lpszText  
);

The SetDocumentText method replaces the current document with the specified HTML or plain text.

Parameters

lpszText
A null-terminated string which specifies the HTML or plain text that should replace the current document. If this parameter is NULL the current document will be cleared.

Return Value

If the method succeeds, the return value is non-zero. If the method fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The SetDocumentText method replaces the currently displayed content with the specified HTML or plain text. If the text does not contain any markup, it will be wrapped in HTML as preformatted text and displayed using the default monospaced font. If the lpszText parameter is NULL or an empty string, the browser will display a blank page.

The string passed to this method is normally rendered directly by the browser. If the string is very large (over 256 KiB), the library will first write the text to a local temporary file and then instruct the browser to navigate to that file. This is done to work around limitations in the Edge WebView2 control when handling long inline strings. The temporary file will be automatically deleted when it is no longer needed.

If your project is configured to use a multi-byte character set (ANSI) instead of Unicode, this method will always interpret the lpszText string as UTF-8 encoded text, regardless of the system's current locale or code page. Legacy ANSI code pages cannot reliably represent the full range of characters, and using them in place of Unicode or UTF-8 can lead to data loss or corrupted text.

Important: It is possible to embed markup that references images, scripts, or other resources located on the Internet. While this is supported, you should be aware of potential risks when combining local content with external resources. This can create mixed-content scenarios and introduce cross-origin or security issues. Applications that render untrusted content should avoid linking to remote resources and ensure that the HTML provided is properly validated.

Example

In this example, the method replaces the current page with a basic HTML document. The <img> tag references an external image from a website. This is supported, but as noted above, applications should carefully consider the security implications of mixing local content with external resources.

// Replace the current document with a simple HTML page
LPCTSTR pszHtml =
    _T("<html><head><title>Hello</title></head>")
    _T("<body><h1>Hello, world!</h1>")
    _T("<p>This content was created dynamically.</p>")
    _T("<img src=\"https://example.com/logo.png\" alt=\"Logo\"/>")
    _T("</body></html>");

if (!pWebView->SetDocumentText(pszHtml))
{
    DWORD dwError = pWebView->GetLastError();
    // Handle the error
}

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also