提交 4fc747d9 编写于 作者: yanghye's avatar yanghye

增加创建浏览器窗口示例

上级 143ee26d
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
package main
import (
"embed"
"fmt"
"github.com/energye/energy/v2/cef"
"github.com/energye/energy/v2/cef/ipc"
"github.com/energye/energy/v2/cef/ipc/callback"
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/types"
)
//go:embed resources
var resources embed.FS
func main() {
//全局初始化 每个应用都必须调用的
cef.GlobalInit(nil, &resources)
//创建应用
app := cef.NewApplication()
//cefApp.SetExternalMessagePump(false)
//cefApp.SetMultiThreadedMessageLoop(false)
//指定一个URL地址,或本地html文件目录
cef.BrowserWindow.Config.Url = "fs://energy"
cef.BrowserWindow.Config.LocalResource(cef.LocalLoadConfig{
ResRootDir: "resources",
FS: &resources,
}.Build())
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
//浏览器窗口之后回调,在这里获取创建的浏览器ID
event.SetOnAfterCreated(func(sender lcl.IObject, browser *cef.ICefBrowser, window cef.IBrowserWindow) bool {
// 创建完之后再拿浏览器id
fmt.Println("on-create-window-ok", browser.Identifier(), window.Id())
ipc.Emit("on-create-window-ok", browser.Identifier(), window.Id())
return false // 什么都不做
})
//---- ipc 监听事件
// 监听事件, 创建新窗口
ipc.On("createWindow", func(name string) {
// 创建窗口常规属性对象
wp := cef.NewWindowProperty()
wp.Url = "fs://energy/new-window.html"
wp.Title = name
// 创建新的浏览器窗口
newWindow := cef.NewLCLBrowserWindow(nil, wp)
newWindow.SetWidth(800)
newWindow.SetHeight(600)
// EnableAllDefaultEvent 启用所有默认实现事件
// 如果未启用,所有事件需要你自己控制和实现, 大部分功能无法使用
newWindow.EnableAllDefaultEvent()
newWindow.SetOnClose(func(sender lcl.IObject, action *types.TCloseAction) bool {
//窗口关闭时触发
ipc.Emit("on-close-window", newWindow.Id())
return false // 不做处理
})
window.RunOnMainThread(func() {
// 在主线程中
newWindow.Show()
})
})
// 改变当前窗口大小
ipc.On("resize", func(_type int, channel callback.IChannel) {
println("resize type", _type, "channel", channel.ChannelId(), channel.BrowserId())
win := cef.BrowserWindow.GetWindowInfo(channel.BrowserId())
if win == nil {
return
}
window.RunOnMainThread(func() {
switch _type {
case 1:
win.SetSize(400, 200)
case 2:
win.SetSize(600, 400)
case 3:
win.SetSize(800, 600)
}
})
})
})
//运行应用
cef.Run(app)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>new-window</title>
<script>
function createWindow() {
ipc.emit("createWindow", ["NewWindowName " + new Date().getTime()], function (windowId) {
});
}
ipc.on("on-create-window-ok", function (id) {
document.getElementById("msg").innerText = "窗口创建成功: " + id
});
ipc.on("on-close-window", function (id) {
document.getElementById("msg").innerText = "窗口被关闭: " + id
});
</script>
</head>
<body style="width: 100%;text-align: center;overflow: hidden;">
<button onclick="createWindow()">创建新窗口</button>
<a href="index.html" target="_blank">超链接打开新窗口</a>
<div id="msg"></div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>new-window</title>
<script>
function resize(type) {
ipc.emit("resize", [type]);
}
</script>
</head>
<body style="width: 100%;text-align: center;overflow: hidden;">
改变当前窗口大小:
<button onclick="resize(1)"></button>
<button onclick="resize(2)"></button>
<button onclick="resize(3)"></button>
<a href="other.html">other</a>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>new-window</title>
</head>
<body style="width: 100%;text-align: center;overflow: hidden;">
<h1>Other</h1>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册