Object Centered In Gui Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
hasantr
Posts: 151
Joined: 05 Apr 2016, 14:18

Object Centered In Gui

10 Apr 2019, 20:53

I want the textboxes I added to be horizontally aligned exactly in the window.
Note: The window size is also the screen size.

Code: Select all

Gui -ToolWindow -SysMenu -MinimizeBox -MaximizeBox -Caption
Gui,Show, h%A_ScreenHeight% w%A_ScreenWidth%,Full Screen Gui


en  := Ceil(A_ScreenWidth / 2)


Gui,Font,s30
Gui, Add, Text,x%en% y50% , Centered Text

Escape::
Gui % (MainGui:=!MainGui) ? "Hide" : "Show"
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Object Centered In Gui

10 Apr 2019, 21:14

Try Gui, Add, Text, x0 y50 w%A_ScreenWidth% Center, Centered Text.
hasantr
Posts: 151
Joined: 05 Apr 2016, 14:18

Re: Object Centered In Gui

11 Apr 2019, 00:30

Osprey wrote:
10 Apr 2019, 21:14
Try Gui, Add, Text, x0 y50 w%A_ScreenWidth% Center, Centered Text.
Aa right. :) Very nice. Thanks.
hasantr
Posts: 151
Joined: 05 Apr 2016, 14:18

Re: Object Centered In Gui  Topic is solved

11 Apr 2019, 00:55

Can you offer an alternative to align the TextBox to the center?

Code: Select all

textBoxWidth := Ceil(A_ScreenWidth / 10)
textBoxLocation := Ceil(A_ScreenWidth / 2 - textBoxWidth / 2)
ToolTip,%textBoxLocation%   %textBoxWidth%
Gui, Add, Edit, x%textBoxLocation% y50  w%textBoxWidth% Center  vPassword gPassword,

Password:

return

Escape::
ExitApp
return
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Object Centered In Gui

11 Apr 2019, 01:25

You forgot to add Gui, Show, w%A_ScreenWidth%.
hasantr
Posts: 151
Joined: 05 Apr 2016, 14:18

Re: Object Centered In Gui

11 Apr 2019, 04:44

Oh my fault.

Code: Select all

textBoxWidth := Ceil(A_ScreenWidth / 10)
textBoxLocation := Ceil(A_ScreenWidth / 2 - textBoxWidth / 2)
ToolTip,%textBoxLocation%   %textBoxWidth%
Gui, Add, Edit, x%textBoxLocation% y50  w%textBoxWidth% Center  vPassword gPassword,

Password:

return

Escape::
ExitApp
return

Code: Select all

textBoxWidth := Ceil(A_ScreenWidth / 10)
textBoxLocation := Ceil(A_ScreenWidth / 2 - textBoxWidth / 2)
This tactic is not practical. And it can cause problems. I wondered if there was a more simple direction.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Object Centered In Gui

11 Apr 2019, 05:25

If you're asking if there's a way as simple as the one that's possible with the Text control, I'm not aware of one. I could be wrong, though.
User avatar
jNizM
Posts: 2420
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Object Centered In Gui

11 Apr 2019, 05:40

another way:

Code: Select all

; GLOBAL SETTINGS ===============================================================================================================

#NoEnv
#SingleInstance Force
SetBatchLines -1

; GUI ===========================================================================================================================

Gui, 2: +Resize
Gui, 2: Add, Edit, center vSampleEdit w200, % "Sample Text"
Gui, 2: Show, w%A_ScreenWidth% h%A_ScreenHeight%, % "Sample Gui"
CenterControl(SampleEdit, 2, 50, 50)
return

; SCRIPT ========================================================================================================================

2GuiClose:
2GuiEscape:
ExitApp


; FUNCTIONS =====================================================================================================================

CenterControl(ByRef ControlID, GuiNum := 1, w := 50, h := 50, offsetx := 0, offsety := 0, method := 0) ; by just me
{
	if (WinExist("ahk_id " ControlID))
		Hwnd_Gui := DllCall("user32\GetParent", "ptr", ControlID, "ptr")
	else
		Gui, %GuiNum%: +LastFound

	VarSetCapacity(RECT, 16)
	DllCall("user32\GetClientRect", "ptr", Hwnd_Gui ? Hwnd_Gui : WinExist(), "ptr", &RECT)
	GuiW := NumGet(RECT, 8, "int")
	GuiH := NumGet(RECT, 12, "int")

	if (Hwnd_Gui)
	{
		WinGetPos,,, WinW, WinH, ahk_id %Hwnd_Gui%
		ControlGetPos, ControlIDX, ControlIDY, ControlIDW, ControlIDH,, ahk_id %ControlID%
		posx := w ? Round((WinW - ControlIDW ) / Round(100 / w, 3)) + offsetx : ControlIDX
		posy := h ? (WinH - GuiH) - (WinW - Guiw) // 2 + Round((GuiH - ControlIDH) / Round(100 / h, 3)) + offsety : ControlIDY    
		ControlMove,, posx, posy,,, ahk_id %ControlID% 
		if (method)
			WinSet, Redraw,, % "ahk_id " Hwnd_Gui
	}
	else
	{
		GuiControlGet, ControlID, %GuiNum%:Pos
		posx := w ? Round((guiw - ControlIDW) // Round(100 / w, 3)) + offsetx : ControlIDX
		posy := h ? Round((guih - ControlIDH) // Round(100 / h, 3)) + offsety : ControlIDY
		GuiControl, % method ? GuiNum . ":MoveDraw" : GuiNum . ":Move", ControlID, % "x" . posx . " y" . posy
	}
}


; ===============================================================================================================================
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1903) x64 | [GitHub] Profile
Donations are appreciated if I could help you
toralf
Posts: 602
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: Object Centered In Gui

11 Apr 2019, 14:18

I believe you an add the control just normally to a hidden GUI, get its size and location relative to the screen. Do the Math, move the control and then show the gui
ciao
toralf

Return to “Ask For Help”

Who is online

Users browsing this forum: hello9876123 and 168 guests