Control Control -- Interactive Control Manipulation

Post your working scripts, libraries and tools for AHK v1.1 and older
Verdlin
Posts: 63
Joined: 04 Oct 2013, 08:55
Contact:

Control Control -- Interactive Control Manipulation

07 Oct 2013, 09:15

There probably is a script out there like this, but it wasn't easy to locate. Here's my version of a script that lets you interactively resize controls.

Edit: Check out the buddy script, Window Control!

Screenshots:
Image Image

Hotkeys:
1. Alt + LButton = Move control
2. Alt + Shift + LButton = Move control along X axis only
3. Alt + Ctrl + LButton = Move control along Y axis only
4. Alt + RButton = Resize
5. Alt + Shift + RButton = Resize control width only
6. Alt + Ctrl + RButton = Resize control height only
7. Alt + MButton OR Alt + Win + C = Change text within control
8. Alt + Shift + C: Copy control coordinates to clipboard
9. Win + LButton = Enable/Disable control

Code: Select all

#Persistent
#SingleInstance, Force

; Hotkeys
;	1. Alt + LButton = Move control
;	2. Alt + Shift + LButton = Move control along X axis only
;	3. Alt + Ctrl + LButton = Move control along Y axis only
;	4. Alt + RButton = Resize
;	5. Alt + Shift + RButton = Resize control width only
;	6. Alt + Ctrl + RButton = Resize control height only
;	7. Alt + MButton OR Alt + Win + C = Change text within control
;	8. Alt + Shift + C: Copy control coordinates to clipboard
;	9. Win + LButton = Enable/Disable control

Menu, TRAY, NoStandard
Menu, TRAY, MainWindow ; For compiled scripts
Menu, TRAY, Add, &Reload, Reload
Menu, TRAY, Add, E&xit, Exit

CoordMode, Mouse
SetControlDelay, -1

return ; End auto-execute

Reload:
	Reload
Exit:
	ExitApp

#!c::
{
	gosub ChangeControlLabel
	return
}

; Simple hotkeys like ~Alt & ~LButton cannot be used becuase this does not disable clicks inside of windows
Shift & ~Alt::
Ctrl & ~Alt::
~Alt::
{
	Hotkey, *LButton, Alt_And_LButton, On
	Hotkey, *RButton, Alt_And_RButton, On
	Hotkey, *MButton, ChangeControlLabel, On
	KeyWait, Alt
	Hotkey, *LButton, Off
	Hotkey, *RButton, Off
	Hotkey, *MButton, Off

	if (A_ThisHotkey = "*LButton")
		gosub Alt_And_LButton
	else if (A_ThisHotkey = "*RButton")
		gosub Alt_And_RButton
	else if (A_ThisHotkey = "*MButton")
		gosub ChangeControlLabel

	return
}

Alt_And_LButton:
{
	iPrevMouseX := iPrevMouseY := A_Blank
	MouseGetPos,,,, hCtrl, 2

	while (GetKeyState("Alt", "P") && GetKeyState("LButton", "P"))
	{
		bIgnoreX := GetKeyState("Ctrl", "P")
		bIgnoreY := GetKeyState("Shift", "P")

		MouseGetPos, iMouseX, iMouseY
		iMouseX := iMouseX
		iMouseY := iMouseY

		ControlGetPos, iX, iY,,,, ahk_id %hCtrl%
		iXDelta := bIgnoreX ? 0 : iMouseX - (iPrevMouseX == A_Blank ? iMouseX : iPrevMouseX)
		iYDelta := bIgnoreY ? 0 : iMouseY - (iPrevMouseY == A_Blank ? iMouseY : iPrevMouseY)
		if (iXDelta <> 0 || iYDelta <> 0)
		{
			ControlMove,, iX + iXDelta, iY + iYDelta,,, ahk_id %hCtrl%
			DllCall("RedrawWindow", "Ptr", hCtrl, "Ptr", 0, "Ptr", 0, "UInt", RDW_INVALIDATE := 1 | RDW_ERASENOW := 0x200 | RDW_ERASE := 4)
		}

		iPrevMouseX := iMouseX
		iPrevMouseY := iMouseY
	}

	return
}

Alt_And_RButton:
{
	iPrevMouseX := iPrevMouseY := A_Blank
	MouseGetPos,,,, hCtrl, 2

	while (GetKeyState("Alt", "P") && GetKeyState("RButton", "P"))
	{
		bIgnoreW := GetKeyState("Ctrl", "P")
		bIgnoreH := GetKeyState("Shift", "P")

		MouseGetPos, iMouseX, iMouseY
		iMouseX := iMouseX
		iMouseY := iMouseY

		ControlGetPos,,, iW, iH,, ahk_id %hCtrl%
		iWDelta := bIgnoreW ? 0 : iMouseX - (iPrevMouseX == A_Blank ? iMouseX : iPrevMouseX)
		iHDelta := bIgnoreH ? 0 : iMouseY - (iPrevMouseY == A_Blank ? iMouseY : iPrevMouseY)
		if (iWDelta <> 0 || iHDelta <> 0)
		{
			ControlMove,,,, iW + iWDelta, iH + iHDelta, ahk_id %hCtrl%
			DllCall("RedrawWindow", "Ptr", hCtrl, "Ptr", 0, "Ptr", 0, "UInt", RDW_INVALIDATE:=1 | RDW_ERASENOW:=0x200 | RDW_ERASE:=4)
		}

		iPrevMouseX := iMouseX
		iPrevMouseY := iMouseY
	}

	return
}

