package windows import ( "sync" "sync/atomic" "time" "unsafe" ) // 全屏检测:前台窗口矩形覆盖其所在显示器面积 ≥ 阈值即视为全屏。 // 覆盖独占全屏(DXGI exclusive)、无边框全屏、全屏视频等场景。 // FullscreenCoverageThreshold 为全屏判定的最小覆盖比例。 const FullscreenCoverageThreshold = 0.95 var ( procGetForegroundWindow = user32Hook.NewProc("GetForegroundWindow") procGetWindowRect = user32Hook.NewProc("GetWindowRect") procIsZoomed = user32Hook.NewProc("IsZoomed") ) // IsFullscreenRect 判定窗口矩形是否覆盖屏幕矩形达到阈值(纯函数,可测)。 // coverage = 交集面积 / 屏幕面积。 func IsFullscreenRect(win, screen MonitorRect, threshold float64) bool { if threshold <= 0 { threshold = FullscreenCoverageThreshold } // 交集 left := max32(win.Left, screen.Left) top := max32(win.Top, screen.Top) right := min32(win.Right, screen.Right) bottom := min32(win.Bottom, screen.Bottom) if right <= left || bottom <= top { return false } inter := float64(right-left) * float64(bottom-top) screenArea := float64(screen.Right-screen.Left) * float64(screen.Bottom-screen.Top) if screenArea <= 0 { return false } return inter/screenArea >= threshold } func max32(a, b int32) int32 { if a > b { return a } return b } func min32(a, b int32) int32 { if a < b { return a } return b } // rectFromWin 读取前台窗口矩形(虚拟屏幕坐标)。 func rectFromWin(hwnd uintptr) (MonitorRect, bool) { var r rect hr, _, _ := procGetWindowRect.Call(hwnd, uintptr(unsafe.Pointer(&r))) if hr == 0 { return MonitorRect{}, false } return MonitorRect{Left: r.Left, Top: r.Top, Right: r.Right, Bottom: r.Bottom}, true } // foregroundFullscreen 检测当前前台窗口是否全屏。 // 无前台窗口或检测失败返回 false(不误抑制)。 func foregroundFullscreen() bool { hwnd, _, _ := procGetForegroundWindow.Call() if hwnd == 0 { return false } // 最大化窗口不是全屏:任务栏可见时最大化窗口覆盖工作区 ≈96%, // 仅靠覆盖率阈值(95%)会把浏览器等普通最大化窗口误判为全屏, // 导致规则被错误抑制(角落/边缘功能失效)。用 IsZoomed 排除 // 带 WS_MAXIMIZE 样式的窗口;真全屏窗口(游戏独占/无边框、 // 全屏视频)没有该样式,不受影响。 z, _, _ := procIsZoomed.Call(hwnd) if z != 0 { return false } win, ok := rectFromWin(hwnd) if !ok { return false } rects, err := MonitorRects() if err != nil || len(rects) == 0 { return false } // 前台窗口可能跨显示器:取与其交集最大的显示器。 best := rects[0] bestOverlap := int64(-1) for _, r := range rects { left := max32(win.Left, r.Left) top := max32(win.Top, r.Top) right := min32(win.Right, r.Right) bottom := min32(win.Bottom, r.Bottom) if right > left && bottom > top { ov := int64(right-left) * int64(bottom-top) if ov > bestOverlap { bestOverlap = ov best = r } } } return IsFullscreenRect(win, best, FullscreenCoverageThreshold) } // FullscreenWatcher 轮询前台窗口全屏状态,事件路径零额外 Win32 调用。 type FullscreenWatcher struct { value atomic.Bool mu sync.Mutex started bool stop chan struct{} done chan struct{} interval time.Duration } // NewFullscreenWatcher 创建全屏状态轮询器(默认 300ms)。 func NewFullscreenWatcher() *FullscreenWatcher { return &FullscreenWatcher{interval: 300 * time.Millisecond} } // Start 启动轮询 goroutine。幂等。 func (w *FullscreenWatcher) Start() { w.mu.Lock() defer w.mu.Unlock() if w.started { return } w.started = true w.stop = make(chan struct{}) w.done = make(chan struct{}) go w.loop() } // Stop 停止轮询并等待退出。幂等。 func (w *FullscreenWatcher) Stop() { w.mu.Lock() if !w.started { w.mu.Unlock() return } w.started = false close(w.stop) w.mu.Unlock() <-w.done } // IsFullscreen 返回最近一次检测的全屏状态(原子读,事件路径使用)。 func (w *FullscreenWatcher) IsFullscreen() bool { return w.value.Load() } func (w *FullscreenWatcher) loop() { defer close(w.done) ticker := time.NewTicker(w.interval) defer ticker.Stop() for { select { case <-w.stop: return case <-ticker.C: w.value.Store(foregroundFullscreen()) } } }