Jump to content
pixelsearch

Resize a child window without the 'disabled label' trick

Recommended Posts

Posted (edited)

Hi everybody :)
I was curious to write a little script that could automatically resize a child window ($WS_CHILD) when the parent window is resized, without using at all the "disabled label" trick found in many posts. Here is the result, which seems to work fine with only 4 ratios needed.

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

Local $iParentW = 500, $iParentH = 400, $iParentX = 100, $iParentY = 50
Local $iChildW = 200, $iChildH = 100, $iChildX = 250, $iChildY = 150

$nRatioW = $iChildW / $iParentW
$nRatioH = $iChildH / $iParentH
$nRatioX = $iChildX / $iParentW
$nRatioY = $iChildY / $iParentH

$hGUI1 = GUICreate("Resize parent -> resize child", $iParentW, $iParentH, $iParentX, $iParentY, _
    BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX), $WS_EX_COMPOSITED) ; avoid flicker

$hGUI2 = GUICreate("", $iChildW, $iChildH, $iChildX, $iChildY, $WS_CHILD, -1, $hGUI1)

Local $idLabel = GUICtrlCreateLabel("Label in child window", 0, 0, $iChildW, $iChildH, _
    BitOr($SS_CENTERIMAGE, $SS_CENTER))
GUICtrlSetBkColor(-1, 0xFFA060) ; orange
; GUICtrlSetResizing(-1, 1) ; 1 = $GUI_DOCKAUTO (no need, it's default resizing for label control)

GUIRegisterMsg($WM_SIZE, "WM_SIZE")

GUISetState(@SW_SHOW, $hGUI1)
GUISetState(@SW_SHOW, $hGUI2)

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

GUIDelete($hGUI1)
GUIDelete($hGUI2)

;==========================================
Func WM_SIZE($hWnd, $iMsg, $wParam, $lParam)
    #forceref $iMsg, $wParam

    If $hWnd = $hGUI1 Then
        Local $iParentNewCliW = BitAND($lParam, 0xFFFF) ; low word
        Local $iParentNewCliH = BitShift($lParam, 16) ; high word

        WinMove($hGUI2, "", $iParentNewCliW * $nRatioX, $iParentNewCliH * $nRatioY, _
            $iParentNewCliW * $nRatioW, $iParentNewCliH * $nRatioH) ; WinMove will use Int coords
    EndIf

    Return $GUI_RUNDEFMSG
EndFunc

Could you guys please confirm this point :
There's a $WS_EX_COMPOSITED extended style applied to the parent window, which should prevent flickering of the child window during the resizing process. Melba23 confirmed this behavior in this link (though he wrote that it worked "sometimes") but he wrote this 10 years ago and several versions of Windows were released since then.

So the question is : when you run this script with, then without this extended style, does the child window flicker when the $WS_EX_COMPOSITED extended style is not present ?
Thanks for testing.

Edit 1/5/2020 : minor change, 4 variables $iRatio... better declared as $nRatio... (they're not always integers)

Edited by pixelsearch

Share this post


Link to post
Share on other sites

Guys, many thanks for the test :thumbsup:
So it's a good idea to always keep $WS_EX_COMPOSITED in case the user's OS is Win7 (or maybe Win8) and it shouldn't hurt if his OS is Win10, very interesting !

This 1st step being done, I can now move to the 2nd step and post another script here... when it will be ready :)

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...