迁移到 mygo 单进程架构并接入签名自更新
- 单进程:托盘常驻 + 原生设置窗口(internal/settings),移除 Wails 前端与 IPC - 触发器(鼠标角落 / 边缘滚轮 / 热键)+ 规则引擎 + 动作注册表 - 调节提示条:截屏 + glass.Blur 三层毛玻璃、背景跟随屏幕变化、悬停光感与拖动 - 音量 Core Audio COM、亮度 DDC/CI(gamma 兜底)、多显示器求平均 - 配置热重载 + 版本迁移 v1→v5、便携模式、崩溃取证三件套 - updater/native 插件 + 托盘「检查更新…」,watchdog 识别更新重启 - 自建 Gitea 发布:scripts/publish-gitea.ps1(updates.url 指向 latest Release)
This commit is contained in:
1 parent
814012ec52
commit
0b8b9b0775
202 files changed
+7254
-13572
No files matched your search
@@ -0,0 +1,172 @@
|
||||
package windows
|
||||
|
||||
// 屏幕区域截屏:提示条需要把背后的桌面内容模糊后作为自绘背景。
|
||||
//
|
||||
// 仅依赖 user32/gdi32 的基础 API;返回 *image.RGBA(alpha 恒为 255),
|
||||
// 由调用方决定模糊与缩放。窗口坐标是虚拟屏幕物理像素,可跨负数显示器。
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
srccopy = 0x00CC0020
|
||||
|
||||
biRGB = 0
|
||||
)
|
||||
|
||||
var (
|
||||
procGetCursorPos = user32Hook.NewProc("GetCursorPos")
|
||||
procCreateCompatibleDC = gdi32Hook.NewProc("CreateCompatibleDC")
|
||||
procCreateCompatibleBitmap = gdi32Hook.NewProc("CreateCompatibleBitmap")
|
||||
procSelectObject = gdi32Hook.NewProc("SelectObject")
|
||||
procBitBlt = gdi32Hook.NewProc("BitBlt")
|
||||
procGetDIBits = gdi32Hook.NewProc("GetDIBits")
|
||||
procDeleteDC = gdi32Hook.NewProc("DeleteDC")
|
||||
procDeleteObject = gdi32Hook.NewProc("DeleteObject")
|
||||
procGetPixel = gdi32Hook.NewProc("GetPixel")
|
||||
)
|
||||
|
||||
// bitmapInfoHeader 对应 Win32 BITMAPINFOHEADER(40 字节)。
|
||||
type bitmapInfoHeader struct {
|
||||
size uint32
|
||||
width int32
|
||||
height int32
|
||||
planes uint16
|
||||
bitCount uint16
|
||||
compression uint32
|
||||
sizeImage uint32
|
||||
xPelsPerMeter int32
|
||||
yPelsPerMeter int32
|
||||
clrUsed uint32
|
||||
clrImportant uint32
|
||||
}
|
||||
|
||||
// bitmapInfo 对应 Win32 BITMAPINFO;32bpp BI_RGB 不需要调色板。
|
||||
type bitmapInfo struct {
|
||||
header bitmapInfoHeader
|
||||
colors [1]uint32
|
||||
}
|
||||
|
||||
// CursorPos 返回当前光标在虚拟屏幕上的物理像素坐标。
|
||||
//
|
||||
// 提示条是鼠标穿透窗口,收不到鼠标消息,悬停判断只能靠读光标位置。
|
||||
func CursorPos() (x, y int, ok bool) {
|
||||
var p struct{ x, y int32 }
|
||||
r, _, _ := procGetCursorPos.Call(uintptr(unsafe.Pointer(&p)))
|
||||
if r == 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
return int(p.x), int(p.y), true
|
||||
}
|
||||
|
||||
// WindowRect 读取窗口在虚拟屏幕上的物理像素矩形。
|
||||
func WindowRect(hwnd uintptr) (x, y, width, height int, ok bool) {
|
||||
if hwnd == 0 {
|
||||
return 0, 0, 0, 0, false
|
||||
}
|
||||
var r rect
|
||||
hr, _, _ := procGetWindowRect.Call(hwnd, uintptr(unsafe.Pointer(&r)))
|
||||
if hr == 0 || r.Right <= r.Left || r.Bottom <= r.Top {
|
||||
return 0, 0, 0, 0, false
|
||||
}
|
||||
return int(r.Left), int(r.Top), int(r.Right - r.Left), int(r.Bottom - r.Top), true
|
||||
}
|
||||
|
||||
// CaptureRect 截取虚拟屏幕上的一个物理像素矩形。坐标可为负数(副屏在
|
||||
// 主屏左侧/上方时)。失败时返回错误,调用方应退化为纯色面板。
|
||||
func CaptureRect(x, y, width, height int) (*image.RGBA, error) {
|
||||
if width <= 0 || height <= 0 {
|
||||
return nil, fmt.Errorf("capture: invalid size %dx%d", width, height)
|
||||
}
|
||||
|
||||
screenDC, _, err := procGetDC.Call(0)
|
||||
if screenDC == 0 {
|
||||
return nil, fmt.Errorf("capture: get screen dc: %v", err)
|
||||
}
|
||||
defer procReleaseDC.Call(0, screenDC)
|
||||
|
||||
memDC, _, err := procCreateCompatibleDC.Call(screenDC)
|
||||
if memDC == 0 {
|
||||
return nil, fmt.Errorf("capture: create memory dc: %v", err)
|
||||
}
|
||||
defer procDeleteDC.Call(memDC)
|
||||
|
||||
bmp, _, err := procCreateCompatibleBitmap.Call(screenDC, uintptr(width), uintptr(height))
|
||||
if bmp == 0 {
|
||||
return nil, fmt.Errorf("capture: create bitmap: %v", err)
|
||||
}
|
||||
defer procDeleteObject.Call(bmp)
|
||||
|
||||
old, _, err := procSelectObject.Call(memDC, bmp)
|
||||
if old == 0 {
|
||||
return nil, fmt.Errorf("capture: select bitmap: %v", err)
|
||||
}
|
||||
// GetDIBits 要求位图不再被选入 DC,因此在读取像素前先恢复旧对象。
|
||||
defer procSelectObject.Call(memDC, old)
|
||||
|
||||
r, _, err := procBitBlt.Call(
|
||||
memDC, 0, 0, uintptr(width), uintptr(height),
|
||||
screenDC, uintptr(int32(x)), uintptr(int32(y)), srccopy)
|
||||
if r == 0 {
|
||||
return nil, fmt.Errorf("capture: bitblt %d,%d %dx%d: %v", x, y, width, height, err)
|
||||
}
|
||||
|
||||
// 先把位图从 DC 里换出来;defer 里再恢复一次是幂等的。
|
||||
if r, _, _ := procSelectObject.Call(memDC, old); r == 0 {
|
||||
return nil, fmt.Errorf("capture: deselect bitmap failed")
|
||||
}
|
||||
|
||||
bi := bitmapInfo{header: bitmapInfoHeader{
|
||||
size: uint32(unsafe.Sizeof(bitmapInfoHeader{})),
|
||||
width: int32(width),
|
||||
height: -int32(height), // 负高度表示 top-down DIB
|
||||
planes: 1,
|
||||
bitCount: 32,
|
||||
compression: biRGB,
|
||||
}}
|
||||
buf := make([]byte, width*height*4)
|
||||
r, _, err = procGetDIBits.Call(
|
||||
memDC, bmp, 0, uintptr(height),
|
||||
uintptr(unsafe.Pointer(&buf[0])),
|
||||
uintptr(unsafe.Pointer(&bi)), 0)
|
||||
if r == 0 {
|
||||
return nil, fmt.Errorf("capture: getdibits: %v", err)
|
||||
}
|
||||
|
||||
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
for row := 0; row < height; row++ {
|
||||
src := buf[row*width*4:]
|
||||
dst := img.Pix[row*img.Stride:]
|
||||
for col := 0; col < width; col++ {
|
||||
s := col * 4
|
||||
d := col * 4
|
||||
// DIB 32bpp BI_RGB 是 B,G,R,X;输出 RGBA 并强制不透明。
|
||||
dst[d+0] = src[s+2]
|
||||
dst[d+1] = src[s+1]
|
||||
dst[d+2] = src[s+0]
|
||||
dst[d+3] = 255
|
||||
}
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
// ScreenPixel 读取虚拟屏幕上单个物理像素的颜色,返回的是 DWM 合成后的最终
|
||||
// 画面。提示条窗口内胶囊以外的四角是透明的,读这些点就能拿到"胶囊下面正
|
||||
// 在显示什么",用来判断背景是否已经变了(变了才值得重新截屏)。
|
||||
func ScreenPixel(x, y int) (r, g, b uint8, ok bool) {
|
||||
dc, _, _ := procGetDC.Call(0)
|
||||
if dc == 0 {
|
||||
return 0, 0, 0, false
|
||||
}
|
||||
defer procReleaseDC.Call(0, dc)
|
||||
v, _, _ := procGetPixel.Call(dc, uintptr(int32(x)), uintptr(int32(y)))
|
||||
// CLR_INVALID:坐标不在屏幕内或读取失败(COLORREF 全 1)。
|
||||
if uint32(v) == 0xFFFFFFFF {
|
||||
return 0, 0, 0, false
|
||||
}
|
||||
// COLORREF 的字节序是 0x00BBGGRR。
|
||||
return uint8(v), uint8(v >> 8), uint8(v >> 16), true
|
||||
}
|
||||
Reference in new issue
Block a user