ChangeControlLabel:
{
	; Turn off hotkeys so that the LButton is not responise
	Hotkey, *LButton, Off
	Hotkey, *RButton, Off
	Hotkey, *MButton, Off

	; This hotkey seems to be triggered twice every time it is activated, so g_iTimeAtThisExecution is used to prevent double-execution
	g_iTimeAtThisExecution := SubStr(A_Now, StrLen(A_Now) - 3, 4)
	if (A_ThisHotkey = "*MButton" && g_iTimeAtLastExecution != A_Blank && g_iTimeAtThisExecution - g_iTimeAtLastExecution < 1)
		return

	MouseGetPos,,,, hCtrl, 2
	ControlGetText, sExistingText,, ahk_id %hCtrl%
	InputBox, sText, Set Control Text,,,,,,,,, %sExistingText%

	g_iTimeAtLastExecution := SubStr(A_Now, StrLen(A_Now) - 3, 4)
	if (ErrorLevel)
		return

	ControlSetText,, %sText%, ahk_id %hCtrl%
	return
}

#LButton::
{
	MouseGetPos,,,, hCtrl, 2
	ControlGet, bEnabled, Enabled,,, ahk_id %hCtrl%
	Control, % bEnabled ? "Disable" : "Enable",,, ahk_id %hCtrl%
	return
}

!+C::
{
	MouseGetPos,,,, hCtrl, 2
	ControlGetText, sCtrlTxt,, ahk_id %hCtrl%
	ControlGetPos, iX, iY, iW, iH,, ahk_id %hCtrl%
	if !((iX == A_Blank || iY == A_Blank || iW == A_Blank || iH == A_Blank))
		clipboard := "Control:`t" sCtrlTxt "`nLeft:`t" iX "`nTop:`t" iY "`nRight:`t" iW "`nBottom:`t" iH
	return
}
Last edited by Verdlin on 04 Nov 2013, 12:39, edited 5 times in total.
Alibaba
Posts: 480
Joined: 29 Sep 2013, 16:15
Location: Germany

Re: Control Control -- Interactive Control Manipulation

07 Oct 2013, 09:52

Wow this is awesome!!!
Very nice to play around a bit or confuse other people. ;)
"Nothing is quieter than a loaded gun." - Heinrich Heine
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Control Control -- Interactive Control Manipulation

07 Oct 2013, 10:11

This. Is. AWESOME!!! :D
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Verdlin
Posts: 63
Joined: 04 Oct 2013, 08:55
Contact:

Re: Control Control -- Interactive Control Manipulation

04 Nov 2013, 12:39

Update:

Fixed bug where clicks were disabled after using hotkey Alt + MButton.
Added Win + LButton = Enable/Disable control.
timeFlies
Posts: 146
Joined: 22 Oct 2013, 20:54
Location: Somewhere in the northern hemisphere.

Re: Control Control -- Interactive Control Manipulation

06 Nov 2013, 08:20

This thing is absolutely AWESOME! If this also had the ability to resize multiple controls at once, that would make it even better. It's great for resizing that pesky msconfig.exe window that hasn't changed in size since Windows 95, so only like 4 lines in the listview show.
Verdlin
Posts: 63
Joined: 04 Oct 2013, 08:55
Contact:

Re: Control Control -- Interactive Control Manipulation

06 Nov 2013, 17:06

Thanks, chaz! I, too, have found myself wanting to resize multiple controls at once.

My thought is to create a way to select a range of controls with some hotkey that, of course, includes LButton (Alt+Win+LButton?). Once a selection is defined, the regular hotkeys can work to resize multiple controls proportionally.

Not sure how to visually represent selection, though. Gdip is one way to go, but perhaps overkill. It would probably look fine to just create a semi-opaque, blue-background GUI that resizes along with your mouse.

Also not sure if this will, initially, be slow. Once the selection is defined, the script will need to loop through all controls in the target window, and, using ControlGetPos, determine whether or not the control fell in the range of the mouse selected. How fast will this be for hundreds of controls?
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Control Control -- Interactive Control Manipulation

06 Nov 2013, 18:05

Just use this simplistic way, sometimes known as SelectArea()
http://www.autohotkey.com/board/topic/4 ... he-screen/
Cheers, ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: Control Control -- Interactive Control Manipulation

22 Dec 2013, 15:36

At first, I didn't know how to use this, but then, the enlightenment came:
calculator-levitation.jpg
:lol: 8-)

So this could be used i.e. on systems with high DPI settings to resize controls that got broken because of too large font. Kewl. Thanks Verdlin.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 140 guests