@@ -7,75 +7,55 @@ 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);
43- CHECK_FAILURE(m_webView->add_FaviconChanged(
44- Callback<ICoreWebView2FaviconChangedEventHandler >(
45- [ this] (ICoreWebView2* sender, IUnknown* args) -> HRESULT {
10+ Gdiplus::GdiplusStartupInput gdiplusStartupInput;
4611
47- wil::com_ptr<IStream> iconStream = SHCreateMemStream(nullptr, 0);
48-
49- sender->GetFavicon(COREWEBVIEW2_FAVICON_IMAGE_FORMAT_PNG, iconStream.get(),
50- Callback<ICoreWebView2ExperimentalGetFaviconCompletedHandler>(
51- [iconStream, this](HRESULT error_code) -> HRESULT
12+ // Initialize GDI+.
13+ Gdiplus::GdiplusStartup (&gdiplusToken_ , &gdiplusStartupInput, NULL);
14+ CHECK_FAILURE(m_webView2->add_FaviconChanged(
15+ Callback<ICoreWebView2ExperimentalFaviconChangedEventHandler >(
16+ [ this] (ICoreWebView2* sender, IUnknown* args) -> HRESULT {
17+ if (m_faviconChanged)
5218 {
53- if (error_code == S_OK)
54- {
55- Gdiplus::Bitmap* iconBitmap =
56- new Gdiplus::Bitmap(iconStream.get());
57- HICON icon;
58- if (!iconBitmap->GetHICON(&icon))
59- {
60- SendMessage(
61- m_appWindow->GetMainWindow(), WM_SETICON,
62- ICON_SMALL, (LPARAM)icon);
63- }
64- }
65-
66- return S_OK;
67- }).Get());
68-
19+ wil::unique_cotaskmem_string url;
20+ Microsoft::WRL::ComPtr<ICoreWebView2 >
21+ webview2Experimental;
22+ CHECK_FAILURE(
23+ sender->QueryInterface(IID_PPV_ARGS(&webview2)));
24+
25+ CHECK_FAILURE(webview2Experimental->get_FaviconUri(&url));
26+ std::wstring strUrl(url.get());
27+
28+ webview2->GetFavicon(
29+ COREWEBVIEW2_FAVICON_IMAGE_FORMAT_PNG,
30+ Callback<ICoreWebView2ExperimentalGetFaviconCompletedHandler>(
31+ [this, strUrl](HRESULT errorCode, IStream* iconStream) -> HRESULT
32+ {
33+ CHECK_FAILURE(errorCode);
34+ Gdiplus::Bitmap iconBitmap(iconStream);
35+ wil::unique_hicon icon;
36+ if (!iconBitmap.GetHICON(&icon))
37+ {
38+ m_favicon = std::move(icon);
39+ SendMessage(
40+ m_appWindow->GetMainWindow(), WM_SETICON,
41+ ICON_SMALL, (LPARAM)m_favicon.get());
42+ m_statusBar.Show(strUrl);
43+ }
44+
45+ return S_OK;
46+ })
47+ .Get());
48+ }
6949 return S_OK;
70- }).Get(), &m_faviconChangedToken));
50+ }).Get(), &m_faviconChangedToken));
51+ }
7152```
7253## .NET / WinRT Registering a listener for favicon changes
7354```c#
7455webView.CoreWebView2.FaviconChanged += (CoreWebView2 sender, Object arg) =>
7556{
7657 IStream stream = await webView.CoreWebView2.GetFaviconAsync(
77- CoreWebView2FaviconImageFormat.Png,
78- stream);
58+ CoreWebView2FaviconImageFormat.Png);
7959 // setting the window Icon to the bitmap
8060 this.Icon = BitmapFrame.Create(stream);
8161
@@ -105,14 +85,18 @@ interface ICoreWebView2FaviconChangedEventHandler : IUnknown {
10585 [ in] IUnknown* args);
10686}
10787
108- // / This interface is a handler for the completion of the copying for the`imageStream`.
109- // / The 'error_code` is E_NOT_SET if the there is no image. Otherwise error_code
110- // / is the result from the image write operation.
88+ // / This interface is a handler for the completion of the population of
89+ // / `imageStream`.
90+ // / `errorCode` returns S_OK if the API succeeded.
91+ // / The image is returned in the `faviconStream` object. If there is no image
92+ // / then no data would be copied into the imageStream.
11193// / For more details, see the `GetFavicon` API.
11294[uuid(A2508329-7DA8-49D7-8C05-FA125E4AEE8D), object, pointer_default(unique)]
113- interface ICoreWebView2GetFaviconCompletedHandler : IUnknown {
95+ interface ICoreWebView2ExperimentalGetFaviconCompletedHandler : IUnknown {
11496 /// Called to notify the favicon has been retrieved.
115- HRESULT Invoke([ in] HRESULT error_code);
97+ HRESULT Invoke(
98+ [ in] HRESULT errorCode,
99+ [ in] IStream* faviconStream);
116100}
117101
118102// / This is the ICoreWebView2 Experimental Favicon interface.
@@ -181,7 +165,7 @@ namespace Microsoft.Web.WebView2.Core
181165
182166 event Windows .Foundation .TypedEventHandler < CoreWebView2 , Object > FaviconChanged ;
183167
184- Windows .Foundation .IAsyncAction < IStream > GetFaviconAsync (CoreWebView2FaviconImageFormat format );
168+ Windows .Foundation .IAsyncAction GetFaviconAsync (CoreWebView2FaviconImageFormat format );
185169 }
186170 }
187171}
0 commit comments