@@ -15,40 +15,39 @@ CHECK_FAILURE(m_webView2->add_FaviconChanged(
1515 Callback<ICoreWebView2FaviconChangedEventHandler >(
1616 [ this] (ICoreWebView2* sender, IUnknown* args) -> HRESULT {
1717 wil::unique_cotaskmem_string url;
18- Microsoft::WRL::ComPtr<ICoreWebView2 >
19- webview2;
20- CHECK_FAILURE(
21- sender->QueryInterface(IID_PPV_ARGS(&webview2)));
2218
23- CHECK_FAILURE(webview2 ->get_FaviconUri(&url));
19+ CHECK_FAILURE(sender ->get_FaviconUri(&url));
2420 std::wstring strUrl(url.get());
25-
26- webview2->GetFavicon(
27- COREWEBVIEW2_FAVICON_IMAGE_FORMAT_PNG,
28- Callback<ICoreWebView2GetFaviconCompletedHandler>(
29- [this, strUrl](HRESULT errorCode, IStream* iconStream) -> HRESULT
21+ if (strUrl.empty())
22+ {
23+ m_favicon.reset();
24+ SendMessage(m_appWindow->GetMainWindow(), WM_SETICON, ICON_SMALL, (LPARAM)NULL);
25+ }
26+ else
27+ {
28+ webview2->GetFavicon(
29+ COREWEBVIEW2_FAVICON_IMAGE_FORMAT_PNG,
30+ Callback<ICoreWebView2GetFaviconCompletedHandler>(
31+ [this](HRESULT errorCode, IStream* iconStream) -> HRESULT
3032 {
3133 CHECK_FAILURE(errorCode);
3234 Gdiplus::Bitmap iconBitmap(iconStream);
3335 wil::unique_hicon icon;
34- if (iconBitmap.GetHICON(&icon) == Gdiplus::Status::Ok)
36+ if (iconBitmap.GetHICON(&icon) = Gdiplus::Status::Ok)
3537 {
3638 m_favicon = std::move(icon);
37- SendMessage(
38- m_appWindow->GetMainWindow(), WM_SETICON,
39- ICON_SMALL, (LPARAM)m_favicon.get());
4039 }
4140 else
4241 {
43- SendMessage(
44- m_appWindow->GetMainWindow(), WM_SETICON,
45- ICON_SMALL, (LPARAM)IDC_NO);
42+ m_favicon.reset();
4643 }
4744
45+ SendMessage(
46+ m_appWindow->GetMainWindow(), WM_SETICON,
47+ ICON_SMALL, (LPARAM)m_favicon.get());
4848 return S_OK;
49- })
50- .Get());
51-
49+ }).Get());
50+ }
5251 return S_OK;
5352 }).Get(), &m_faviconChangedToken));
5453}
@@ -67,11 +66,15 @@ webView.CoreWebView2.FaviconChanged += (object sender, Object arg) =>
6766};
6867```
6968# API Notes
70- Note that even if a web page does not have a Favicon, the FaviconChanged event
71- is raised when the page is navigated to. The Favicon would be an
72- empty image stream and empty Uri for the lack of a favicon. The end developer is expected to handle this scenario.
73- Otherwise, we raise the FaviconChanged with an observed change to the
74- Favicon. In that scenario, the CoreWebView2 has an updated value for the FaviconUri property, and the GetFavicon method to match the updated favicon.
69+ Note that even if a web page does not have a Favicon and there was not a previously
70+ loaded Favicon, the event is not raised. Otherwise if there is not Favicon and the
71+ previous page did have a Favicon, the FaviconChanged event is raised when the page
72+ is navigated. The Favicon would be an empty image stream and empty Uri for the lack
73+ of a favicon. The end developer is expected to handle this scenario. Otherwise, we
74+ raise the FaviconChanged with an observed change to the Favicon. In that scenario,
75+ the CoreWebView2 has an updated value for the FaviconUri property, and the
76+ GetFavicon method to match the updated favicon. Loading the same Favicon twice does
77+ re-raise the FaviconChanged event.
7578See [ API Details] ( #api-details ) Section below for API reference
7679# API Details
7780## Win32 C++
@@ -144,10 +147,10 @@ interface ICoreWebView2_10 : ICoreWebView2_9 {
144147
145148[v1_enum]
146149typedef enum COREWEBVIEW2_FAVICON_IMAGE_FORMAT {
147- /// Indicates that CoreWebView2.GetFaviconAsync should return the favicon in PNG format.
150+ /// Indicates that CoreWebView2.GetFavicon should return the favicon in PNG format.
148151 COREWEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT_PNG,
149152
150- /// Indicates that CoreWebView2.GetFaviconAsync should return the favicon in JPG format.
153+ /// Indicates that CoreWebView2.GetFavicon should return the favicon in JPG format.
151154 COREWEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT_JPEG,
152155}
153156```
0 commit comments