Jump to content


Recommended Posts

Following on from #2368.

This sets moves the window using the client, rather than the non-client, area. This will make a difference when using some styles. The function has workarounds so it will work with WS_OVERLAPPED, WS_HSCROLL and WS_VSCROLL.

I've tested it with quite a few styles, and in all cases WinGetClientSize returned the same as the input parameters.

$l and $t (left and top) can be set to default to center the window. -1 will move it so that the client is actually at -1, which is probably not as intended.

#include <WinAPISys.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>

Func _WinSetClientPos($sTitle, $sText, $w, $h, $l = Default, $t = Default)
    Local Const $iDefExStyle = $WS_EX_WINDOWEDGE

    Local $hWnd = WinGetHandle($sTitle, $sText)
    If @error Then Return SetError(1, 0, 0)

    Local $x = $l, $y = $t

    If IsKeyword($l) = $KEYWORD_DEFAULT Then $x = 100
    If IsKeyword($t) = $KEYWORD_DEFAULT Then $y = 100

    Local $iStyle = _WinAPI_GetWindowLong($hWnd, $GWL_STYLE)
    Local $iExStyle = _WinAPI_GetWindowLong($hWnd, $GWL_EXSTYLE)

    If $iStyle = -1 Then $iStyle = $iDefStyle
    If $iExStyle = -1 Then $iExStyle = $iDefExStyle

    Local $rect = DllStructCreate($tagRECT)
    DllStructSetData($rect, "left", $x)
    DllStructSetData($rect, "right", $x + $w)
    DllStructSetData($rect, "top", $y)
    DllStructSetData($rect, "bottom", $y + $h)

    If Not BitAND($iStyle, BitOR(BitAND($WS_CAPTION, BitNOT($WS_BORDER)), $WS_POPUP)) Then
        _WinAPI_AdjustWindowRectEx($rect, BitOR($iStyle, $WS_CAPTION), $iExStyle, False)
        _WinAPI_AdjustWindowRectEx($rect, $iStyle, $iExStyle, False)

    $w = DllStructGetData($rect, "right") - DllStructGetData($rect, "left")
    $h = DllStructGetData($rect, "bottom") - DllStructGetData($rect, "top")

    If BitAND($iStyle, $WS_VSCROLL) Then
        $w += _WinAPI_GetSystemMetrics($SM_CXVSCROLL)

    If BitAND($iStyle, $WS_HSCROLL) Then
        $h += _WinAPI_GetSystemMetrics($SM_CYHSCROLL)

    If IsKeyword($l) = $KEYWORD_DEFAULT Then
        $x = (@DesktopWidth - $w) / 2
        $x = DllStructGetData($rect, "left")

    If IsKeyword($t) = $KEYWORD_DEFAULT Then
        $y = (@DesktopHeight - $h) / 2
        $y = DllStructGetData($rect, "top")

    Return WinMove($hWnd, "", $x, $y, $w, $h)
Endfunc   ;==>_WinSetClientPos


An example of using this for a GUICreate wrapper:

Func _GUICreate($sTitle, $w, $h, $x = -1, $y = -1, $iStyle = -1, $iExStyle = -1, $hParent = 0)
    Local $hRet = GUICreate($sTitle, $w, $h, $x, $y, $iStyle, $iExStyle, $hParent)

    If $x = -1 Then $x = Default
    If $y = -1 Then $y = Default

    _WinSetClientPos($hRet, "", $w, $h, $x, $y)

    Return $hRet
EndFunc   ;==>_GUICreate

This is a much better way to make GUIs, especially if you are like me, and rely on maths rather than koda to layout your controls.


Edit: Requires beta for new WinAPI functions.

Edited by Mat
Link to post
Share on other sites

Good to know about _WinAPI_AdjustWindowRectEx, thanks.

Edited by FireFox


OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control


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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...