Win32 WebView2将不聚焦HTML页面



我有一个非常困难的时间试图集中的HTML窗口,WebView2加载启动。我已经尝试过从web文档中调用JS,在c++接口和HTML页面之间来回调用-似乎没有任何工作。只有当某些"动作"发生时,如弹出警告对话框时,窗口才会被聚焦。我希望利用c++的强大功能,并将其集成到轻量级的本地浏览器客户端中,但目前看来这相当困难。我希望你能告诉我为什么会这样。我分享了两段代码,一段是WebView2实现的代码,另一段是一个在正常情况下的html示例文件。请不要忘记HTML路径到您的自定义的,我已经标记出来,为您的方便。

  • 提前为大量代码道歉,我不知道我们是否能够在这里折叠代码。微软使用12行来编写一个函数原型。有时候会让人迷失方向。

  • 如果你不使用Visual Studio,我已经做了一个脚本,这样我们仍然能够编译和测试代码(运行作为。bat文件):

@echo off
cl /EHsc /GR /FI"iso646.h" /Zc:strictStrings /we4627 /we4927 /wd4351 /W4 /D"_CRT_SECURE_NO_WARNINGS" /nologo /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c /Ipathtowebview2include /Ipathtowinrtinclude webweb.cpp&^
link webweb.obj WebView2Loader.dll.lib /subsystem:windows user32.lib&^
webweb.exe
pause
  • WebView2测试代码示例
#include <windows.h>
#include <stdlib.h>
#include <string>
#include <tchar.h>
#include <wrl.h>
#include <wil/com.h>
// include WebView2 header
#include "WebView2.h"
using namespace Microsoft::WRL;
// Global variables
// The main window class name.
static TCHAR szWindowClass[] = _T("DesktopApp");
// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("WebView sample");
HINSTANCE hInst;
// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
// Pointer to WebViewController
static wil::com_ptr<ICoreWebView2Controller> webviewController;
// Pointer to WebView window
static wil::com_ptr<ICoreWebView2> webviewWindow;
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR     lpCmdLine,
_In_ int       nCmdShow
)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
// Store instance handle in our global variable
hInst = hInstance;
int dispw = GetSystemMetrics(SM_CXSCREEN);
int disph = GetSystemMetrics(SM_CYSCREEN);
int windw = dispw / 2;
int windh = disph / 2;
int x = (dispw - windw) / 2;
int y = (disph - windh) / 2;
// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
x, y,
windw, windh,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
nCmdShow);
UpdateWindow(hWnd);
// <-- WebView2 sample code starts here -->
// Step 3 - Create a single WebView within the parent window
// Locate the browser and set up the environment for WebView
CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, nullptr,
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
[hWnd](HRESULT result, ICoreWebView2Environment* env) -> HRESULT {
// Create a CoreWebView2Controller and get the associated CoreWebView2 whose parent is the main window hWnd
env->CreateCoreWebView2Controller(hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
[hWnd](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
if (controller != nullptr) {
webviewController = controller;
webviewController->get_CoreWebView2(&webviewWindow);
}
// Add a few settings for the webview
// The demo step is redundant since the values are the default settings
ICoreWebView2Settings* Settings;
webviewWindow->get_Settings(&Settings);
Settings->put_IsScriptEnabled(TRUE);
Settings->put_AreDefaultScriptDialogsEnabled(FALSE);
Settings->put_IsWebMessageEnabled(FALSE);
RECT bounds;
GetClientRect(hWnd, &bounds);
webviewController->put_Bounds(bounds);
webviewWindow->AddScriptToExecuteOnDocumentCreated(L"text.setValue('ayee');", NULL);


webviewWindow->Navigate(L"file:///path/to/test.html");  

return S_OK;
}).Get());
return S_OK;
}).Get());



// <-- WebView2 sample code ends here -->
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_DESTROY  - post a quit message and return
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
TCHAR greeting[] = _T("Hello, Windows desktop!");
switch (message)
{
case WM_SIZE:
if (webviewController != nullptr) {
RECT bounds;
GetClientRect(hWnd, &bounds);
webviewController->put_Bounds(bounds);
};
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
  • 测试示例HTML文件
<!DOCTYPE HTML>
<HTML>
<HEAD>
<STYLE>
body
{
display: flex;
width: 100vw;
overflow: none;
scrollbar: none;
align-items: center;
justify-content: center;
}
#textarea
{
width: 400px;
height: 400px;
resize: none;
overflow:auto;
}
</STYLE>
<TITLE>EXAMPLE</TITLE>
</HEAD>
<BODY>
<textarea ID="textpane"></textarea>
<SCRIPT>
var te = document.getElementById('textpane');
window.onload = function()
{
te.focus();
}
</SCRIPT>
</BODY>
</HTML>

在筛选了Microsoft文档之后,我们认为我们必须添加:

webviewController->MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC);

让WebView组件聚焦于自身。否则,一些消息/JS警告对话框必须与视图一起呈现,它将在退出呈现的对话框窗口后聚焦

  • Navigate()方法后添加
  • 在某些情况下,可能还需要显式地指示HTML中的组件焦点。前面提到的代码只激活WebView, HTML还必须决定哪个组件获得焦点。Win32 API窗口焦点工具不能工作的原因是因为它们处理本地窗口,而不会像这样解释内部的WebView。

window.h库包含一个winuser.h库那个库有一个SetActiveWindow函数Winuser-SetActiveWindow

还有SetFocus函数

相关内容

  • 没有找到相关文章

最新更新