Sign in to follow this  
Followers 0
Sori

How to center a Window?

5 posts in this topic

#1 ·  Posted (edited)

Can someone explain what's going wrong with this code?

You open the program.

You click on the window.

You click "center window"

The window moves, but while it looks centered on the X axis, there is a sizeable difference above the window than below it. (It's too low on the Y axis)

--

or recommend another program for the same effect, I keep finding carpentry things when looking up centering a window.

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

Global $class1, $class2, $msg
Global $g_tStruct = DllStructCreate($tagPOINT) ; Create a structure that defines the point to be checked.
Global $hWnd;, $windowSize
Local $hDLL = DllOpen("user32.dll")
$main = GUICreate("", 350, 50, 1200, 750, -1, BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
$lbClass = GUICtrlCreateLabel("", 5, 5, 340, 25)
$btnCenter = GUICtrlCreateButton("center window", 5, 25)
GUISetState(@SW_SHOW)

While 1
    Position() ; Update the X and Y elements with the X and Y co-ordinates of the mouse.
    $hWnd = _WinAPI_WindowFromPoint($g_tStruct) ; Retrieve the window handle.
    If _IsPressed("01", $hDLL) Then
        $class1 = _WinAPI_GetClassName($hWnd)
        If $class1 <> "AutoIt V3 GUI" And $class1 <> "button" Then
            If $class1 <> $class2 Then
                GUICtrlSetData($lbClass, WinGetTitle(""))
                $class2 = WinGetTitle("")
            EndIf
        EndIf
    EndIf
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $btnCenter
            Center()
    EndSwitch
    Sleep(10)
WEnd

Func Center()
    Local $newX, $newY
    $windowSize = WinGetClientSize($class2, "")
    $newX = (@DesktopWidth - $windowSize[0]) / 2
    $newY = (@DesktopHeight - $windowSize[1]) / 2
    WinMove($class2, "", $newX, $newY)
EndFunc   ;==>Center

Func Position()
    DllStructSetData($g_tStruct, "x", MouseGetPos(0))
    DllStructSetData($g_tStruct, "y", MouseGetPos(1))
EndFunc   ;==>Position
Edited by Sori

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Share this post


Link to post
Share on other sites



Sori,

Just don't specify the x/y positions like...

#include <GUIConstantsEx.au3>

local $gui010 = guicreate('Centered Gui',500,200)
guisetstate()

while 1
    switch guigetmsg()
        case $gui_event_close
            Exit
    EndSwitch
WEnd

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

I don't understand your advice.


If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Share this post


Link to post
Share on other sites

The client area is the inside area of a window excluding the title bar, toolbars, status bar, scroll bars. So instead of "WinGetClientSize" use "WinGetPos", which returns the position and size of a given window.

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

Global $class1, $class2, $msg
Global $g_tStruct = DllStructCreate($tagPOINT) ; Create a structure that defines the point to be checked.
Global $hWnd;, $windowSize
Local $hDLL = DllOpen("user32.dll")
$main = GUICreate("", 350, 50, 1200, 750, -1, BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
$lbClass = GUICtrlCreateLabel("", 5, 5, 340, 25)
$btnCenter = GUICtrlCreateButton("center window", 5, 25)
GUISetState(@SW_SHOW)

While 1
    Position() ; Update the X and Y elements with the X and Y co-ordinates of the mouse.
    $hWnd = _WinAPI_WindowFromPoint($g_tStruct) ; Retrieve the window handle.
    If _IsPressed("01", $hDLL) Then
        $class1 = _WinAPI_GetClassName($hWnd)
        If $class1 <> "AutoIt V3 GUI" And $class1 <> "button" Then
            If $class1 <> $class2 Then
                GUICtrlSetData($lbClass, WinGetTitle(""))
                $class2 = WinGetTitle("")
            EndIf
        EndIf
    EndIf
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $btnCenter
            Center()
    EndSwitch
    Sleep(10)
WEnd

Func Center()
    Local $newX, $newY
    $windowSize = WinGetPos($class2, "")
    $newX = (@DesktopWidth - $windowSize[2]) / 2
    $newY = (@DesktopHeight - $windowSize[3]) / 2
    WinMove($class2, "", $newX, $newY)
EndFunc   ;==>Center

Func Position()
    DllStructSetData($g_tStruct, "x", MouseGetPos(0))
    DllStructSetData($g_tStruct, "y", MouseGetPos(1))
EndFunc   ;==>Position

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

If your purpose is to create a new window that is centered than do not specify the "left" or "top" parms in GuiCreate().  If your purpose is to center an existing window than use something like Malkey is suggesting...

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

local $gui010 = guicreate('',200,200,20,20)
local $btn010 = guictrlcreatebutton('Center',10,10,180,20)
guisetstate()

while 1
    switch guigetmsg()
        case $gui_event_close
            Exit
        case $btn010
            _center()
    EndSwitch
WEnd

func _center()

    winmove($gui010,'',(@Desktopwidth - wingetpos($gui010)[2]) / 2,(@Desktopheight - wingetpos($gui010)[3]) / 2)

endfunc
Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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