Code: Select all
#Persistent
SetTitleMatchMode, 2
WinTitle := "Notepad" ; target window title criteria
WinWidth := 650 ; initial window width
WinHeight := 650 ; initial window height
MinWidth := 500 ; minimum window width
MinHeight := 500 ; minumum window height
MaxWidth := 800 ; maximum window width
MaxHeight := 800 ; maximum window height
WinGet, PID, PID, %WinTitle%
if (!PID)
ExitApp
WinActivate, ahk_pid %PID%
WinMove,A,,,,%WinWidth%,%WinHeight%
DllCall("CoInitialize", "uint", 0)
hWinEventHook := DllCall("SetWinEventHook", "uint", 0xA, "uint", 0xB, "uint", 0, "uint", RegisterCallback("HookProc"), "uint", PID, "uint", 0, "uint", 0)
ResizeStart:
GoSub, Resizing
return
ResizeEnd:
IfWinActive, ahk_pid %PID%
{
if (WinHeight < MinHeight)
WinMove,A,,,,,%MinHeight%
else
if (WinWidth < MinWidth)
WinMove,A,,,,%MinWidth%,
if (WinHeight > MaxHeight)
WinMove,A,,,,,%MaxHeight%
else
if (WinWidth > MaxWidth)
WinMove,A,,,,%MaxWidth%,
}
return
Resizing:
IfWinActive, ahk_pid %PID%
{
loop
{
WinGetPos, X, Y, WinWidth, WinHeight, A
if (WinHeight < MinHeight or WinWidth < MinWidth or WinHeight > MaxHeight or WinWidth > MaxWidth)
{
MouseClick, Left,,,,,U
break
}
}
}
return
HookProc(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
if (event == 10)
Gosub, ResizeStart
else if (event == 11)
Gosub, ResizeEnd
}