[AutoIt] 設定視窗的寬度與高度

[AutoIt] 設定視窗的寬度與高度

在痞客邦上發表文章時,因為我的文章版面寬度是設成 600,

因此如果有附上抓圖的圖片時,

通常會需要先把圖片寬度設成 600,不然就會被自動縮小~

問題是縮圖的話,視窗裡的有些東西就看不清楚了…

 

舉例來說,我抓了一個 Everything 的視窗,大小是 938×544,

當傳到痞客邦選擇寬度 600 時,可以看到視窗的字都被縮小了…

resize1  

 

為了避免這個問題,通常我會自己先把視窗的寬度調到 600 再來抓圖~

可是要把視窗調整到 600 很困難,我都是先抓圖看看大小,

不對就調整一下視窗再抓一次圖,直到大小正確為止,

是非常麻煩的工作…

因此決定用 AutoIt 寫一個小程式,

讓它自動在按下熱鍵時,就幫我把視窗設成指定的大小!~^^

 

在 AutoIt script 設定熱鍵的話,只要使用 HotKeySet 就可以了,

按下熱鍵後就會去執行指定的函式~

; When Ctrl-Alt-R hotkey is pressed, resize current window
HotKeySet(“^!r”, “ResizeCurrentWindow”)

 

下面附上完整的程式供參考:

Opt(“MustDeclareVars”,  1)
Opt(“TrayIconHide”,     1)

Const $CONST_SCRIPT_TITLE   = StringReplace(StringReplace(@ScriptName, “.au3”, “”), “.exe”, “”)

; Check if width/height is provided
If $CmdLine[0] < 1 Then
    ConsoleWrite(“Syntax: “ & $CONST_SCRIPT_TITLE & ” {width} [height]” & @CRLF & @CRLF & “Use 0 for width/height if you don’t want to change it.”)
    Exit
EndIf

; When Ctrl-Alt-R hotkey is pressed, resize current window
HotKeySet(“^!r”, “ResizeCurrentWindow”)

; Print out help messages
ConsoleWrite(“Press Ctrl-Alt-R to resize current window.” & @CRLF)
ConsoleWrite(“Press Ctrl-C to terminate this program…” & @CRLF & @CRLF)

; Wait for hotkey or Ctrl-C to break
While True
    Sleep(100)
WEnd

Func ResizeCurrentWindow()

    ; Get current window position
    Local $posWin = WinGetPos(“[ACTIVE]”)

    ; Use the desired width from command line parameter, if valid
    If $CmdLine[0] >= 1 And $CmdLine[1] <> 0 Then
        $posWin[2] = $CmdLine[1]
    EndIf

    ; Use the desired height from command line parameter, if valid
    If $CmdLine[0] >= 2 And $CmdLine[2] <> 0 Then
        $posWin[3] = $CmdLine[2]
    EndIf

    ; Resize window
    WinMove(“[ACTIVE]”, “”, Default, Default, $posWin[2], $posWin[3])
    ConsoleWrite(“Window [“ & WinGetTitle(“[ACTIVE]”) & “] is resized to “ & $posWin[2] & “x” & $posWin[3] & @CRLF)

EndFunc
 

 

程式執行的語法是 AutoSetWindowSize.exe {寬度} [高度],

若不想改變的部分就設 0 就可以了~ 

執行結果如下:

resize2  

 

當我開啟剛剛的 Everything 視窗,再按下 Ctrl-Alt-R 之後,視窗就被縮成 600×544 了,

可以看見因為圖片沒有被縮小,因此視窗的內容可以比較清楚~

resize3  

 

我們剛寫的程式也會印出哪些視窗被我們改變大小囉~

resize4  

 

如果你想要自己編譯這個程式的話,記得用 Console mode 來編譯~

你也可以點這裡下載原始碼和執行檔

(本頁面已被瀏覽過 1,412 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料