Deye

Relative GUI centering

5 posts in this topic

#1 ·  Posted (edited)


Hi,

What could be a better formula for switching between GUIs, center to each other more accurately , especially when either GUIs are back on the screen (note the slight shift in position)

of course, just use the close-[x] to test 

Thanks

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

$hGUI1 = GUICreate("GUI 1", 500, 220, -1, -1, BitOR($WS_EX_APPWINDOW, $WS_EX_TOPMOST))

GUISetState(@SW_SHOW)
Global $aClient1 = WinGetClientSize($hGUI1, '')

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            _start_Gui2()
    EndSwitch
WEnd

Func _start_Gui2()
    Local $aClient2, $aPOS = WinGetPos($hGUI1, '')
    GUISetState(@SW_HIDE, $hGUI1)
    GUISetState(@SW_DISABLE, $hGUI1)

    $hGUI2 = GUICreate("GUI 2", 340, 600, ($aClient1[0] / 2 + $aPOS[0]) - (340 / 2), ($aClient1[1] / 2 + $aPOS[1]) - (600 / 2), BitOR($WS_EX_APPWINDOW, $WS_EX_TOPMOST))
    GUISetState(@SW_SHOW)
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                $aPOS = WinGetPos($hGUI2, '')
                $aClient2 = WinGetClientSize($hGUI2, '')
                GUISetState(@SW_ENABLE, $hGUI1)
                WinMove($hGUI1, '', ($aClient2[0] / 2 + $aPOS[0]) - ($aClient1[0] / 2), ($aClient2[1] / 2 + $aPOS[1]) - ($aClient1[1] / 2))
                GUISetState(@SW_SHOW, $hGUI1)
                ExitLoop
        EndSwitch
    WEnd
    GUIDelete($hGUI2)
EndFunc   ;==>_start_Gui2

 

Edited by Deye

Share this post


Link to post
Share on other sites



#2 ·  Posted

I have used something like this in the past, well, I still do, they maybe of use

centering

Func _OIG_CentreGUI($h_Parent, $h_Child)
    Local $ai_ParentPos = WinGetPos($h_Parent, '') ; get the position of the parent gui
    Local $a_ViewerGUISize = WinGetClientSize($h_Child, '') ; get the size of the viewer gui
    Local $i_Xcord = $ai_ParentPos[0] + ($ai_ParentPos[2] / 2) - ($a_ViewerGUISize[0] / 2)
    Local $i_Ycord = $ai_ParentPos[1] + ($ai_ParentPos[3] / 2) - ($a_ViewerGUISize[1] / 2)

    WinMove($h_Child, '', $i_Xcord, $i_Ycord) ; move the child gui to the centre of the parent gui
EndFunc   ;==>_OIG_CentreGUI

When switching between GUIs

Func _OIG_SwitchGUI($h_NewGUI, $i_EventMode = 0, $i_DisableGUI = 0, $h_OldGUI = '') ; Change current GUI ($hwdGUI = GUI to change to, $i_EventMode = Set GUIOnEventMode option 0/1, $iDisableMain = Disable the main OI GUI 0/1)
    Opt('GUIOnEventMode', $i_EventMode) ; Set on event mode.
    GUISwitch($h_NewGUI) ; Switch control to the specified GUI.
    GUISetState(@SW_ENABLE, $h_NewGUI)

    If $i_DisableGUI Then GUISetState(@SW_DISABLE, $h_OldGUI) ; Disable the main GUI.
EndFunc   ;==>_OIG_SwitchGUI

 

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks,I think your formula is the same as mine ..
Only, when either GUIs are back on the screen I'm getting a notable mis-position

hold down escape for 2 seconds to see the drift

Edited by Deye

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The problem seems to be with the second GUI's styles

BitOR($WS_EX_APPWINDOW, $WS_EX_TOPMOST)

It is throwing the calculations out. If you remove the styles for the second GUI it works. You just need to find out how to adjust for the styles.

If it's an option you could create both GUIs first then show and hide as required

Edited by benners
1 person likes this

Share this post


Link to post
Share on other sites

#5 ·  Posted

Nice find ,Good to see it works now, Thanks again

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