8231bdd4-14f6-4010-b09b-ad180384ea16.png (22.57 KB, 下载次数: 0)
88f76e10-cadc-4902-83b9-01830f1b88c2.png (4.37 KB, 下载次数: 0)
40491ee9-def4-405a-8144-06e9f4423497.png (30.11 KB, 下载次数: 0)
5a55dfd5-ddb2-4e0a-8d8c-42001c245e45.png (14.45 KB, 下载次数: 0)
// main.cpp
#include <Windows.h>
#include <wrl.h>
#include <WebView2.h>
using namespace Microsoft::WRL;
ComPtr<ICoreWebView2Controller> webviewController;
ComPtr<ICoreWebView2> webview;
// 窗口消息处理函数
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
// 初始化WebView2
void InitializeWebView(HWND hWnd) {
CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, nullptr,
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
[hWnd](HRESULT result, ICoreWebView2Environment* env) -> HRESULT {
env->CreateCoreWebView2Controller(hWnd,
Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
[](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
if (controller != nullptr) {
webviewController = controller;
webviewController->get_CoreWebView2(&webview);
}
// 加载HTML内容
webview->NavigateToString(
L"<html><body><h1>Hello from C++!</h1></body></html>");
return S_OK;
}).Get());
return S_OK;
}).Get());
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow) {
// 创建窗口
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"WebViewWindow";
RegisterClass(&wc);
HWND hWnd = CreateWindowEx(0, L"WebViewWindow", L"HTML in C++ Window",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
nullptr, nullptr, hInstance, nullptr);
InitializeWebView(hWnd);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// 消息循环
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
QT += core gui webenginewidgetsTARGET = HtmlInCppSOURCES += main.cpp
// main.cpp
#include <QApplication>
#include <QWebEngineView>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWebEngineView view;
view.setHtml("<h1>Hello from Qt!</h1>");
view.show();
return app.exec();
}
你的猫 发表于 2025-5-17 23:24
方案一:使用Microsoft WebView2(推荐Windows平台)1. 环境准备安装 WebView2 Runtime 下载 WebVie ...
wlp 发表于 2025-5-18 04:30
你需要:电子垃圾 或 Sciter
a449829624 发表于 2025-5-24 05:23
电子垃圾 是什么?
欢迎光临 精易论坛 (https://125.confly.eu.org/) | Powered by Discuz! X3.4 |