Sign in to follow this  
Followers 0
GraaF1337

Finding center of GUI

6 posts in this topic

#1 ·  Posted (edited)

I've been trying to find the center of my GUI, but it seems WinGetClientSize, dont work as i hoped :-/

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$form1 = GUICreate("Center Test", 500, 500)
GUISetState(@SW_SHOW)
WinMove($form1, "", 400, 400)
$aClientSize = WinGetClientSize($form1)

$text = GUICtrlCreateLabel("Center", $aClientSize[0] /2, $aClientSize[1] /2)

Sleep(1000)

MsgBox(0, "Resizing", "Resizing the GUI")

WinMove($form1, "", 400, 400, 450, 450)
$aClientSize = WinGetClientSize($form1)
GUICtrlSetPos($text, $aClientSize[0] /2, $aClientSize[1] /2)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

The problem is that the label's text has to be removed on the x/y, but if i do that by hand and you resize the GUI, it will be off again.

So I'm hoping you guys know a soild way of finding the center of GUI and create a label, but then calculate the label size and move it to the center.

Edited by GraaF1337

Share this post


Link to post
Share on other sites



GraaF1337,

This seems to work and I think it is clear how it does it: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$hGUI = GUICreate("Center Test", 500, 500)
GUISetState(@SW_SHOW)

$cText = GUICtrlCreateLabel("Centre", @DesktopWidth, 0)

WinMove($hGUI, "", 400, 400)

_Centre_Label($hGUI, $cText)

Sleep(1000)

MsgBox(0, "Resizing", "Resizing the GUI")

WinMove($hGUI, "", 400, 400, 450, 450)

_Centre_Label($hGUI, $cText)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func _Centre_Label($hWnd, $cID)

    $aClientSize = WinGetClientSize($hWnd)
    $aLabelSize = ControlGetPos($hWnd, "", $cID)
    ControlMove($hWnd, "", $cID, ($aClientSize[0] / 2) - ($aLabelSize[2] / 2), ($aClientSize[1] / 2) - ($aLabelSize[3] / 2))

EndFunc
M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

GraaF1337,

This seems to work and I think it is clear how it does it: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$hGUI = GUICreate("Center Test", 500, 500)
GUISetState(@SW_SHOW)

$cText = GUICtrlCreateLabel("Centre", @DesktopWidth, 0)

WinMove($hGUI, "", 400, 400)

_Centre_Label($hGUI, $cText)

Sleep(1000)

MsgBox(0, "Resizing", "Resizing the GUI")

WinMove($hGUI, "", 400, 400, 450, 450)

_Centre_Label($hGUI, $cText)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func _Centre_Label($hWnd, $cID)

    $aClientSize = WinGetClientSize($hWnd)
    $aLabelSize = ControlGetPos($hWnd, "", $cID)
    ControlMove($hWnd, "", $cID, ($aClientSize[0] / 2) - ($aLabelSize[2] / 2), ($aClientSize[1] / 2) - ($aLabelSize[3] / 2))

EndFunc
M23

 

 

There is 2-3things i dont fully understand about this.

$hGUI is the GUI.

$cText is the text which is being centered.

WinMove, just moves the GUI.

But your call function.

"_Centre_Label($hGUI, $cText)" Why the "($hGUI, $cText)" part?

And the function it self.

This part "($hWnd, $cID)"

And what is "$cID" because i can't see it being pre defined? And it's called 3times.

I'm sorry to bother you, but i like to fully understand the code i use :D

Share this post


Link to post
Share on other sites

GraaF1337,

As the code stands, both $hGUI & $cText are Global variables and so could be read inside the _Centre_Label function without problem - but that might not always be the case (in your full script you might create the GUI and label within another function for example) and so passing these variables as parameters seemed a good idea. The ($hWnd, $cID) part of the function is declaring that these variables are Local to the function (no need to pre-declare them as this is done automatically for parameters) and that they should take the values passed in the function call: ($hGUI, $cText). :)

