@@ -7,6 +7,39 @@ We propose a new Webview2 event which would allow developers to access the curre
77# Examples
88## Win32 C++ Registering a listener for favicon changes
99``` cpp
10+ m_webView2->add_FaviconChanged (
11+ Callback<ICoreWebView2FaviconChangedEventHandler>(
12+ [this](ICoreWebView2* sender, IUnknown* args) -> HRESULT {
13+ wil::unique_cotaskmem_string url;
14+ Microsoft::WRL::ComPtr<ICoreWebView2Experimental12>
15+ webview2Experimental;
16+ CHECK_FAILURE(
17+ sender->QueryInterface(IID_PPV_ARGS(&webview2Experimental)));
18+
19+ CHECK_FAILURE(webview2Experimental->get_FaviconUri(&url));
20+ std::wstring strUrl(url.get());
21+
22+ webview2Experimental->GetFavicon(
23+ COREWEBVIEW2_FAVICON_IMAGE_FORMAT_PNG,
24+ Callback<ICoreWebView2ExperimentalGetFaviconCompletedHandler>(
25+ [this, strUrl](IStream* iconStream) -> HRESULT
26+ {
27+ Gdiplus::Bitmap iconBitmap(iconStream);
28+ wil::unique_hicon icon;
29+ if (!iconBitmap.GetHICON(&icon))
30+ {
31+ m_favicon = std::move(icon);
32+ SendMessage(
33+ m_appWindow->GetMainWindow(), WM_SETICON,
34+ ICON_SMALL, (LPARAM)m_favicon.get());
35+ m_statusBar.Show(strUrl);
36+ }
37+
38+ return S_OK;
39+ })
40+ .Get());
41+ return S_OK;
42+ }).Get(), &m_faviconChangedToken);
1043CHECK_FAILURE(m_webView->add_FaviconChanged(
1144 Callback<ICoreWebView2FaviconChangedEventHandler >(
1245 [ this] (ICoreWebView2* sender, IUnknown* args) -> HRESULT {
@@ -40,8 +73,7 @@ CHECK_FAILURE(m_webView->add_FaviconChanged(
4073```c#
4174webView.CoreWebView2.FaviconChanged += (CoreWebView2 sender, Object arg) =>
4275{
43- System.IO.Stream stream = new System.IO.MemoryStream();
44- await webView.CoreWebView2.GetFaviconAsync(
76+ IStream stream = await webView.CoreWebView2.GetFaviconAsync(
4577 CoreWebView2FaviconImageFormat.Png,
4678 stream);
4779 // setting the window Icon to the bitmap
@@ -89,9 +121,10 @@ interface ICoreWebView2_10 : ICoreWebView2_9 {
89121 /// Add an event handler for the `FaviconChanged` event.
90122 /// The `FaviconChanged` event is raised when the
91123 /// [favicon](https://developer.mozilla.org/en-US/docs/Glossary/Favicon)
92- /// of the top-level document changes or if script dynamically changes the favicon .
124+ /// when the favicon had a different URL then the previous URL .
93125 /// The FaviconChanged event will be raised for first navigating to a new
94- /// document, whether or not a document declares a Favicon in HTML. The event will
126+ /// document, whether or not a document declares a Favicon in HTML if the
127+ /// favicon is different from the previous fav icon. The event will
95128 /// be raised again if a favicon is declared in its HTML or has script
96129 /// to set its favicon. The favicon information can then be retrieved with
97130 /// `GetFavicon` and `FaviconUri`.
@@ -148,7 +181,7 @@ namespace Microsoft.Web.WebView2.Core
148181
149182 event Windows .Foundation .TypedEventHandler < CoreWebView2 , Object > FaviconChanged ;
150183
151- Windows .Foundation .IAsyncAction GetFaviconAsync (CoreWebView2FaviconImageFormat format , Windows . Storage . Streams . IRandomAccessStream imageStream );
184+ Windows .Foundation .IAsyncAction < IStream > GetFaviconAsync (CoreWebView2FaviconImageFormat format );
152185 }
153186 }
154187}
0 commit comments