Sign in to follow this  
Followers 0
MyEarth

Center and Resize Window on Screen

9 posts in this topic

#1 ·  Posted (edited)

From the snippet:

https://www.autoitscript.com/wiki/Snippets_(_GUI_)#Center_Window_on_Screen

#include <GUIConstantsEx.au3>

Global Const $GUI = GUICreate("Test Window",300 ,300 ,100 ,100)
GUISetState(@SW_SHOWNORMAL)

Sleep(2000)

_Middle($GUI, "Test Window")

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

Func _Middle(Const $win, Const $txt)
    Local Const $size = WinGetClientSize($win, $txt)
    Local Const $y = (@DesktopHeight / 2) - ($size[1] / 2)
    Local Const $x = (@DesktopWidth / 2) - ($size[0] / 2)
    Return WinMove($win, $txt, $x, $y)
EndFunc  ;==>_Middle

Pratically i want to add the the _Middle function the ability to resize AND put on the center. Using two different WinMove is easy but the result does not suit to me. I'd like to do the operation with only one WinMove.

Thank you

Edited by MyEarth

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Bad moon today? Yes spoon-feed me with your superior knowledge mr.programmer...

Again, i have changed that function with 2 WinMove, resize the window and use _Middle, and i like to know if is possible to only one WinMove or is necessary to resize first.

Edited by MyEarth

Share this post


Link to post
Share on other sites

I have changed that function with 2 WinMove, resize the window and use _Middle

So, would you like to share those efforts ?


My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites

I'll post it since it is so required, dunno why but ok.

#include <GUIConstantsEx.au3>

Global $GUI = GUICreate("Test Window", 100, 100, 100, 100)
GUISetState(@SW_SHOW)

Sleep(2000)

_Middle2($GUI, "Test Window", 300, 300)

; just for test if is correct centered like the original one <<<<<<<<<<<<<<<<<<<<<
$size = WinGetPos($GUI)
ConsoleWrite("Active window stats (x,y,width,height):" & $size[0] & " " & $size[1] & " " & $size[2] & " " & $size[3] & @CRLF)

Sleep(1000)

_Middle($GUI, "Test Window")
$size = WinGetPos($GUI)
ConsoleWrite("Active window stats (x,y,width,height):" & $size[0] & " " & $size[1] & " " & $size[2] & " " & $size[3] & @CRLF)
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

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

Func _Middle2($win, $txt, $width, $height)
    Local $aWin = WinGetPos($win)
    WinMove($win, $txt, $aWin[0], $aWin[1], $width, $height) ; first winmove
    Local $size = WinGetClientSize($win, $txt)
    Local $y = (@DesktopHeight / 2) - ($size[1] / 2)
    Local $x = (@DesktopWidth / 2) - ($size[0] / 2)
    Return WinMove($win, $txt, $x, $y) ; second winmove
EndFunc   ;==>_Middle

Func _Middle(Const $win, Const $txt)
    Local Const $size = WinGetClientSize($win, $txt)
    Local Const $y = (@DesktopHeight / 2) - ($size[1] / 2)
    Local Const $x = (@DesktopWidth / 2) - ($size[0] / 2)
    Return WinMove($win, $txt, $x, $y) ; second winmove
EndFunc   ;==>_Middle

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Looks fine to me...switched out one function call...and no need for _Middle anymore:

#include <GUIConstantsEx.au3>

Global $GUI = GUICreate("Test Window", 100, 100, 100, 100)
GUISetState(@SW_SHOW)

Sleep(2000)


_Middle2($GUI, "Test Window", 300, 300)
; just for test if is correct centered like the original one <<<<<<<<<<<<<<<<<<<<<
$size = WinGetPos($GUI)
ConsoleWrite("Active window stats (x,y,width,height):" & $size[0] & " " & $size[1] & " " & $size[2] & " " & $size[3] & @CRLF)

Sleep(1000)
_Middle2($GUI, "Test Window", 100, 100)
$size = WinGetPos($GUI)
ConsoleWrite("Active window stats (x,y,width,height):" & $size[0] & " " & $size[1] & " " & $size[2] & " " & $size[3] & @CRLF)
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

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

Func _Middle2($win, $txt, $width, $height)
    Local $aWin = WinGetPos($win)
    WinMove($win, $txt, $aWin[0], $aWin[1], $width, $height) ; first winmove
    Local $size = WinGetClientSize($win, $txt)
    Local $y = (@DesktopHeight / 2) - ($size[1] / 2)
    Local $x = (@DesktopWidth / 2) - ($size[0] / 2)
    Return WinMove($win, $txt, $x, $y) ; second winmove
EndFunc   ;==>_Middle

Edit: small improvement:

#include <GUIConstantsEx.au3>

Global $GUI = GUICreate("Test Window", 100, 100, 100, 100)
GUISetState(@SW_SHOW)

Sleep(2000)


_Middle2($GUI, "Test Window", 300, 300)
; just for test if is correct centered like the original one <<<<<<<<<<<<<<<<<<<<<
$size = WinGetPos($GUI)
ConsoleWrite("Active window stats (x,y,width,height):" & $size[0] & " " & $size[1] & " " & $size[2] & " " & $size[3] & @CRLF)

Sleep(1000)
_Middle2($GUI, "Test Window",800)
$size = WinGetPos($GUI)
ConsoleWrite("Active window stats (x,y,width,height):" & $size[0] & " " & $size[1] & " " & $size[2] & " " & $size[3] & @CRLF)

Sleep(1000)
_Middle2($GUI, "Test Window",Default,1000)
$size = WinGetPos($GUI)
ConsoleWrite("Active window stats (x,y,width,height):" & $size[0] & " " & $size[1] & " " & $size[2] & " " & $size[3] & @CRLF)

Sleep(1000)
; change back
_Middle2($GUI, "Test Window", 100, 100)
$size = WinGetPos($GUI)
ConsoleWrite("Active window stats (x,y,width,height):" & $size[0] & " " & $size[1] & " " & $size[2] & " " & $size[3] & @CRLF)
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

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

Func _Middle2($win, $txt, $width=-1, $height=-1)
    If $width >= 0 Or $height >= 0 Then
        Local $aWin = WinGetPos($win)
        If $width < 0 Then $width = $aWin[2]
        If $height < 0 Then $height = $aWin[3]
        WinMove($win, $txt, $aWin[0], $aWin[1], $width, $height) ; first winmove
    EndIf
    Local $size = WinGetClientSize($win, $txt)
    Local $y = (@DesktopHeight / 2) - ($size[1] / 2)
    Local $x = (@DesktopWidth / 2) - ($size[0] / 2)
    Return WinMove($win, $txt, $x, $y) ; second winmove
EndFunc   ;==>_Middle
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

jdelaney i know it work but the question is always the same:

Is possible to use only one WinMove for resizing AND center instead of two, one for resize and one for center?

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Oh, that's what you wanted?  sure...just needs more calcs before hand.

Give it a go, we can correct your problems.

Had a bug in my script, so providing your needed func:

Func _Middle2($win, $txt, $width=Default, $height=Default)
    Local $size = WinGetClientSize($win, $txt)
    If Not ($width = Default) Then $size[0] = $width
    If Not ($height = Default) Then $size[1] = $height
    Local $x = (@DesktopWidth / 2) - ($size[0]/ 2)
    Local $y = (@DesktopHeight / 2) - ($size[1]/ 2)
    Return WinMove($win, $txt, $x, $y, $size[0], $size[1]) ; second winmove
EndFunc   ;==>_Middle
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

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