So within the function the variables $hWnd & $cID have the same values as $hGUI & $cText, which allows the code within the function to operate on the correct GUI and control. if you had a second GUI and control, you could use the same function as long as you passed it the correct variables:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$hGUI_1 = GUICreate("Center Test", 400, 400, 100, 100)
$cText_1 = GUICtrlCreateLabel("Centre One", @DesktopWidth, 0)
GUICtrlSetResizing($cText_1, $GUI_DOCKSIZE) ; Retain size or label automatically adjusts and could be too small
GUISetState(@SW_SHOW)

$hGUI_2 = GUICreate("Center Test", 400, 400, 600, 100)
$cText_2 = GUICtrlCreateLabel("Centre Two", @DesktopWidth, 0)
GUICtrlSetResizing($cText_2, $GUI_DOCKSIZE)
GUISetState(@SW_SHOW)

_Centre_Label($hGUI_1, $cText_1)
_Centre_Label($hGUI_2, $cText_2)

Sleep(1000)

MsgBox(0, "Resizing", "Resizing the GUIs")

WinMove($hGUI_1, "", 100, 100, 300, 300)
WinMove($hGUI_2, "", 600, 100, 200, 200)

_Centre_Label($hGUI_1, $cText_1)
_Centre_Label($hGUI_2, $cText_2)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func _Centre_Label($hWnd, $cID)

    $aClientSize = WinGetClientSize($hWnd)
    $aLabelSize = ControlGetPos($hWnd, "", $cID)
    ControlMove($hWnd, "", $cID, ($aClientSize[0] / 2) - ($aLabelSize[2] / 2), ($aClientSize[1] / 2) - ($aLabelSize[3] / 2))

EndFunc
Clearer now? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

GraaF1337,

As the code stands, both $hGUI & $cText are Global variables and so could be read inside the _Centre_Label function without problem - but that might not always be the case (in your full script you might create the GUI and label within another function for example) and so passing these variables as parameters seemed a good idea. The ($hWnd, $cID) part of the function is declaring that these variables are Local to the function (no need to pre-declare them as this is done automatically for parameters) and that they should take the values passed in the function call: ($hGUI, $cText). :)

So within the function the variables $hWnd & $cID have the same values as $hGUI & $cText, which allows the code within the function to operate on the correct GUI and control. if you had a second GUI and control, you could use the same function as long as you passed it the correct variables:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$hGUI_1 = GUICreate("Center Test", 400, 400, 100, 100)
$cText_1 = GUICtrlCreateLabel("Centre One", @DesktopWidth, 0)
GUICtrlSetResizing($cText_1, $GUI_DOCKSIZE) ; Retain size or label automatically adjusts and could be too small
GUISetState(@SW_SHOW)

$hGUI_2 = GUICreate("Center Test", 400, 400, 600, 100)
$cText_2 = GUICtrlCreateLabel("Centre Two", @DesktopWidth, 0)
GUICtrlSetResizing($cText_2, $GUI_DOCKSIZE)
GUISetState(@SW_SHOW)

_Centre_Label($hGUI_1, $cText_1)
_Centre_Label($hGUI_2, $cText_2)

Sleep(1000)

MsgBox(0, "Resizing", "Resizing the GUIs")

WinMove($hGUI_1, "", 100, 100, 300, 300)
WinMove($hGUI_2, "", 600, 100, 200, 200)

_Centre_Label($hGUI_1, $cText_1)
_Centre_Label($hGUI_2, $cText_2)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func _Centre_Label($hWnd, $cID)

    $aClientSize = WinGetClientSize($hWnd)
    $aLabelSize = ControlGetPos($hWnd, "", $cID)
    ControlMove($hWnd, "", $cID, ($aClientSize[0] / 2) - ($aLabelSize[2] / 2), ($aClientSize[1] / 2) - ($aLabelSize[3] / 2))

EndFunc
Clearer now? :)

M23

 

Yea thank you! :)

Share this post


Link to post
Share on other sites

GraaF1337,

Great. :)

But when you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - I know what I wrote and it just pads the thread unnecessarily. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0