LunaTranslator/cpp/winsharedutils/webview2_extra.cpp

76 lines
2.9 KiB
C++
Raw Normal View History

2024-11-05 15:46:45 +08:00
2024-06-05 02:27:47 +08:00
#include <wrl.h>
#include <wil/com.h>
#include <wil/result.h>
#include <wil/com.h>
#include <wrl/implements.h>
using namespace Microsoft::WRL;
#include <WebView2.h>
2024-06-17 17:01:01 +08:00
#define CHECK_FAILURE(x) \
if (FAILED((x))) \
return x;
2024-06-05 02:27:47 +08:00
2024-11-04 23:10:41 +08:00
DECLARE_API void set_transparent_background(void* m_host){
2024-06-18 17:44:02 +08:00
COREWEBVIEW2_COLOR color;
ZeroMemory(&color,sizeof(color));
wil::com_ptr<ICoreWebView2Controller> m_controller(reinterpret_cast<ICoreWebView2Controller *>(m_host));
wil::com_ptr<ICoreWebView2Controller2> coreWebView2 =
m_controller.try_query<ICoreWebView2Controller2>();
if(coreWebView2){
coreWebView2->put_DefaultBackgroundColor(color);
}
}
2024-11-04 23:10:41 +08:00
DECLARE_API HRESULT put_PreferredColorScheme(void *m_host, COREWEBVIEW2_PREFERRED_COLOR_SCHEME scheme)
2024-06-05 02:27:47 +08:00
{
2024-06-17 17:01:01 +08:00
wil::com_ptr<ICoreWebView2Controller> m_controller(reinterpret_cast<ICoreWebView2Controller *>(m_host));
wil::com_ptr<ICoreWebView2> coreWebView2;
CHECK_FAILURE(m_controller->get_CoreWebView2(&coreWebView2));
auto webView2_13 = coreWebView2.try_query<ICoreWebView2_13>();
if (webView2_13)
{
wil::com_ptr<ICoreWebView2Profile> profile;
CHECK_FAILURE(webView2_13->get_Profile(&profile));
CHECK_FAILURE(profile->put_PreferredColorScheme(scheme));
}
return S_FALSE;
}
2024-11-04 23:10:41 +08:00
DECLARE_API void *add_ZoomFactorChanged(void *m_host, void (*signal)(double))
2024-06-17 17:01:01 +08:00
{
2024-06-05 02:27:47 +08:00
EventRegistrationToken *m_zoomFactorChangedToken = new EventRegistrationToken;
// Register a handler for the ZoomFactorChanged event.
// This handler just announces the new level of zoom on the window's title bar.
reinterpret_cast<ICoreWebView2Controller *>(m_host)->add_ZoomFactorChanged(
Callback<ICoreWebView2ZoomFactorChangedEventHandler>(
[signal](ICoreWebView2Controller *sender, IUnknown *args) -> HRESULT
{
double zoomFactor;
sender->get_ZoomFactor(&zoomFactor);
signal(zoomFactor);
// std::wstring message = L"WebView2APISample (Zoom: " +
// std::to_wstring(int(zoomFactor * 100)) + L"%)";
// SetWindowText(m_appWindow->GetMainWindow(), message.c_str());
return S_OK;
})
.Get(),
m_zoomFactorChangedToken);
return m_zoomFactorChangedToken;
}
2024-11-04 23:10:41 +08:00
DECLARE_API void remove_ZoomFactorChanged(void *m_host, void *m_zoomFactorChangedToken)
2024-06-05 02:27:47 +08:00
{
reinterpret_cast<ICoreWebView2Controller *>(m_host)->remove_ZoomFactorChanged(*reinterpret_cast<EventRegistrationToken *>(m_zoomFactorChangedToken));
delete m_zoomFactorChangedToken;
}
2024-11-04 23:10:41 +08:00
DECLARE_API double get_ZoomFactor(void *m_host)
2024-06-05 02:27:47 +08:00
{
double zoomFactor;
reinterpret_cast<ICoreWebView2Controller *>(m_host)->get_ZoomFactor(&zoomFactor);
return zoomFactor;
}
2024-11-04 23:10:41 +08:00
DECLARE_API void put_ZoomFactor(void *m_host, double zoomFactor)
2024-06-05 02:27:47 +08:00
{
reinterpret_cast<ICoreWebView2Controller *>(m_host)->put_ZoomFactor(zoomFactor);
}