[Asm] 纯文本查看 复制代码
; 聚焦阅读 - 圆形聚光灯版(圆心跟随鼠标,点击不穿透问题已修)
; F1 开关 | = 增大半径 | - 减小半径 | Esc 退出
#Requires AutoHotkey v2.0
#SingleInstance Force
#Warn
; 必须在创建任何窗口前声明 DPI 感知,否则高分屏缩放会导致坐标偏移
DllCall("user32\SetProcessDPIAware")
; ================= 配置区 =================
global Config := Map(
"Radius", 150, ; 初始圆半径(像素)
"RadiusStep", 10, ; 半径调整步长
"MinRadius", 30, ; 最小半径
"MaxRadius", 600, ; 最大半径
"BackgroundOpacity", 180, ; 背景透明度(0~255)
"UpdateInterval", 12 ; 更新频率(毫秒)
)
; ================ 全局变量 ================
global SpotGui := 0
global IsSpotlightActive := false
global UpdateTimer := 0
; ================ 主程序 ================
CreateSpotlightGui()
; ================ 热键绑定 ================
F1::ToggleSpotlight()
=::AdjustRadius(1)
-::AdjustRadius(-1)
Esc::ExitApp
; ================ 函数定义 ================
CreateSpotlightGui() {
global SpotGui, Config
; WS_EX_TRANSPARENT(0x20) | WS_EX_NOACTIVATE(0x08000000) = 0x08000020
; NOACTIVATE:窗口永远不被激活/抢焦点
SpotGui := Gui("+AlwaysOnTop -Caption +ToolWindow +E0x08000020")
SpotGui.BackColor := "Black"
SpotGui.MarginX := 0
SpotGui.MarginY := 0
SpotGui.Add("Text", "w" A_ScreenWidth " h" A_ScreenHeight " BackgroundTrans")
SpotGui.Show("NA x0 y0 w" A_ScreenWidth " h" A_ScreenHeight)
WinSetTransparent(Config["BackgroundOpacity"], SpotGui.Hwnd)
; 拦截 WM_NCHITTEST(0x0084),对我们的窗口一律返回 HTTRANSPARENT(-1)
; 这样系统会把所有鼠标点击/移动消息直接穿透到下面的窗口,聚光灯窗口完全不参与命中测试
OnMessage(0x0084, WM_NCHITTEST)
SpotGui.Hide()
}
WM_NCHITTEST(wParam, lParam, msg, hwnd) {
global SpotGui
if (hwnd = SpotGui.Hwnd)
return -1 ; HTTRANSPARENT —— 鼠标事件穿透
}
ToggleSpotlight() {
global IsSpotlightActive, SpotGui, UpdateTimer
IsSpotlightActive := !IsSpotlightActive
if IsSpotlightActive {
SpotGui.Show()
UpdateTimer := SetTimer(UpdateSpotlightPosition, Config["UpdateInterval"])
UpdateSpotlightPosition()
ToolTip("圆形聚光灯已开启 [= 增大 / - 减小 / F1 关闭 / Esc 退出]")
SetTimer () => ToolTip(), -2200
} else {
SpotGui.Hide()
if UpdateTimer
SetTimer(UpdateTimer, 0)
ToolTip("圆形聚光灯已关闭")
SetTimer () => ToolTip(), -1000
}
}
UpdateSpotlightPosition() {
global Config, SpotGui
; 直接调 GetCursorPos 拿物理屏幕坐标,不经过 AHK 的坐标模式/虚拟化
pt := Buffer(8, 0)
DllCall("user32\GetCursorPos", "ptr", pt)
mx := NumGet(pt, 0, "int")
my := NumGet(pt, 4, "int")
r := Config["Radius"]
; 外矩形:整个屏幕
hRgnOuter := DllCall("CreateRectRgn", "int", 0, "int", 0
, "int", A_ScreenWidth, "int", A_ScreenHeight, "ptr")
; 内圆:以鼠标为圆心
hRgnInner := DllCall("CreateEllipticRgn"
, "int", mx - r, "int", my - r
, "int", mx + r, "int", my + r, "ptr")
; 外矩形 − 内圆 = 黑色遮罩(圆内挖空)
hRgnResult := DllCall("CreateRectRgn", "int", 0, "int", 0, "int", 1, "int", 1, "ptr")
DllCall("CombineRgn", "ptr", hRgnResult, "ptr", hRgnOuter
, "ptr", hRgnInner, "int", 4) ; 4 = RGN_DIFF
; SetWindowRgn 接管 hRgnResult 所有权,由系统释放
DllCall("SetWindowRgn", "ptr", SpotGui.Hwnd, "ptr", hRgnResult, "int", 1)
DllCall("DeleteObject", "ptr", hRgnOuter)
DllCall("DeleteObject", "ptr", hRgnInner)
}
AdjustRadius(direction) {
global Config
if !IsSpotlightActive
return
newRadius := Config["Radius"] + (direction * Config["RadiusStep"])
newRadius := Min(Max(newRadius, Config["MinRadius"]), Config["MaxRadius"])
Config["Radius"] := newRadius
UpdateSpotlightPosition()
ToolTip("聚光灯半径: " newRadius " px")
SetTimer () => ToolTip(), -800
}