Jump to content

SetWindowRgn messes with Titlebar

Recommended Posts

Here is a strange behavior I am not sure how to fix.  When using SetWindowRgn, the title bar of the GUI changes and the minimize button stops functioning.  It also appears to be misaligned as can be seen by clicking in the area where it was prior to the titlebar changing, but it does not function.

Using the example from the help file produces the effect on my Windows 10 machine.

#include <GUIConstantsEx.au3>
#include <WinAPIGdi.au3>
#include <WinAPIHObj.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>

; get height of window title and width of window frame - may be different when XP theme is ON/OFF
Global $g_iHtit = _WinAPI_GetSystemMetrics($SM_CYCAPTION)
Global $g_iFrame = _WinAPI_GetSystemMetrics($SM_CXDLGFRAME)

Global $g_hGui = GUICreate("Test Windows regions", 350, 210)
Local $id_Default = GUICtrlCreateButton("Default region", 100, 30, 150)
Local $id_Round = GUICtrlCreateButton("Round region", 100, 60, 150)
Local $id_Buble = GUICtrlCreateButton("Buble region ", 100, 90, 150)
Local $id_Transparent = GUICtrlCreateButton("Transparent region", 100, 120, 150)
Local $id_Exit = GUICtrlCreateButton("Exit", 100, 150, 150)

Local $aPos = WinGetPos($g_hGui) ; get whole window size (no client size defined in GUICreate)
Global $g_iWidth = $aPos[2]
Global $g_iHeight = $aPos[3]

Local $iMsg, $hRgn
While 1
    $iMsg = GUIGetMsg()
        Case $iMsg = $GUI_EVENT_CLOSE Or $iMsg = $id_Exit

        Case $iMsg = $id_Default
            $hRgn = _WinAPI_CreateRectRgn(0, 0, $g_iWidth, $g_iHeight)
            _WinAPI_SetWindowRgn($g_hGui, $hRgn)

        Case $iMsg = $id_Round
            $hRgn = _WinAPI_CreateRoundRectRgn(0, 0, $g_iWidth, $g_iHeight, $g_iWidth / 3, $g_iHeight / 3)
            _WinAPI_SetWindowRgn($g_hGui, $hRgn)

        Case $iMsg = $id_Buble
            Local $hRgn1 = _WinAPI_CreateRoundRectRgn(0, 0, $g_iWidth / 2, $g_iHeight / 2, $g_iWidth / 2, $g_iHeight / 2) ; left-top
            Local $hRgn2 = _WinAPI_CreateRoundRectRgn($g_iWidth / 2, 0, $g_iWidth, $g_iHeight / 2, $g_iWidth / 2, $g_iHeight / 2) ; right-top
            _WinAPI_CombineRgn($hRgn1, $hRgn1, $hRgn2, $RGN_OR)
            $hRgn2 = _WinAPI_CreateRoundRectRgn(0, $g_iHeight / 2, $g_iWidth / 2, $g_iHeight, $g_iWidth / 2, $g_iHeight / 2) ; left-bottom
            _WinAPI_CombineRgn($hRgn1, $hRgn1, $hRgn2, $RGN_OR)
            $hRgn2 = _WinAPI_CreateRoundRectRgn($g_iWidth / 2, $g_iHeight / 2, $g_iWidth, $g_iHeight, $g_iWidth / 2, $g_iHeight / 2) ; right-bottom
            _WinAPI_CombineRgn($hRgn1, $hRgn1, $hRgn2, $RGN_OR)
            $hRgn2 = _WinAPI_CreateRoundRectRgn(10, 10, $g_iWidth - 10, $g_iHeight - 10, $g_iWidth, $g_iHeight) ; middle
            _WinAPI_CombineRgn($hRgn1, $hRgn1, $hRgn2, $RGN_OR)
            _WinAPI_SetWindowRgn($g_hGui, $hRgn1)

        Case $iMsg = $id_Transparent
            _GuiHole($g_hGui, 40, 40, 260, 170)


; make inner transparent area but add controls
Func _GuiHole($hWin, $iX, $iY, $iSizeW, $iSizeH)
    Local $hOuter_rgn, $hInner_rgn, $hCombined_rgn

    $hOuter_rgn = _WinAPI_CreateRectRgn(0, 0, $g_iWidth, $g_iHeight)
    $hInner_rgn = _WinAPI_CreateRectRgn($iX, $iY, $iX + $iSizeW, $iY + $iSizeH)
    $hCombined_rgn = _WinAPI_CreateRectRgn(0, 0, 0, 0)
    _WinAPI_CombineRgn($hCombined_rgn, $hOuter_rgn, $hInner_rgn, $RGN_DIFF)
    _AddCtrlRegion($hCombined_rgn, $id_Default)
    _AddCtrlRegion($hCombined_rgn, $id_Round)
    _AddCtrlRegion($hCombined_rgn, $id_Buble)
    _AddCtrlRegion($hCombined_rgn, $id_Transparent)
    _AddCtrlRegion($hCombined_rgn, $id_Exit)
    _WinAPI_SetWindowRgn($hWin, $hCombined_rgn)
EndFunc   ;==>_GuiHole

; add control's area to given region
; respecting also window title/frame sizes
Func _AddCtrlRegion($hFull_rgn, $idCtrl)
    Local $aCtrl_pos, $hCtrl_rgn

    $aCtrl_pos = ControlGetPos($g_hGui, "", $idCtrl)
    $hCtrl_rgn = _WinAPI_CreateRectRgn($aCtrl_pos[0] + $g_iFrame, $aCtrl_pos[1] + $g_iHtit + $g_iFrame, _
            $aCtrl_pos[0] + $aCtrl_pos[2] + $g_iFrame, $aCtrl_pos[1] + $aCtrl_pos[3] + $g_iHtit + $g_iFrame)
    _WinAPI_CombineRgn($hFull_rgn, $hFull_rgn, $hCtrl_rgn, $RGN_OR)
EndFunc   ;==>_AddCtrlRegion

Anyone else experience the same thing? Anyone have an idea on how to address this by either fixing operation of the minimize button and alignment, or keeping/restoring the titlebar?

Link to post
Share on other sites

Hi @spudw2k.  I can reproduce the issue on Win7 and on Win10.  It seems that the SetWindowRgn API changes the theme of the window when it includes the title/frame.  You can change it yourself if you add the SetWindowTheme of the window at the beginning of the script like this :

_WinAPI_SetWindowTheme($g_hGui, "0", "0")

On Win10 there is an additional problem.  The minimize button does not work with the basic theme (it does work on Win7 though). Not exactly sure if it is a Windows or AutoIt bug.  We would need to run a GUI with the basic theme in another language to see if the minimize button persists to be dysfunctional.

I did not find a solution to this issue so far.

ps. just tested the SetWindowTheme in Win10 under FreeBasic and the same problem appears.  So this is a Window bug

Edited by Nine
Test with FreeBasic
Link to post
Share on other sites

Very interesting.  Thank you for taking the time to investigate.  I was looking into the window theme venue, but did not get very far. Curious behavior with the "classic" theme (for lack of a better term) on Windows 10.

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...