Right-align buttons on GUI

Get help with using AutoHotkey and its commands and hotkeys
StefOnSteroids
Posts: 63
Joined: 08 Aug 2015, 10:22

Right-align buttons on GUI

24 Apr 2018, 09:57

I am looking for a way to right-align buttons (OK, Cancel) on a GUI-form. To make them float to the bottom right of the form. The way it looks on native Windows dialogs.

I know I can "hard-wire" the distances to make it look right-aligned within a given form size. But that's not what I want. Every time the user resizes the dialog or changes fonts/ sizes, the button alignment is out of whack again. How do I line them up in a flexible way to the bottom right of the form? Tried entering negative values, but no luck. :cry:
swagfag
Posts: 2481
Joined: 11 Jan 2017, 17:59

Re: Right-align buttons on GUI

24 Apr 2018, 10:20

you need to programmatically do it inside guisize

https://autohotkey.com/docs/commands/Gui.htm#GuiSize
Odlanir
Posts: 336
Joined: 20 Oct 2016, 08:20

Re: Right-align buttons on GUI

24 Apr 2018, 12:45

This should work

Code: Select all

Gui, +hwndGui1
Gui, +resize
gui, font, s15 ; try to change the font size 
Gui, add, button, w100 hwndBut1 vBut1 section, Ok 
Gui, add, button, w100 hwndBut2 vBut2 ys, Cancel
Gui, show, w300 h100
return

GuiSize:
	gosub doTheJob
return

doTheJob:	
	rightMargin := 20, bottomMargin := 50
	WinGetPos, x,y,w,h, ahk_id %Gui1%
	ControlGetPos,,,w1,h1,,ahk_id %But1%
	ControlGetPos,,,w2,h2,,ahk_id %But2%
	GuiControl, move, But1, % "x" w-(w1+w2+rightMargin) " y" h-(h1+bottomMargin)
	GuiControl, move, But2, % "x" w-(w2+rightMargin)    " y" h-(h2+bottomMargin)
return

GuiClose:
GuiEscape:
    ExitApp
____________________________________________________________________________
Windows 7 Home Premium 64 bit - Autohotkey v1.1.24.02 64-bit Unicode
StefOnSteroids
Posts: 63
Joined: 08 Aug 2015, 10:22

Re: Right-align buttons on GUI

24 Apr 2018, 13:11

Thank you, guys.
If I read you correctly, there is no simple, automatic way to right-align buttons inside the form? :wtf:
I always have to calculate the distances to manually push them into the right bottom corner - then include extra code that intercepts or rather re-calculates all necessary distances to make it appear right-aligned again?

Special Thanks to Odlamir for providing the code. :bravo:
There is no way I would have ever figured this out just by reading the manual. The example works quite nicely, although it's not exactly proportional, the borders touch into the buttons when the GUI is resized to large values. But I can live with that. Still W-A-Y better than my desperate attempts.
hunter99
Posts: 118
Joined: 20 Jan 2014, 17:57

Re: Right-align buttons on GUI

24 Apr 2018, 17:54

Hi Stef,
This should fix the borders touching problem. Also stops the shadow copies of the buttons when sizing the Gui fast.
I just modified Odlanir's script, which made it easy for me. Added a few comments to it.
hunter99

edit put it in code display:

Code: Select all

Gui, +hwndGui1  -DPIScale	;-DPIScale added, keeps borders off of buttons 
Gui, +resize
gui, font, s15 ; try to change the font size 
Gui, add, button, w100 hwndBut1 vBut1 section, Ok 
Gui, add, button, w100 hwndBut2 vBut2 ys, Cancel	;places button to right of section
Gui, show, w300 h100
return

GuiSize:		;puts the code right with the label that uses it
	rightMargin := 20, bottomMargin := 50
	WinGetPos, x,y,w,h, ahk_id %Gui1%		;reordering next 4 lines stops most artifacts shown when draging to resize
    ControlGetPos,,,w2,h2,,ahk_id %But2%		;get right button position first, as will be referanced to rightMargin
	ControlGetPos,,,w1,h1,,ahk_id %But1%
	GuiControl, move, But2, % "x" w-(w2+rightMargin)    " y" h-(h2+bottomMargin)	;move right button first, same reason as above
	GuiControl, move, But1, % "x" w-(w1+w2+rightMargin) " y" h-(h1+bottomMargin)
return

GuiClose:
GuiEscape:
    ExitApp
User avatar
Alguimist
Posts: 324
Joined: 05 Oct 2015, 16:41
Contact:

Re: Right-align buttons on GUI

24 Apr 2018, 18:40

In AutoGUI, right click the control and select Properties. In the "Anchor" group box, check X and Y.
StefOnSteroids
Posts: 63
Joined: 08 Aug 2015, 10:22

Re: Right-align buttons on GUI

26 Apr 2018, 12:05

Thanks a ton, hunter99,

your modifed code comes close to perfect, the buttons behaving nicely, aligned exactly the way I was hoping for.

Lots of new stuff for me to learn and look up, from DPI-Scale to using the GUISize label - which caused quite some headscratching in the beginning as to why it works in the first, place, but I think I got it figured out by now.
Your commenting the code also helped a lot. :D

Many thanks, I really appreciate it.
hunter99
Posts: 118
Joined: 20 Jan 2014, 17:57

Re: Right-align buttons on GUI

28 Apr 2018, 00:04

