![微調活動視窗 AHK 腳本周圍的紅色邊框](https://rvso.com/image/1525455/%E5%BE%AE%E8%AA%BF%E6%B4%BB%E5%8B%95%E8%A6%96%E7%AA%97%20AHK%20%E8%85%B3%E6%9C%AC%E5%91%A8%E5%9C%8D%E7%9A%84%E7%B4%85%E8%89%B2%E9%82%8A%E6%A1%86.png)
我是一個自動熱鍵新手,但我發現了這個有用的腳本,可以在我的活動視窗周圍放置紅色邊框,使其更加引人注目。我已經安裝了 autohotkey 並運行腳本,但有兩個問題:
- 當最大化或視窗捕捉到一側時它不起作用
- 邊界不均勻
如果有人可以幫助並修復此腳本,或提供如何修復它的詳細說明(請記住我是新手),我將不勝感激。
#Persistent
SetTimer, DrawRect, 50
border_thickness = 10
border_color = FF0000
DrawRect:
WinGetPos, x, y, w, h, A
if (x="")
return
Gui, +Lastfound +AlwaysOnTop +Toolwindow
iw:= w+4
ih:= h + 4
w:=w+ 8
h:=h + 8
x:= x - border_thickness
y:= y - border_thickness
Gui, Color, FF0000
Gui, -Caption
WinSet, Region, 0-0 %w%-0 %w%-%h% 0-%h% 0-0 %border_thickness%-%border_thickness% %iw%-%border_thickness% %iw%-%ih% %border_thickness%-%ih% %border_thickness%-%border_thickness%
Gui, Show, w%w% h%h% x%x% y%y% NoActivate, Table awaiting Action
return
答案1
我建議重命名變數以使其有意義。該WinSet
語句以 x/y 座標對的形式繪製兩個框,因此以 x/y 座標對的方式命名變數並在語句之前對它們進行賦值WinSet
就不會那麼混亂了
在此特定範例中,您也繪製了視窗外部的邊框。當視窗的外側位於螢幕邊緣時,邊框位於螢幕外,因此不會可見。如果您想在邊框位於(甚至遠離)螢幕邊緣時將其繪製在螢幕上,則需要將邊框繪製在視窗邊界內部(或部分內部),而不是將其繪製在視窗邊界外部。
但這只是部分解決方案,因為在最大化狀態下,視窗距離螢幕甚至比邊緣更遠。在這種情況下,您必須偵測最大化條件並將邊界偏移得比正常情況更遠一定數量的像素。
我調整了變數的重命名並調整了您的程式碼以給出一個範例。您可以更改不同的“邊框類型”以查看效果...如果您想以不同的方式繪製它,剩下的只是數學。
#Persistent
SetTimer, DrawRect, 50
border_thickness = 5
border_color = FF0000
DrawRect:
WinGetPos, x, y, w, h, A
if (x="")
return
Gui, +Lastfound +AlwaysOnTop +Toolwindow
borderType:="inside" ; set to inside, outside, or both
if (borderType="outside") {
outerX:=0
outerY:=0
outerX2:=w+2*border_thickness
outerY2:=h+2*border_thickness
innerX:=border_thickness
innerY:=border_thickness
innerX2:=border_thickness+w
innerY2:=border_thickness+h
newX:=x-border_thickness
newY:=y-border_thickness
newW:=w+2*border_thickness
newH:=h+2*border_thickness
} else if (borderType="inside") {
WinGet, myState, MinMax, A
if (myState=1)
offset:=8
else
offset:=0
outerX:=offset
outerY:=offset
outerX2:=w-offset
outerY2:=h-offset
innerX:=border_thickness+offset
innerY:=border_thickness+offset
innerX2:=w-border_thickness-offset
innerY2:=h-border_thickness-offset
newX:=x
newY:=y
newW:=w
newH:=h
} else if (borderType="both") {
outerX:=0
outerY:=0
outerX2:=w+2*border_thickness
outerY2:=h+2*border_thickness
innerX:=border_thickness*2
innerY:=border_thickness*2
innerX2:=w
innerY2:=h
newX:=x-border_thickness
newY:=y-border_thickness
newW:=w+4*border_thickness
newH:=h+4*border_thickness
}
Gui, Color, %border_color%
Gui, -Caption
;WinSet, Region, 0-0 %w%-0 %w%-%h% 0-%h% 0-0 %border_thickness%-%border_thickness% %iw%-%border_thickness% %iw%-%ih% %border_thickness%-%ih% %border_thickness%-%border_thickness%
WinSet, Region, %outerX%-%outerY% %outerX2%-%outerY% %outerX2%-%outerY2% %outerX%-%outerY2% %outerX%-%outerY% %innerX%-%innerY% %innerX2%-%innerY% %innerX2%-%innerY2% %innerX%-%innerY2% %innerX%-%innerY%
;Gui, Show, w%w% h%h% x%x% y%y% NoActivate, Table awaiting Action
Gui, Show, w%newW% h%newH% x%newX% y%newY% NoActivate, Table awaiting Action
return
另外,僅供參考,還有一些方法可以掛鉤視窗移動事件的視窗回呼事件,這樣您就不必使用始終更新的 50 毫秒計時器。在這種情況下,它只會在視窗移動時更新,並且計時器會帶來明顯的延遲和處理開銷。但計時器是迄今為止更容易實現的解決方案(正如您已經完成的那樣),在許多情況下,如果您不需要,那麼並不值得額外複雜地掛鉤視窗事件。只是讓你知道這是可能的。
答案2
不要修正你的腳本,而是使用這個完全符合你想要的功能的程式碼。
#Requires AutoHotkey 2.0+
#SingleInstance Force
SetTimer(DrawBorder,100)
DrawBorder(){
Static OS:=3
Static BG:="FF0000"
Static myGui:=Gui("+AlwaysOnTop +ToolWindow -Caption","GUI4Border")
myGui.BackColor:=BG
WA:=WinActive("A")
If WA && !WinGetMinMax(WA) && !WinActive("GUI4Border ahk_class AutoHotkeyGUI"){
WinGetPos(&wX,&wY,&wW,&wH,WA)
myGui.Show("x" wX " y" wY " w" wW " h" wH " NA")
Try WinSetRegion("0-0 " wW "-0 " wW "-" wH " 0-" wH " 0-0 " OS "-" OS " " wW-OS
. "-" OS " " wW-OS "-" wH-OS " " OS "-" wH-OS " " OS "-" OS,"GUI4Border")
}Else{
myGui.Hide()
Return
}
}