Control Window Sizing/Resizing - Initial/Min/Max

Post your working scripts, libraries and tools for AHK v1.1 and older
elgeeko
Posts: 1
Joined: 17 Nov 2013, 03:33

Control Window Sizing/Resizing - Initial/Min/Max

17 Nov 2013, 03:50

Geekdude showed me how this could be done using a hook, i needed to be able to control my window sizes in a script... anyways figured id share.... enjoy!

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
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 114 guests