Jump to content

Centering Gui


GaryFrost
 Share

Recommended Posts

This will also take into account if the gui has a menu, just pass the menu boolean

Note: there's probably a way to figure out the real height of a menu, didn't

feel like trying that just yet.

you'll need to get the au3xtra.dll, if don't have it just search for it.

move your task bar to different positions and run the script it will center

#include <GuiConstants.au3>

$GuiWidth = 392
$GuiHeight = 322
$GuiLeft = 0
$GuiTop = 0

_GetGuiCentering($GuiLeft, $GuiTop, $GuiWidth, $GuiHeight)

; we have menu so turn on menu flag
;~ _GetGuiCentering($GuiLeft, $GuiTop, $GuiWidth, $GuiHeight, 1)


GUICreate("GUI Centering", $GuiWidth , $GuiHeight , $GuiLeft, $GuiTop)
;~ $MENUFILE = GUICtrlCreateMenu("&File")


GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
WEnd
Exit

Func _GetGuiCentering(ByRef $GuiLeft, ByRef $GuiTop, $GuiWidth, $GuiHeight, $i_HasMenu = 0)
    If (@Compiled And (Not FileExists(@ScriptDir & "\AU3Xtra.dll"))) Then
        FileInstall( "Au3xtra.dll", @ScriptDir & "\AU3Xtra.dll")
    EndIf
    $menuheight = 0
    If ($i_HasMenu) Then
;~ app has menu approximate size of 30
        $menuheight = 30
    EndIf
    $DeskTopWorkArea = DllCall(@ScriptDir & "\AU3Xtra.dll", _
            "int", "DesktopGetWorkArea", _
            "int_ptr", 0, _
            "int_ptr", 0, _
            "int_ptr", 0, _
            "int_ptr", 0)
    If @error Then
        MsgBox(4096, "Error occured", "Error Getting DeskTopWorkArea")
        Exit
    EndIf
    If (@DesktopWidth <> $DeskTopWorkArea[3] And $DeskTopWorkArea[1] == 0) Then
    ;~ task bar on right side
        $GuiLeft = ((@DesktopWidth - $GuiWidth) / 2) - ((@DesktopWidth - $DeskTopWorkArea[3]) / 2)
        $GuiTop = (@DesktopHeight - $GuiHeight) / 2
    ElseIf (@DesktopWidth == $DeskTopWorkArea[3] And $DeskTopWorkArea[1] <> 0) Then
    ;~ task bar on left side
        $GuiLeft = ((@DesktopWidth - $GuiWidth) / 2) + ($DeskTopWorkArea[1] / 2)
        $GuiTop = (@DesktopHeight - $GuiHeight) / 2
    ElseIf (@DesktopHeight <> $DeskTopWorkArea[4] And $DeskTopWorkArea[2] == 0) Then
    ;~ task bar on the bottom
        $GuiTop = ((@DesktopHeight- ($GuiHeight + $menuheight)) / 2) - ((@DesktopHeight - $DeskTopWorkArea[4]) / 2)
        $GuiLeft = (@DesktopWidth - $GuiWidth) / 2
    ElseIf (@DesktopHeight == $DeskTopWorkArea[4] And $DeskTopWorkArea[2] <> 0) Then
    ;~ task bar on the top
        $GuiTop = ((@DesktopHeight- ($GuiHeight + $menuheight)) / 2) + ($DeskTopWorkArea[2] / 2)
        $GuiLeft = (@DesktopWidth - $GuiWidth) / 2
    Else
    ;~ task bar hidden
        $GuiLeft = (@DesktopWidth - $GuiWidth) / 2
        $GuiTop = (@DesktopHeight - $GuiHeight) / 2
    EndIf
EndFunc  ;==>_GetGuiCentering
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

here's a DllStruct version of it

#include <GuiConstants.au3>

$GuiWidth = 392
$GuiHeight = 322
$GuiLeft = 0
$GuiTop = 0

_GetGuiCentering($GuiLeft, $GuiTop, $GuiWidth, $GuiHeight)

; we have menu so turn on menu flag
;~ _GetGuiCentering($GuiLeft, $GuiTop, $GuiWidth, $GuiHeight, 1)


GUICreate("GUI Centering", $GuiWidth , $GuiHeight , $GuiLeft, $GuiTop)
;~ $MENUFILE = GUICtrlCreateMenu("&File")


GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
WEnd
Exit

Func _GetGuiCentering(ByRef $GuiLeft, ByRef $GuiTop, $GuiWidth, $GuiHeight, $i_HasMenu = 0)
    $menuheight = 0
    If ($i_HasMenu) Then
;~ app has menu approximate size of 30
        $menuheight = 30
    EndIf
    Const $SPI_GETWORKAREA = 48

    Local $struct = "int;int;int;int"
    Local $left = 1
    Local $top = 2
    Local $right = 3
    Local $bottom = 4
    Local $p, $ret
    $p = DllStructCreate ($struct)
    If @error Then
         MsgBox(0,"","Error in DllStructCreate " & @error);
         Exit
    EndIf
    $ret = DllCall("user32.dll","long","SystemParametersInfo","long",$SPI_GETWORKAREA,"long",0,"ptr",DllStructPtr ($p),"long",0)
    If ($ret[0] == 0) Then
        DllStructFree ($p)
        MsgBox(0,"","Error in DllCall " & $ret[0]);
        Exit
    EndIf
    Local $DeskTopWorkArea = StringSplit(DllStructGet ($p, $left) & "," & DllStructGet ($p, $top) & "," & DllStructGet ($p, $right) & "," & DllStructGet ($p, $bottom), ",")
    DllStructFree ($p)
    If (@DesktopWidth <> $DeskTopWorkArea[3] And $DeskTopWorkArea[1] == 0) Then
    ;~ task bar on right side
        $GuiLeft = ((@DesktopWidth - $GuiWidth) / 2) - ((@DesktopWidth - $DeskTopWorkArea[3]) / 2)
        $GuiTop = (@DesktopHeight - $GuiHeight) / 2
    ElseIf (@DesktopWidth == $DeskTopWorkArea[3] And $DeskTopWorkArea[1] <> 0) Then
    ;~ task bar on left side
        $GuiLeft = ((@DesktopWidth - $GuiWidth) / 2) + ($DeskTopWorkArea[1] / 2)
        $GuiTop = (@DesktopHeight - $GuiHeight) / 2
    ElseIf (@DesktopHeight <> $DeskTopWorkArea[4] And $DeskTopWorkArea[2] == 0) Then
    ;~ task bar on the bottom
        $GuiTop = ((@DesktopHeight- ($GuiHeight + $menuheight)) / 2) - ((@DesktopHeight - $DeskTopWorkArea[4]) / 2)
        $GuiLeft = (@DesktopWidth - $GuiWidth) / 2
    ElseIf (@DesktopHeight == $DeskTopWorkArea[4] And $DeskTopWorkArea[2] <> 0) Then
    ;~ task bar on the top
        $GuiTop = ((@DesktopHeight- ($GuiHeight + $menuheight)) / 2) + ($DeskTopWorkArea[2] / 2)
        $GuiLeft = (@DesktopWidth - $GuiWidth) / 2
    Else
    ;~ task bar hidden
        $GuiLeft = (@DesktopWidth - $GuiWidth) / 2
        $GuiTop = (@DesktopHeight - $GuiHeight) / 2
    EndIf
EndFunc  ;==>_GetGuiCentering
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...