跳到主要内容

示例:最小宿主

下面的流程对应 SDK 中的 examples/litheview_demo。真实应用通常把生命周期放在应用对象中,把 View 操作放在窗口对象中。

初始化与运行

struct ShellState {
std::unique_ptr<ShellWindow> window;
};

void LTV_CALLBACK OnReady(void* user_data) {
auto* state = static_cast<ShellState*>(user_data);
state->window = std::make_unique<ShellWindow>();
if (!state->window->Open()) {
ltv_shutdown();
}
}

int RunApp(int argc, const char* const* argv) {
ltv_settings_t settings = {
.struct_size = sizeof(ltv_settings_t),
.version = LTV_STRUCT_VERSION,
.width_dip = 1280,
.height_dip = 720,
.device_scale_factor = 1.0f,
.remote_debugging_enabled = 0,
};

if (ltv_initialize(&settings) != LTV_OK) {
return 1;
}

ShellState state;
return ltv_run(argc, argv, &OnReady, &state);
}

创建并绑定 View

bool ShellWindow::CreateView() {
view_ = ltv_view_create();
if (!view_) {
return false;
}

if (ltv_win_view_attach(
view_, reinterpret_cast<uintptr_t>(render_hwnd_)) != LTV_OK) {
ltv_view_destroy(view_);
view_ = nullptr;
return false;
}

return ltv_view_navigate(view_, "https://example.com/") == LTV_OK;
}

响应 WM_SIZE

void ShellWindow::ResizeView() {
const int width_pixels = client_rect.right - client_rect.left;
const int height_pixels = client_rect.bottom - client_rect.top;
const float scale = static_cast<float>(GetDpiForWindow(hwnd_)) / 96.0f;

ltv_win_view_resize(view_,
static_cast<int>(width_pixels / scale),
static_cast<int>(height_pixels / scale),
width_pixels,
height_pixels);
}

关闭

ShellWindow::~ShellWindow() {
if (view_) {
ltv_win_view_detach(view_);
ltv_view_destroy(view_);
}
}

// 最后一个窗口销毁后:
ltv_shutdown();

完整 Demo 还展示多标签页、popup、文件对话框、下载、IME 和共享 GPU 纹理输出。