分享下我自用的函数 (V2)。区别是使用窗口消息 (更灵敏),没使用定时器,减少性能消耗。相应的,变成非活动则触发隐藏。
想要鼠标离开窗口即隐藏,除了定时器,有两个方向:1,在窗口边缘绘制 1 像素的透明边框,鼠标向外移动则隐藏。2,使用系统钩子,监听鼠标离开窗口事件。
EdgeHiding(hwnd, to := 1) {
if id := DllCall('FindWindowW', "ptr", 0, "str", 'EdgeHiding_' hwnd, "ptr")
return SendMessage(40400, , , , id)
WinGetPos &x, &y, &w, &h, hwnd
b := SysGet(4)
switch to ;1下 ,2左, 3上, 4右
{
case 1:
mx := x
my := A_ScreenHeight - h
ax := x
ay := A_ScreenHeight - 2
aw := w
ah := 2
case 2:
mx := 0
my := y
ax := -20
ay := y
aw := 22
ah := h - b
case 3:
mx := x
my := -20
ax := x
ay := 0
aw := w
ah := 22
default:
mx := A_ScreenWidth - w
my := y
ax := A_ScreenWidth - 1
ay := y
aw := 1
ah := h - b
}
WinMove mx, my, , , hwnd
WinSetAlwaysOnTop 1, hwnd
WinHide hwnd
WinSetStyle -0xC00000, hwnd
WinSetExStyle +0x80, hwnd
ui := Gui('+Owner -Caption AlwaysOnTop +ToolWindow +E0x8000000', 'EdgeHiding_' hwnd)
ui.show("NoActivate x" ax " y" ay " w" aw " h" ah)
WinSetTransparent 1, ui.hwnd
ui.OnMessage(0x200, EdgeHiding_show)
ui.OnMessage(40400, EdgeHiding_destroy)
ui.win := hwnd
ui.x := x
ui.y := y
}
EdgeHiding_destroy(ui, *) {
ui.de := 1
WinActivate ui.hwnd
Sleep 20
WinSetAlwaysOnTop 0, ui.win
WinSetStyle +0xC00000, ui.win
WinSetExStyle -0x80, ui.win
WinShow ui.win
WinMove ui.x, ui.y, , , ui.win
WinActivate ui.win
ui.destroy()
}
EdgeHiding_show(ui, *) {
ui.OnMessage(0x200, EdgeHiding_show, 0)
ui.Hide()
if !WinExist(ui.win)
return SendMessage(40400, , , , ui.hwnd)
WinShow ui.win
WinActivate ui.win
WinWaitNotActive ui.win
if ui.de
return
WinHide ui.win
ui.show('NoActivate')
ui.OnMessage(0x200, EdgeHiding_show)
}