Hi Stef,
Glad it was of help. I was looking at the code I posted when it hit me that I could remove some useless code.
I also wanted to test again the the reordering of the code in "GuiSize:" , this was to stop the leaving part of one buttons image on the other.
This would happen when resized repeatedly and rapidly. This would help some, but not as much as I first thought. When all else fails RTFM.
So looking at "GuiControl, Move" in the Docs. , I see right below it "GuiControl, MoveDraw". The part that stood out was this
"also repaints the region of the GUI window occupied by the control". Well that figures because putting the curser on the button got rid of
the artifacts, as displaying the curser will redraw the image. Changed "GuiControl, Move" to "GuiControl, MoveDraw". All fixed.
Here is my previous post with changes and comments

Code: Select all

Gui, +hwndGui1 +resize -DPIScale	;-DPIScale added, keeps borders off of buttons 
Gui, font, s15 						;Change the font size 
Gui, add, button, w75 h25 gkey1 hwndBut1, Ok                    ;Removed "section" as the y is relative to the bottomMargin.
Gui, add, button, w75 h25 gkey2 hwndBut2, Cancel 				;Removed "ys" as the y is relative to the bottomMargin.
Gui, font,											;Restore the font to the system's default GUI typeface, size, and color.
Gui, show, w300 h100
return

GuiSize:		;Launched when the window is resized, minimized, maximized, or restored.
	rightMargin := 20, bottomMargin := 50
	WinGetPos, x,y,w,h, ahk_id %Gui1%
    ControlGetPos,,,w1,h1,,ahk_id %But1%
	ControlGetPos,,,w2,h2,,ahk_id %But2%
	GuiControl, movedraw, %But1%, % "x" w-(w1+w2+rightMargin) " y" h-(h1+bottomMargin)	;"MoveDraw" solves painting artifacts 
	GuiControl, movedraw, %But2%, % "x" w-(w2+rightMargin)    " y" h-(h2+bottomMargin)		;when resized repeatedly and rapidly.
	return

GuiClose:
GuiEscape:
    ExitApp
	

	key1:		;Do some work
	msgbox,,, You clicked the Ok button.
	return
	
	key2:
	msgbox,,, You clicked the Cancel button.
	return
Alguimist posted here how to anchor the buttons using his fine editor AutoGUI. You can find the link to it here:
https://autohotkey.com/docs/commands/Edit.htm#Editors
I had used it a few years ago never even noticing the "Anchor" group box, and this was an older version I have v.1.1.3.7.
So grabbed his latest version 2.2.5, which uses a newer Lib. "AutoXYWH.ahk". Thank You to tmplinshi and toralf for that.
I installed AutoGUI, it's just a matter of extracting the zip file to a folder of your choice. I have one called Util in my C drive. In a couple of minutes a visual copy
of the Gui that was typed by hand was on the screen. Only typing required here was the window size and name, text on one button. Everything else was just mouse clicks.
I hit the Execute button in AutoGUI and tried resizing repeatedly and rapidly, and got the artifacts as with the handmade Gui before using "GuiControl, MoveDraw".
When all fails-READ, so looking at the source code for Lib. "AutoXYWH.ahk", there was the answer almost right at the top. A big Thank You to tmplinshi and toralf for this.
Read the comments in the example below to see how simple it was to fix.

Code: Select all

;From the comment "Generated by" until the comment after "ExitApp" the code is all 
;made by AutoGUI, except my added changes, removes, or comments which are marked h99. 
;All changes, removes, or comments are done in an editor after saving the AutoGUI script there.
;I used Notepad ++, but any editor you normaly use is Ok.
;The comment "Do not edit above this line"  means what it says, because if you 
;then make any changes using AutoGUI's "Preview Window" those edits are lost. 

; Generated by AutoGUI 2.2.5a
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

#Include %A_ScriptDir%\AutoXYWH.ahk

Gui +Resize		; No -DPIScale needed here as AutoXYWH.ahk takes care of the problem. (comment added h99)
Gui Font, s10, Tahoma
Gui Add, Button, hWndhButton1 gkey1 x219 y73 w80 h23, &Cancel
;Gui Font							;redundant, so removed  h99
;Gui Font, s10, Tahoma				;redundant, so removed  h99
Gui Add, Button, hWndhButton2 gkey2 x136 y74 w80 h23, &OK
Gui Font	;Restores the font to the system's default GUI typeface, size, and color. (comment added h99)

Gui Show, w300 h100, Gui Test 1
Return

GuiSize:
    If (A_EventInfo == 1) {
        Return
    }

    AutoXYWH("xy*", hButton1)	;Added a * to xy to use MoveDraw instead of Move,   h99
    AutoXYWH("xy*", hButton2)	;solves painting artifacts when resized repeatedly and rapidly.  h99
Return

GuiEscape:
GuiClose:
    ExitApp

; Do not edit above this line

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx h99
;This is all added by me in Notepad++ just to show the keys doing something.  h99    x
;																					 x
	key1:																			;x
	msgbox,,, You clicked the Cancel button.										;x
	return																			;x
																					;x    
    key2:		;Do some work														;x
	msgbox,,, You clicked the Ok button.											;x
	return																			;x
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx h99	
Everyone should have a copy of AutoGUI on hand. It is a real time saver. There are some real gems in the Tools folder, one of which is MagicBox.
That one I have been using for quite a while, it saves me from looking up alot of code. Alguimist, WELL DONE and a BIG THANK YOU

hunter99

Return to “Ask For Help”

Who is online

Users browsing this forum: Gangrel, Google [Bot], Rohwedder and 141 guests