energy/example/windows/transparent/transparen.go
2023-07-12 18:35:29 +08:00

55 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"embed"
"fmt"
"github.com/energye/energy/v2/cef"
"github.com/energye/energy/v2/cef/winapi"
"github.com/energye/energy/v2/pkgs/assetserve"
"github.com/energye/energy/v2/types"
"github.com/energye/golcl/lcl/types/colors"
"github.com/energye/golcl/lcl/win"
)
//go:embed resources
var resources embed.FS
func main() {
cef.GlobalInit(nil, nil)
cefApp := cef.NewApplication()
cef.BrowserWindow.Config.Url = "http://localhost:22022/index.html"
cef.BrowserWindow.Config.EnableHideCaption = true
cef.BrowserWindow.Config.AlwaysOnTop = true
cef.BrowserWindow.Config.Width = 400
cef.BrowserWindow.Config.Height = 450
cef.SetBrowserProcessStartAfterCallback(func(b bool) {
fmt.Println("主进程启动 创建一个内置http服务")
//通过内置http服务加载资源
server := assetserve.NewAssetsHttpServer()
server.PORT = 22022 //服务端口号
server.AssetsFSName = "resources" //必须设置目录名和资源文件夹同名
server.Assets = &resources
go server.StartHttpServer()
})
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
WindowTransparent(types.HWND(window.Handle()))
})
cef.Run(cefApp)
}
//WindowTransparent 窗口透明组件不透明设置
func WindowTransparent(hWnd types.HWND) {
//SetWindowLongHandle GWL_EXSTYLE GetWindowLongHandle GWL_EXSTYLE or WS_EX_LAYERED
//SetLayeredWindowAttributesHandleclWhite255LWA_COLORKEY;
exStyle := winapi.WinGetWindowLong(hWnd, win.GWL_EXSTYLE)
exStyle = exStyle | win.WS_EX_LAYERED //win.WS_EX_LAYERED&^win.WS_EX_TRANSPARENT // or WS_EX_TRANSPARENT;
winapi.WinSetWindowLong(hWnd, win.GWL_EXSTYLE, exStyle)
win.SetLayeredWindowAttributes(hWnd.ToPtr(), //指定分层窗口句柄
colors.ClWhite, //crKey指定需要透明的背景颜色值可用RGB()宏 0-255
255, //bAlpha设置透明度0表示完全透明255表示不透明
win.LWA_COLORKEY) //LWA_ALPHA: crKey无效bAlpha有效
//win.LWA_ALPHA|win.LWA_COLORKEY) //LWA_ALPHA: crKey无效bAlpha有效
//LWA_COLORKEY窗体中的所有颜色为crKey的地方全透明bAlpha无效。
//LWA_ALPHA | LWA_COLORKEYcrKey的地方全透明其它地方根据bAlpha确定透明度
}