Jump to content

_WinSetClientPos


Mat
 Share

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 $iDefStyle = BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU)
    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)
    Else
        _WinAPI_AdjustWindowRectEx($rect, $iStyle, $iExStyle, False)
    EndIf

    $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)
    EndIf

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

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

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

    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.

Matt

Edit: Requires beta for new WinAPI functions.

Edited by Mat
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...