mirror of
https://gitee.com/energye/energy.git
synced 2024-12-11 07:30:46 +08:00
A: demo lcl window mask
This commit is contained in:
parent
e5b8e53c41
commit
18cfe18706
74
examples/window/masking/mask/mask.go
Normal file
74
examples/window/masking/mask/mask.go
Normal file
@ -0,0 +1,74 @@
|
||||
package mask
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/energye/energy/v2/cef"
|
||||
"github.com/energye/golcl/lcl"
|
||||
"github.com/energye/golcl/lcl/types"
|
||||
"github.com/energye/golcl/lcl/types/colors"
|
||||
)
|
||||
|
||||
type Mask struct {
|
||||
maskForm *lcl.TForm
|
||||
gifPlay *cef.TGIFPlay
|
||||
progressLabel *lcl.TLabel
|
||||
}
|
||||
|
||||
func Create(window *cef.LCLBrowserWindow) *Mask {
|
||||
var mask = new(Mask)
|
||||
// 创建一个 form 或 panel、或其它任意 IWinControl 组件模拟遮罩
|
||||
// form 可以设置透明度
|
||||
mask.maskForm = lcl.NewForm(window) // form有窗口句柄
|
||||
mask.maskForm.SetParent(window) // 显示在主窗口里的一个子窗口
|
||||
mask.maskForm.SetBorderStyle(types.BsNone) // 因为是窗口所以要去掉标签栏,效果和panel差不多了
|
||||
mask.maskForm.SetAlign(types.AlClient) //铺满整个主窗口
|
||||
mask.maskForm.SetColor(colors.ClSkyblue)
|
||||
// 这是透明设置,只能form, 但是它的子组件也会跟着半透明
|
||||
mask.maskForm.SetAlphaBlend(true) //透明
|
||||
mask.maskForm.SetAlphaBlendValue(150) //透明度
|
||||
mask.maskForm.SetFormStyle(types.FsSystemStayOnTop) //置顶??
|
||||
// 创建一个gif播放组件
|
||||
mask.gifPlay = cef.NewGIFPlay(mask.maskForm)
|
||||
mask.gifPlay.SetParent(mask.maskForm)
|
||||
//在内置FS中读取gif资源
|
||||
mem := lcl.NewMemoryStream()
|
||||
mem.LoadFromFSFile("resources/loading.gif")
|
||||
//play.LoadFromFile("本地加载") //或本地加载
|
||||
mask.gifPlay.LoadFromStream(mem)
|
||||
mem.Free() // stream free
|
||||
//关闭时释放掉 play 占内存啊
|
||||
mask.maskForm.SetOnClose(func(sender lcl.IObject, action *types.TCloseAction) {
|
||||
mask.gifPlay.Animate(false)
|
||||
mask.gifPlay.Free()
|
||||
})
|
||||
mask.progressLabel = lcl.NewLabel(mask.maskForm)
|
||||
mask.progressLabel.SetParent(mask.maskForm)
|
||||
mask.progressLabel.Font().SetColor(colors.ClRed)
|
||||
mask.progressLabel.Font().SetSize(24)
|
||||
// 居中显示
|
||||
var center = func(sender lcl.IObject) {
|
||||
mask.gifPlay.SetLeft(window.Width()/2 - mask.gifPlay.Width()/2)
|
||||
mask.gifPlay.SetTop(window.Height()/2 - mask.gifPlay.Height()/2)
|
||||
mask.progressLabel.SetLeft(window.Width()/2 + 80)
|
||||
mask.progressLabel.SetTop(window.Height()/2 - 20)
|
||||
}
|
||||
mask.maskForm.SetOnShow(center)
|
||||
mask.maskForm.SetOnResize(center)
|
||||
return mask
|
||||
}
|
||||
|
||||
func (m *Mask) Progress(v int) {
|
||||
m.progressLabel.SetCaption(fmt.Sprintf("加载进度: %v", v))
|
||||
}
|
||||
|
||||
func (m *Mask) Show() {
|
||||
m.maskForm.Show()
|
||||
}
|
||||
|
||||
func (m *Mask) Mask() *lcl.TForm {
|
||||
return m.maskForm
|
||||
}
|
||||
|
||||
func (m *Mask) GIFPlay() *cef.TGIFPlay {
|
||||
return m.gifPlay
|
||||
}
|
@ -15,84 +15,42 @@ import (
|
||||
"fmt"
|
||||
"github.com/energye/energy/v2/cef"
|
||||
_ "github.com/energye/energy/v2/examples/syso"
|
||||
"github.com/energye/energy/v2/examples/window/masking/mask"
|
||||
"github.com/energye/golcl/lcl"
|
||||
"github.com/energye/golcl/lcl/types"
|
||||
)
|
||||
|
||||
//go:embed resources
|
||||
var resources embed.FS
|
||||
|
||||
func main() {
|
||||
// 打开 GIF 文件
|
||||
//gifPlay := NewGIFPlay()
|
||||
//gifPlay.LoadFile("E:\\SWT\\gopath\\src\\github.com\\energye\\energy\\examples\\window\\masking\\resources\\autumn.gif")
|
||||
//return
|
||||
//全局初始化 每个应用都必须调用的
|
||||
cef.GlobalInit(nil, resources)
|
||||
//创建应用
|
||||
app := cef.NewApplication()
|
||||
//指定一个URL地址,或本地html文件目录
|
||||
cef.BrowserWindow.Config.Url = "https://energy.yanghy.cn"
|
||||
//cef.BrowserWindow.Config.Url = "https://github.com/energye/energy"
|
||||
cef.BrowserWindow.Config.Url = "https://gitee.com/energye/energy"
|
||||
|
||||
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
|
||||
if window.IsLCL() {
|
||||
bw := window.AsLCLBrowserWindow().BrowserWindow()
|
||||
// 页面呈现组件,默认让它隐藏, 也可不隐藏
|
||||
part := bw.WindowParent()
|
||||
part.SetVisible(false)
|
||||
|
||||
// 创建一个 form 或 panel、或其它任意 IWinControl 组件模拟遮罩
|
||||
// form 可以设置透明度,但也没啥用
|
||||
mask := lcl.NewForm(bw) // form它是有窗口句柄的,所以也可以使用 api 调整它
|
||||
mask.SetParent(bw) // 显示在主窗口里的一个子窗口
|
||||
mask.SetBorderStyle(types.BsNone) // 因为是窗口所以要去掉标签栏,效果和panel差不多了
|
||||
mask.SetAlign(types.AlClient) //铺满整个主窗口
|
||||
// 这是透明设置,只能form, 但是它的子组件也会跟着半透明
|
||||
//mask.SetAlphaBlend(true)
|
||||
//mask.SetAlphaBlendValue(80)
|
||||
// 创建一个gif播放组件
|
||||
gifPlay := cef.NewGIFPlay(mask)
|
||||
gifPlay.SetParent(mask)
|
||||
gifPlay.LoadFSFile("resources/autumn.gif") // gif图片可能有点要求吧??
|
||||
// 这里是根据 gif 图片实际大小
|
||||
gifPlay.SetWidth(639)
|
||||
gifPlay.SetHeight(426)
|
||||
// 居中显示
|
||||
gifPlay.SetLeft(bw.Width()/2 - gifPlay.Width()/2)
|
||||
gifPlay.SetTop(bw.Height()/2 - gifPlay.Height()/2)
|
||||
// 窗口显示时开始播放gif动画
|
||||
mask.SetOnShow(func(sender lcl.IObject) {
|
||||
gifPlay.Start()
|
||||
})
|
||||
// 遮罩窗口关闭时,停止并释放gif
|
||||
mask.SetOnClose(func(sender lcl.IObject, action *types.TCloseAction) {
|
||||
gifPlay.Free()
|
||||
})
|
||||
// 显示form
|
||||
mask.Show()
|
||||
bw.SetOnResize(func(sender lcl.IObject) bool {
|
||||
if gifPlay.IsValid() {
|
||||
gifPlay.SetLeft(bw.Width()/2 - gifPlay.Width()/2)
|
||||
gifPlay.SetTop(bw.Height()/2 - gifPlay.Height()/2)
|
||||
}
|
||||
return false
|
||||
})
|
||||
// 页面加载进度
|
||||
maskForm := mask.Create(bw)
|
||||
maskForm.Show()
|
||||
// 页面加载进度, 控制何时关闭遮罩
|
||||
bw.Chromium().SetOnLoadingProgressChange(func(sender lcl.IObject, browser *cef.ICefBrowser, progress float64) {
|
||||
v := progress * 100
|
||||
v := int(progress * 100)
|
||||
fmt.Println("OnLoadingProgressChange:", v)
|
||||
if v == 100 {
|
||||
cef.RunOnMainThread(func() {
|
||||
// 页面加载进度100%后关闭遮罩层
|
||||
if mask != nil {
|
||||
mask.Close()
|
||||
mask.Free()
|
||||
mask = nil
|
||||
// 最后显示浏览器组件
|
||||
part.SetVisible(true)
|
||||
// 更新UI组件或状态必须在UI线程中运行
|
||||
cef.RunOnMainThread(func() {
|
||||
if maskForm.Mask().IsValid() {
|
||||
maskForm.Progress(v)
|
||||
if v == 100 {
|
||||
maskForm.Mask().Close()
|
||||
maskForm.Mask().Free()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 3.6 MiB |
BIN
examples/window/masking/resources/loading.gif
Normal file
BIN
examples/window/masking/resources/loading.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.8 KiB |
Loading…
Reference in New Issue
Block a user