Github原文:https://github.com/zserge/webview#webview-for-cc-developers
Webview for C/C++ developers
Getting started
Download webview.h and include it in your C/C++ code: //下载webview。h,并将其包含在C/ c++代码中
// main.c
#define WEBVIEW_IMPLEMENTATION
#include "webview.h"
#ifdef WIN32
int WINAPI WinMain(HINSTANCE hInt, HINSTANCE hPrevInst, LPSTR lpCmdLine,
int nCmdShow) {
#else
int main() {
#endif
/* Open wikipedia in a 800x600 resizable window */
webview("Minimal webview example",
"https://en.m.wikipedia.org/wiki/Main_Page", 800, 600, 1);
return 0;
}
Build it:
# Linux
$ cc main.c -DWEBVIEW_GTK=1 `pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0` -o webview-example
# MacOS
$ cc main.c -DWEBVIEW_COCOA=1 -framework WebKit -o webview-example
# Windows (mingw)
$ cc main.c -DWEBVIEW_WINAPI=1 -lole32 -lcomctl32 -loleaut32 -luuid -mwindows -o webview-example.exe
API
For the most simple use cases there is only one function: //对于最简单的用例,只有一个函数:
int webview(const char *title, const char *url, int width, int height, int resizable);
The following URL schemes are supported: //支持以下网址方案:
http://
andhttps://
, no surprises here. //http://和https://,这并不奇怪。file:///
can be useful if you want to unpack HTML/CSS assets to some temporary directory and point a webview to open index.html from there.//如果您想将HTML/CSS资产解压到某个临时目录,并指向一个webview从那里打开index.html,那么文件:///将非常有用。data:text/html,<html>...</html>
allows to pass short HTML data inline without using a web server or polluting the file system. Further modifications of the webview contents can be done via JavaScript bindings.- //数据:text / html, < html >…</html>允许在不使用web服务器或污染文件系统的情况下内联传递简短的html数据。可以通过JavaScript绑定进一步修改webview内容。
If have chosen a regular http URL scheme, you can use Mongoose or any other web server/framework you like.//如果选择了常规的http URL方案,您可以使用Mongoose或任何其他您喜欢的web服务器/框架。
If you want to have more control over the app lifecycle you can use the following functions:
//如果你想对应用的生命周期有更多的控制,你可以使用以下功能:
struct webview webview = {
.title = title,
.url = url,
.width = w,
.height = h,
.debug = debug,
.resizable = resizable,
};
/* Create webview window using the provided options 使用提供的选项*/创建webview窗口*/
webview_init(&webview);
/* Main app loop, can be either blocking or non-blocking /*主应用程序循环,可以是阻塞或非阻塞*/*/
while (webview_loop(&webview, blocking) == 0);
/* Destroy webview window, often exits the app/*破坏webview窗口,经常退出app */ */
webview_exit(&webview);
/* To change window title later: /*稍后更改窗口标题:*/*/
webview_set_title(&webview, "New title");
/* To terminate the webview main loop:终止webview主循环 */
webview_terminate(&webview);
/* To print logs to stderr, MacOS Console or DebugView: 打印日志到stderr, MacOS控制台或DebugView:*/
webview_debug("exited: %d\n", 1);
To evaluate arbitrary JavaScript code use the following C function://要计算任意JavaScript代码,请使用以下C函数:
webview_eval(&webview, "alert('hello, world');");
There is also a special callback (webview.external_invoke_cb
) that can be invoked from JavaScript:
//还有一个特殊的回调函数(webview.external_invoke_cb)可以从JavaScript调用:
// C
void my_cb(struct webview *w, const char *arg) {
...
}
// JS
window.external.invoke('some arg');
// Exactly one string argument must be provided, to pass more complex objects 为了传递更复杂的对象,必须提供一个字符串参数
// serialize them to JSON and parse it in C. To pass binary data consider using 将它们序列化为JSON并用c语言解析,以传递考虑使用的二进制数据
// base64.mfc类库
window.external.invoke(JSON.stringify({fn: 'sum', x: 5, y: 3}));
Webview library is meant to be used from a single UI thread only. So if you want to call webview_eval
or webview_terminate
from some background thread
//Webview库只能在一个UI线程中使用。如果你想从后台线程调用webview_eval或webview_terminate。
- you have to use
webview_dispatch
to post some arbitrary function with some context to be executed inside the main UI thread: - 你必须使用webview_dispatch来发布一些任意的函数,以及一些要在主UI线程中执行的上下文:
// This function will be executed on the UI thread 这个函数将在UI线程上执行
void render(struct webview *w, void *arg) {
webview_eval(w, ......);
}
// Dispatch render() function from another thread: 函数的作用是:
webview_dispatch(w, render, some_arg);
You may find some C/C++ examples in this repo that demonstrate the API above.//您可以在这个repo中找到一些C/ c++示例,它们演示了上面的API。
Also, there is a more more advanced complete C++ app, Slide, that uses webview as a GUI. You may have a look how webview apps can be built, packaged and how automatic CI/CD can be set up.
此外,还有一个更高级的完整的c++应用程序Slide,它使用webview作为GUI。您可能已经了解了如何构建、打包webview应用程序,以及如何设置自动CI/CD。
Notes(注释)
Execution on OpenBSD requires wxallowed
mount(8) option.
FreeBSD is also supported, to install webkit2 run pkg install webkit2-gtk3
.
在OpenBSD上执行需要wxallowed mount(8)选项。
也支持FreeBSD,安装webkit2运行pkg安装webkit2-gtk3。
License(许可)
Code is distributed under MIT license, feel free to use it in your proprietary projects as well.
代码是在MIT许可下发布的,您也可以在您的专有项目中使用它。