I've been converting our application from using CHtmlView
over to WebView2. Our application has a web-based start page that generally gets all its information from our servers, but we have local resources setup in the event of our servers being down or the client having internet trouble. Using CHtmlView
we were able to navigate to an html page in the embedded resources like so:
HINSTANCE hInstance = AfxGetResourceHandle();
ASSERT(hInstance != NULL);
TCHAR lpszModule[_MAX_PATH];
if (GetModuleFileName(hInstance, lpszModule, _MAX_PATH))
{
CString strResourceURL;
strResourceURL.Format(_T("res://%s/%d"), lpszModule, IDR_STARTPAGE);
Navigate2(strResourceURL, NULL,NULL);
}
This would load the page and the images that where also embedded as resources.
WebView2 Navigate doesn't seem to support this same method since passing the same string only gives a blank page.
I was able to load the page into a CString
via LoadResource
and then pass that on to NavigateToString
. That loads the page fine, but none of the images show up. Is there any way to get to the embedded images with WebView2?
WebView2 does not support the res
URI scheme. For serving app content that is not on the disk, you can use:
NavigateToString
: You can provide app created HTML to render, however there is no way to additionally reference subresources that are dynamically app created.WebResourceRequested
: You can use the CoreWebView2.WebResourceRequested
event to intercept any resource requests that you want after setting the filter via AddWebResourceRequestedFilter
. In this event you can intercept any resource request and decide to supply your own response stream rather than allowing the request to actually go to the network.You could use these both together, using NavigateToString
to supply the initial HTML to render, refer to subresources on a particular domain like app.example
in that HTML, and then in WebResourceRequested
intercept all requests to app.example
and provide your own stream.