Jump to content

Need help with Tab Control on 24H2/25H2 with the new DarkMode_DarkTheme


Go to solution Solved by Nine,

Recommended Posts

Posted
On 5/11/2026 at 7:16 PM, UEZ said:

Took the code from my example and modified it:

Awesome code.

Sorry to butt in here a bit .... but I just couldn't resist and checked if this code could do something I've always wanted to do.
Through the color of the red frame and the highlighting of the active element (its background and text color) it clearly indicates which tab is active.

#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiTab.au3>
#include <TabConstants.au3>
#include <WindowsNotifsConstants.au3>
#include <WindowsSysColorConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPISysWin.au3>
#include <WinAPITheme.au3>

; Initialize System DPI awareness
DllCall("user32.dll", "bool", "SetProcessDpiAwarenessContext", @AutoItX64 ? "int64" : "int", -2)

Global $g_hTab_CB, $g_pTab_CB, $g_hProc, $g_hTab
Global Const $COLOR_BUTTON_BG = 0x383838, $COLOR_BG_DARK = 0x202020, $COLOR_GUI_BG = 0x101010, $COLOR_BORDER = 0x606060, $COLOR_BORDER_DARK = 0x303030

Example()

Func Example()
    Local $hGUI = GUICreate("DarkTheme TabControl (24H2/25H2)", 500, 300)

    GUISetBkColor($COLOR_GUI_BG)
    GUISetFont(10)

    Local $idTab = GUICtrlCreateTab(20, 20, 460, 260)
    $g_hTab = GUICtrlGetHandle($idTab)

    GUICtrlCreateTabItem("tab0")
    GUICtrlCreateTabItem("tab1")
    GUICtrlCreateTabItem("tab2")
    GUICtrlSetState(-1, $GUI_SHOW)
    GUICtrlCreateTabItem("tab3")
    GUICtrlCreateTabItem("tab4")

    ; Remove focus rectangle from tab control
    GUICtrlSendMsg($idTab, $WM_CHANGEUISTATE, 65537, 0)

    ; Set dark titlebar
    _WinAPI_DwmSetWindowAttribute($hGUI, $DWMWA_USE_IMMERSIVE_DARK_MODE, True)

    ; Set theme if OS supports it
    If _is24H2Plus() Then _WinAPI_SetWindowTheme(GUICtrlGetHandle($idTab), 'DarkMode_DarkTheme')

    ; Register Subclassing / Window Procedure
    $g_hTab_CB = DllCallbackRegister(_WinProc, "ptr", "hwnd;uint;wparam;lparam")
    $g_pTab_CB = DllCallbackGetPtr($g_hTab_CB)
    $g_hProc = _WinAPI_SetWindowLong($g_hTab, $GWL_WNDPROC, $g_pTab_CB)

    GUISetState(@SW_SHOW)

    Local $idMsg
    While 1
        $idMsg = GUIGetMsg()
        If $idMsg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd

    ; Cleanup: Restore original Window Procedure
    _WinAPI_SetWindowLong($g_hTab, $GWL_WNDPROC, $g_hProc)
    DllCallbackFree($g_hTab_CB)
EndFunc   ;==>Example

Func _is24H2Plus()
    ; Check if this OS build is Windows 11 24H2/25H2 to support the newer DarkMode_DarkTheme
    Local $iRevision = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "UBR")
    Local $b24H2Plus = False
    If @OSBuild >= 26100 And $iRevision >= 6899 Then
        $b24H2Plus = True
    Else
        ConsoleWrite("Windows 11 24H2/25H2 (build 26100.6899 or higher) is required to use DarkMode_DarkTheme." & @CRLF)
    EndIf
    Return $b24H2Plus
EndFunc   ;==>_is24H2Plus

Func _WinProc($hWnd, $iMsg, $wParam, $lParam) ;coded by UEZ
    Switch $iMsg
        Case $WM_ERASEBKGND
            Return 1 ; Prevent background erasing to avoid flickering

        Case $WM_PAINT

            Local $tPaint = DllStructCreate($tagPAINTSTRUCT)
            Local $hDC = _WinAPI_BeginPaint($hWnd, $tPaint)
            If @error Or Not $hDC Then Return _WinAPI_CallWindowProc($g_hProc, $hWnd, $iMsg, $wParam, $lParam)

            Local $tClient = _WinAPI_GetClientRect($hWnd)
            Local $iWidth = $tClient.Right
            Local $iHeight = $tClient.Bottom

            ; Prepare Double Buffering
            Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC)
            Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $iWidth, $iHeight)
            Local $hOldBmp = _WinAPI_SelectObject($hMemDC, $hBitmap)

            ; --- 1. Clipping (Exclude child controls from drawing) ---
            Local $hParent = _WinAPI_GetParent($hWnd)
            Local $hChild = _WinAPI_GetWindow($hParent, $GW_CHILD)
            Local $tCR, $tPR = _WinAPI_GetWindowRect($hWnd)
            Local $left, $top, $right, $bottom

            While $hChild
                If $hChild <> $hWnd And _WinAPI_IsWindowVisible($hChild) Then
                    $tCR = _WinAPI_GetWindowRect($hChild)
                    If Not ($tCR.right < $tPR.left Or $tCR.left > $tPR.right Or $tCR.bottom < $tPR.top Or $tCR.top > $tPR.bottom) Then
                        $left = Max($tCR.left, $tPR.left) - $tPR.left
                        $top = Max($tCR.top, $tPR.top) - $tPR.top
                        $right = Min($tCR.right, $tPR.right) - $tPR.left
                        $bottom = Min($tCR.bottom, $tPR.bottom) - $tPR.top
                        DllCall("gdi32.dll", "int", "ExcludeClipRect", "handle", $hMemDC, "int", $left, "int", $top, "int", $right, "int", $bottom)
                    EndIf
                EndIf
                $hChild = _WinAPI_GetWindow($hChild, $GW_HWNDNEXT)
            WEnd

            Local $hTabUpDown = _WinAPI_FindWindowEx($hWnd, "msctls_updown32")
            If $hTabUpDown And _WinAPI_IsWindowVisible($hTabUpDown) Then
                $tCR = _WinAPI_GetWindowRect($hTabUpDown)
                If Not ($tCR.right < $tPR.left Or $tCR.left > $tPR.right Or $tCR.bottom < $tPR.top Or $tCR.top > $tPR.bottom) Then
                    $left = Max($tCR.left, $tPR.left) - $tPR.left
                    $top = Max($tCR.top, $tPR.top) - $tPR.top
                    $right = Min($tCR.right, $tPR.right) - $tPR.left
                    $bottom = Min($tCR.bottom, $tPR.bottom) - $tPR.top
                    DllCall("gdi32.dll", "int", "ExcludeClipRect", "handle", $hMemDC, "int", $left, "int", $top, "int", $right, "int", $bottom)
                EndIf
            EndIf

            ; 2. Draw main background (Dark color)
            Local $hBrushBg = _WinAPI_CreateSolidBrush(_ColorToCOLORREF($COLOR_BG_DARK)) ;
            _WinAPI_FillRect($hMemDC, $tClient, $hBrushBg)

            Local $iTabCount = _SendMessage($hWnd, $TCM_GETITEMCOUNT, 0, 0)
            Local $iCurSel = _SendMessage($hWnd, $TCM_GETCURSEL, 0, 0)

            ; 3. Prepare the Body Frame (The area beneath the tabs)
            Local $tFirstTabRect = DllStructCreate($tagRECT)
            _SendMessage($hWnd, $TCM_GETITEMRECT, 0, DllStructGetPtr($tFirstTabRect))

            Local $tBodyRect = DllStructCreate($tagRECT)
            $tBodyRect.Left = 0
            $tBodyRect.Top = $tFirstTabRect.Bottom  ; Starts at the bottom edge of the tabs
            $tBodyRect.Right = $iWidth
            $tBodyRect.Bottom = $iHeight

            Local $hBrushBorder = _WinAPI_CreateSolidBrush(_ColorToCOLORREF($COLOR_RED))
            _WinAPI_FrameRect($hMemDC, $tBodyRect, $hBrushBorder)

            ; 4. Draw the "Gap" to the right of the tabs in GUI background color
            If $iTabCount > 0 Then
                Local $tLastTabRect = DllStructCreate($tagRECT)
                _SendMessage($hWnd, $TCM_GETITEMRECT, $iTabCount - 1, DllStructGetPtr($tLastTabRect))
                Local $tGapRect = DllStructCreate($tagRECT)
                $tGapRect.Left = $tLastTabRect.Right + 2
                $tGapRect.Top = 0
                $tGapRect.Right = $iWidth
                $tGapRect.Bottom = $tLastTabRect.Bottom
                Local $hBrushGui = _WinAPI_CreateSolidBrush(_ColorToCOLORREF($COLOR_GUI_BG))
                _WinAPI_FillRect($hMemDC, $tGapRect, $hBrushGui)
                _WinAPI_DeleteObject($hBrushGui)
            EndIf

            _WinAPI_SetBkMode($hMemDC, 1) ; Transparent background for text
;~             _WinAPI_SetTextColor($hMemDC, _ColorToCOLORREF(0xF0F0F0))

            ; 5. Draw individual tabs
            For $i = 0 To $iTabCount - 1
                Local $tRECT = DllStructCreate($tagRECT)
                _SendMessage($hWnd, $TCM_GETITEMRECT, $i, DllStructGetPtr($tRECT))
                If $tRECT.Right < 0 Or $tRECT.Left > $iWidth Then ContinueLoop
                $tRECT.top -= 2
                Local $bSelected = ($i = $iCurSel)
                Local $iTabColor = $bSelected ? $COLOR_DARKSLATEGRAY : $COLOR_BG_DARK
                Local $hTabBrush = _WinAPI_CreateSolidBrush(_ColorToCOLORREF($iTabColor))

                ; Fill tab background
                _WinAPI_FillRect($hMemDC, $tRECT, $hTabBrush)

                If $bSelected Then
                    _WinAPI_SetTextColor($hMemDC, _WinAPI_SwitchColor($COLOR_VIOLET))
                    ; Draw border ONLY for the active tab (Top, Left, Right)
                    _WinAPI_FrameRect($hMemDC, $tRECT, $hBrushBorder)

                    ; OPEN BOTTOM: Draw a line in tab-color over the body-border to merge them
                    Local $tOpenLine = DllStructCreate($tagRECT)
                    $tOpenLine.Left = $tRECT.Left + 1
                    $tOpenLine.Top = $tRECT.Bottom - 1 ; Exactly on the border line of the body
                    $tOpenLine.Right = $tRECT.Right - 1
                    $tOpenLine.Bottom = $tRECT.Bottom + 1
                    _WinAPI_FillRect($hMemDC, $tOpenLine, $hTabBrush)
                Else
                    _WinAPI_SetTextColor($hMemDC, _WinAPI_SwitchColor($COLOR_RED))
                    ; Draw rectangle around non active tabs
                    Local $hBrushTabRecDark = _WinAPI_CreateSolidBrush(_ColorToCOLORREF($COLOR_BORDER_DARK))
                    _WinAPI_FrameRect($hMemDC, $tRECT, $hBrushTabRecDark)
                    _WinAPI_DeleteObject($hBrushTabRecDark)
                EndIf

                _WinAPI_DeleteObject($hTabBrush)

                ; Draw text centered
                Local $sText = _GUICtrlTab_GetItemText($hWnd, $i)
                Local $tTextRect = DllStructCreate($tagRECT)
                With $tTextRect
                    .Left = $tRECT.Left + 6
                    .Top = $tRECT.Top + ($bSelected ? 1 : 3)
                    .Right = $tRECT.Right - 6
                    .Bottom = $tRECT.Bottom - 3
                EndWith
                DllCall("user32.dll", "int", "DrawTextW", "handle", $hMemDC, "wstr", $sText, "int", -1, "struct*", $tTextRect, "uint", BitOR($DT_CENTER, $DT_VCENTER, $DT_SINGLELINE, $DT_NOPREFIX))
            Next

            ; 6. Copy memory DC to screen DC (BitBlt)
            _WinAPI_BitBlt($hDC, 0, 0, $iWidth, $iHeight, $hMemDC, 0, 0, $SRCCOPY)

            ; Cleanup
            _WinAPI_SelectObject($hMemDC, $hOldBmp)
            _WinAPI_DeleteObject($hBitmap)
            _WinAPI_DeleteObject($hBrushBg)
            _WinAPI_DeleteObject($hBrushBorder)
            _WinAPI_DeleteDC($hMemDC)
            _WinAPI_EndPaint($hWnd, $tPaint)
            Return 0
    EndSwitch

    Return _WinAPI_CallWindowProc($g_hProc, $hWnd, $iMsg, $wParam, $lParam)
EndFunc   ;==>_WinProc

Func _ColorToCOLORREF($iColor) ; Convert RGB to BGR
    Local $iR = BitAND(BitShift($iColor, 16), 0xFF)
    Local $iG = BitAND(BitShift($iColor, 8), 0xFF)
    Local $iB = BitAND($iColor, 0xFF)
    Return BitOR(BitShift($iB, -16), BitShift($iG, -8), $iR)
EndFunc   ;==>_ColorToCOLORREF

Func Min($a, $b)
    Return ($a < $b ? $a : $b)
EndFunc   ;==>Min

Func Max($a, $b)
    Return ($a > $b ? $a : $b)
EndFunc   ;==>Max

Func _WinAPI_FindWindowEx($hParent, $sClass, $sTitle = "", $hAfter = 0)
    Local $ret = DllCall("user32.dll", "hwnd", "FindWindowExW", "hwnd", $hParent, "hwnd", $hAfter, "wstr", $sClass, "wstr", $sTitle)
    If @error Or Not IsArray($ret) Then Return 0
    Return $ret[0]
EndFunc   ;==>_WinAPI_FindWindowEx

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Posted (edited)

@UEZ If you have a moment, could you please help me with something?

From the following example, I would like to prevent the top 2 pixels from being painted over on the active tab only.

ActiveTab1.png.347e397eff9b8862f3eaec7f421747ad.png

ActiveTab2.png.039d171bfe734fd10a033a4656b97608.png

Sorry for the screenshot clips being so small. The top screenshot shows what is happening. The top 2 pixels on the active tab is also being painted over. On a darker GUI background colour, that makes it difficult to see that the active tab is actually the active tab.

The bottom screenshot just shows what that top 2 pixels contains and why it is important.

Do you know how to exclude the top 2 pixels from active tab only from being painted over?

Thank you. :)

EDIT: Sorry I forgot to post my current testing example:

EDIT2: I'm wondering if the _WinAPI_ExtFloodFill function would be beneficial in the "; paints strip on top of each tab" section since it would target the specific white color and leave the active tab alone. 

; From Nine
#include <GUIConstants.au3>
#include <WinAPI.au3>
#include <GuiTab.au3>
#include <WindowsSysColorConstants.au3>
#include <WinAPITheme.au3>

; initiate System DPI awareness
DllCall("user32.dll", "bool", "SetProcessDpiAwarenessContext", @AutoItX64 ? "int64" : "int", -2)

Opt("MustDeclareVars", True)

Example()

Func Example()
    Local $hGUI = GUICreate("DarkTheme TabControl", 400, 300)

    GUISetBkColor(0x191919)
    GUISetFont(10, 300)

    Local $idTab = GUICtrlCreateTab(20, 20, 360, 260, -1)
    Local $hTab = GUICtrlGetHandle($idTab)

    GUICtrlCreateTabItem("tab0")
    GUICtrlCreateLabel("label0", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)

    GUICtrlCreateTabItem("tab----1")
    GUICtrlCreateLabel("label1", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)

    GUICtrlCreateTabItem("tab2")
    GUICtrlSetState(-1, $GUI_SHOW)
    GUICtrlCreateLabel("label2", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)
    Local $idAddTab = GUICtrlCreateButton("Trigger UpDown", 50, 120)
    _WinAPI_SetWindowTheme(GUICtrlGetHandle($idAddTab), 'DarkMode_Explorer')

    GUICtrlCreateTabItem("tab3")
    GUICtrlCreateLabel("label3", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)

    GUICtrlCreateTabItem("") ; end tabitem definition

    ; set DarkMode_DarkTheme visual theme on Tab control as long as OSBuild supports it
    If _is24H2Plus() Then _WinAPI_SetWindowTheme($hTab, 'DarkMode_DarkTheme')

    ; get handle for UpDown control to apply theme
    Local $hUpDown = _WinAPI_FindWindowEx($hTab, "msctls_updown32")
    If _is24H2Plus() And $hUpDown Then _WinAPI_SetWindowTheme($hUpDown, 'DarkMode_DarkTheme')

    Local $pAddress = _WinAPI_GetWindowLong($hTab, $GWL_WNDPROC)
    Local $hSubclass = DllCallbackRegister(WM_PAINT, "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr")
    _WinAPI_SetWindowSubclass($hTab, DllCallbackGetPtr($hSubclass), $idTab, $pAddress)

    ; remove focus rectangle from tab control
    GUICtrlSendMsg($idTab, $WM_CHANGEUISTATE, 65537, 0)

    ; needed to fix button frame when button is in tab control
    GUIRegisterMsg($WM_CTLCOLORBTN, "_WM_CTLCOLORBTN")

    GUISetState()

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

    _WinAPI_RemoveWindowSubclass($hTab, DllCallbackGetPtr($hSubclass), $idTab)
    DllCallbackFree($hSubclass)
EndFunc   ;==>Example

Func _CreateNewTab()
    GUICtrlCreateTabItem("tab4")
    GUICtrlCreateLabel("label4", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)

    GUICtrlCreateTabItem("tab5")
    GUICtrlCreateLabel("label5", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)

    GUICtrlCreateTabItem("tab6")
    GUICtrlCreateLabel("label6", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)
EndFunc

Func WM_PAINT($hWnd, $iMsg, $wParam, $lParam, $iID, $pData)
    Switch $iMsg
        Case $WM_ERASEBKGND
            Return 1 ; Prevent background erase to avoid flicker

        Case $WM_PAINT
            Local $tPaint = DllStructCreate($tagPAINTSTRUCT)
            Local $hDC = _WinAPI_BeginPaint($hWnd, $tPaint)
            Local $tRect = _WinAPI_GetClientRect($hWnd)
            Local $iW = $tRect.Right
            Local $iH = $tRect.Bottom

            Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC)
            Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
            Local $hOldBmp = _WinAPI_SelectObject($hMemDC, $hBitmap)

            _SendMessage($hWnd, $WM_PRINTCLIENT, $hMemDC, 20) ; 20 = $PRF_CLIENT or $PRF_CHILDREN

            Local $hBrush = _WinAPI_CreateSolidBrush(0x191919)
            Local $iTabCount = _GUICtrlTab_GetItemCount($hWnd)
            
            If $iTabCount > 0 Then
                Local $tRectLast = _GUICtrlTab_GetItemRectEx($hWnd, $iTabCount - 1)

                ; Exclude overlapping GUI controls from painting
                Local $hParent = _WinAPI_GetParent($hWnd)
                Local $hChild = _WinAPI_GetWindow($hParent, $GW_CHILD)
                Local $tCR, $tPR = _WinAPI_GetWindowRect($hWnd)
                Local $left, $top, $right, $bottom

                ; Exclude overlapping GUI controls from painting
                While $hChild
                    If $hChild <> $hWnd And _WinAPI_IsWindowVisible($hChild) Then
                        $tCR = _WinAPI_GetWindowRect($hChild)

                        ; Ensure control is within tab control
                        If Not ($tCR.right < $tPR.left Or $tCR.left > $tPR.right Or _
                                $tCR.bottom < $tPR.top Or $tCR.top > $tPR.bottom) Then

                            $left = Max($tCR.left, $tPR.left) - $tPR.left
                            $top = Max($tCR.top, $tPR.top) - $tPR.top
                            $right = Min($tCR.right, $tPR.right) - $tPR.left
                            $bottom = Min($tCR.bottom, $tPR.bottom) - $tPR.top

                            DllCall("gdi32.dll", "int", "ExcludeClipRect", "handle", $hMemDC, "int", $left, "int", $top, "int", $right, "int", $bottom)
                            DllCall("gdi32.dll", "int", "ExcludeClipRect", "handle", $hDC, "int", $left, "int", $top, "int", $right, "int", $bottom)
                        EndIf
                    EndIf
                    $hChild = _WinAPI_GetWindow($hChild, $GW_HWNDNEXT)
                WEnd

                ; exclude UpDown control from being painted over - ExcludeClipRect code from UEZ
                Local $hTabUpDown = _WinAPI_FindWindowEx($hWnd, "msctls_updown32")
                If $hTabUpDown And _WinAPI_IsWindowVisible($hTabUpDown) Then
                    Local $tCR = _WinAPI_GetWindowRect($hTabUpDown)
                    Local $tPR = _WinAPI_GetWindowRect($hWnd)
                    DllCall('gdi32.dll', "int", "ExcludeClipRect", "handle", $hMemDC, _
                            "int", $tCR.Left - $tPR.Left, "int", $tCR.Top - $tPR.Top - 1, _
                            "int", $tCR.Right - $tPR.Left, "int", $tCR.Bottom - $tPR.Top + 2)
                    DllCall('gdi32.dll', "int", "ExcludeClipRect", "handle", $hDC, _
                            "int", $tCR.Left - $tPR.Left, "int", $tCR.Top - $tPR.Top - 1, _
                            "int", $tCR.Right - $tPR.Left, "int", $tCR.Bottom - $tPR.Top + 2)
                EndIf

                ; paints area to the right of all tabs
                Local $tPatch = DllStructCreate($tagRECT)
                $tPatch.Left = $tRectLast.Right - 2
                $tPatch.Top = 0
                $tPatch.Right = $iW
                $tPatch.Bottom = $tRectLast.Bottom
                _WinAPI_FillRect($hMemDC, $tPatch, $hBrush)

                ; paints area to the left of all tabs
                $tPatch.Left = 0
                $tPatch.Right = 2
                _WinAPI_FillRect($hMemDC, $tPatch, $hBrush)

                ; paints strip on top of each tab
                $tPatch.Left = 0
                $tPatch.Top = 0
                $tPatch.Right = $iW
                $tPatch.Bottom = 2
                _WinAPI_FillRect($hMemDC, $tPatch, $hBrush)
            EndIf
            
            _WinAPI_DeleteObject($hBrush)
            _WinAPI_BitBlt($hDC, 0, 0, $iW, $iH, $hMemDC, 0, 0, $SRCCOPY)

            _WinAPI_SelectObject($hMemDC, $hOldBmp)
            _WinAPI_DeleteObject($hBitmap)
            _WinAPI_DeleteDC($hMemDC)
            _WinAPI_EndPaint($hWnd, $tPaint)
            Return 0
        Case $WM_PARENTNOTIFY
            ; Fired when a child window is created inside the tab control.
            ; The tab spinner (msctls_updown32) is created lazily by Windows when tabs overflow -
            ; it doesn't exist at init time, so we theme it here the moment it appears.
            If _WinAPI_LoWord($wParam) = $WM_CREATE Then
                Local $hNewChild = HWnd($lParam) ; lParam carries the new child's HWND as integer - must cast!
                If _WinAPI_GetClassName($hNewChild) = "msctls_updown32" Then
                    If _is24H2Plus() Then _WinAPI_SetWindowTheme($hNewChild, 'DarkMode_DarkTheme')
                EndIf
            EndIf
            Return __WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
    EndSwitch

    Return __WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
EndFunc   ;==>WM_PAINT

Func _is24H2Plus()
    ; check if this OS build is Windows 11 24H2/25H2 to support the newer DarkMode_DarkTheme
    Local $iRevision = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "UBR")
    Local $b24H2Plus = False
    If @OSBuild >= 26100 And $iRevision >= 6899 Then
        $b24H2Plus = True
    Else
        ConsoleWrite("Windows 11 24H2/25H2 (build 26100.6899 or higher) is required to use DarkMode_DarkTheme.")
    EndIf
    Return $b24H2Plus
EndFunc

Func _WinAPI_FindWindowEx($hParent, $sClass, $sTitle = "", $hAfter = 0)
    Local $ret = DllCall('user32.dll', "hwnd", "FindWindowExW", "hwnd", $hParent, "hwnd", $hAfter, "wstr", $sClass, "wstr", $sTitle)
    If @error Or Not IsArray($ret) Then Return 0
    Return $ret[0]
EndFunc   ;==>_WinAPI_FindWindowEx

Func __WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
    Return DllCall('comctl32.dll', 'lresult', 'DefSubclassProc', 'hwnd', $hWnd, 'uint', $iMsg, 'wparam', $wParam, _
            'lparam', $lParam)[0]
EndFunc   ;==>__WinAPI_DefSubclassProc

Func Min($a, $b)
    Return ($a < $b ? $a : $b)
EndFunc   ;==>Min

Func Max($a, $b)
    Return ($a > $b ? $a : $b)
EndFunc   ;==>Max

Func _WM_CTLCOLORBTN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hCtrl = $lParam
    Local $hBrush = _WinAPI_CreateSolidBrush(0x262626)
    Return $hBrush
EndFunc   ;==>_CTLCOLORBTN

 

Edited by WildByDesign
Posted (edited)

I think I got it! :)

Not the most elegant of coding, but it works. The UpDown control made it more complicated. I just hope that my measurements work on any system.

EDIT: Improved code.

EDIT2: I can see some potential for failure on my measurements depending on tab size...

EDIT3: I think I've mitigated the potential failure.

; From Nine
#include <GUIConstants.au3>
#include <WinAPI.au3>
#include <GuiTab.au3>
#include <WindowsSysColorConstants.au3>
#include <WinAPITheme.au3>

; initiate System DPI awareness
DllCall("user32.dll", "bool", "SetProcessDpiAwarenessContext", @AutoItX64 ? "int64" : "int", -2)

Opt("MustDeclareVars", True)

Example()

Func Example()
    Local $hGUI = GUICreate("DarkTheme TabControl", 400, 300)

    GUISetBkColor(0x191919)
    GUISetFont(10, 300)

    Local $idTab = GUICtrlCreateTab(20, 20, 360, 260, -1)
    Local $hTab = GUICtrlGetHandle($idTab)

    GUICtrlCreateTabItem("tab0")
    GUICtrlCreateLabel("label0", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)

    GUICtrlCreateTabItem("tab----1")
    GUICtrlCreateLabel("label1", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)

    GUICtrlCreateTabItem("tab2")
    GUICtrlSetState(-1, $GUI_SHOW)
    GUICtrlCreateLabel("label2", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)
    Local $idAddTab = GUICtrlCreateButton("Trigger UpDown", 50, 120)
    _WinAPI_SetWindowTheme(GUICtrlGetHandle($idAddTab), 'DarkMode_Explorer')

    GUICtrlCreateTabItem("tab3")
    GUICtrlCreateLabel("label3", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)

    GUICtrlCreateTabItem("") ; end tabitem definition

    ; set DarkMode_DarkTheme visual theme on Tab control as long as OSBuild supports it
    If _is24H2Plus() Then _WinAPI_SetWindowTheme($hTab, 'DarkMode_DarkTheme')

    ; get handle for UpDown control to apply theme
    Local $hUpDown = _WinAPI_FindWindowEx($hTab, "msctls_updown32")
    If _is24H2Plus() And $hUpDown Then _WinAPI_SetWindowTheme($hUpDown, 'DarkMode_DarkTheme')

    Local $pAddress = _WinAPI_GetWindowLong($hTab, $GWL_WNDPROC)
    Local $hSubclass = DllCallbackRegister(WM_PAINT, "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr")
    _WinAPI_SetWindowSubclass($hTab, DllCallbackGetPtr($hSubclass), $idTab, $pAddress)

    ; remove focus rectangle from tab control
    GUICtrlSendMsg($idTab, $WM_CHANGEUISTATE, 65537, 0)

    ; needed to fix button frame when button is in tab control
    GUIRegisterMsg($WM_CTLCOLORBTN, "_WM_CTLCOLORBTN")

    GUISetState()

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

    _WinAPI_RemoveWindowSubclass($hTab, DllCallbackGetPtr($hSubclass), $idTab)
    DllCallbackFree($hSubclass)
EndFunc   ;==>Example

Func _CreateNewTab()
    GUICtrlCreateTabItem("tab4")
    GUICtrlCreateLabel("label4", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)

    GUICtrlCreateTabItem("tab5")
    GUICtrlCreateLabel("label5", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)

    GUICtrlCreateTabItem("tab6")
    GUICtrlCreateLabel("label6", 30, 80, 50, 20)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetBkColor(-1, 0x262626)
EndFunc

Func WM_PAINT($hWnd, $iMsg, $wParam, $lParam, $iID, $pData)
    Switch $iMsg
        Case $WM_ERASEBKGND
            Return 1 ; Prevent background erase to avoid flicker

        Case $WM_PAINT
            Local $tPaint = DllStructCreate($tagPAINTSTRUCT)
            Local $hDC = _WinAPI_BeginPaint($hWnd, $tPaint)
            Local $tRect = _WinAPI_GetClientRect($hWnd)
            Local $iW = $tRect.Right
            Local $iH = $tRect.Bottom

            Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC)
            Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
            Local $hOldBmp = _WinAPI_SelectObject($hMemDC, $hBitmap)

            Local $iUpDownWidth = 0

            _SendMessage($hWnd, $WM_PRINTCLIENT, $hMemDC, 20) ; 20 = $PRF_CLIENT or $PRF_CHILDREN

            Local $hBrush = _WinAPI_CreateSolidBrush(0x191919)
            Local $iTabCount = _GUICtrlTab_GetItemCount($hWnd)
            
            If $iTabCount > 0 Then
                Local $tRectLast = _GUICtrlTab_GetItemRectEx($hWnd, $iTabCount - 1)

                ; Exclude overlapping GUI controls from painting
                Local $hParent = _WinAPI_GetParent($hWnd)
                Local $hChild = _WinAPI_GetWindow($hParent, $GW_CHILD)
                Local $tCR, $tPR = _WinAPI_GetWindowRect($hWnd)
                Local $left, $top, $right, $bottom

                ; Exclude overlapping GUI controls from painting
                While $hChild
                    If $hChild <> $hWnd And _WinAPI_IsWindowVisible($hChild) Then
                        $tCR = _WinAPI_GetWindowRect($hChild)

                        ; Ensure control is within tab control
                        If Not ($tCR.right < $tPR.left Or $tCR.left > $tPR.right Or _
                                $tCR.bottom < $tPR.top Or $tCR.top > $tPR.bottom) Then

                            $left = Max($tCR.left, $tPR.left) - $tPR.left
                            $top = Max($tCR.top, $tPR.top) - $tPR.top
                            $right = Min($tCR.right, $tPR.right) - $tPR.left
                            $bottom = Min($tCR.bottom, $tPR.bottom) - $tPR.top

                            DllCall("gdi32.dll", "int", "ExcludeClipRect", "handle", $hMemDC, "int", $left, "int", $top, "int", $right, "int", $bottom)
                            DllCall("gdi32.dll", "int", "ExcludeClipRect", "handle", $hDC, "int", $left, "int", $top, "int", $right, "int", $bottom)
                        EndIf
                    EndIf
                    $hChild = _WinAPI_GetWindow($hChild, $GW_HWNDNEXT)
                WEnd

                ; exclude UpDown control from being painted over - ExcludeClipRect code from UEZ
                Local $hTabUpDown = _WinAPI_FindWindowEx($hWnd, "msctls_updown32")
                If $hTabUpDown And _WinAPI_IsWindowVisible($hTabUpDown) Then
                    Local $tCR = _WinAPI_GetWindowRect($hTabUpDown)
                    $iUpDownWidth = $tCR.Right - $tCR.Left + 1
                    Local $tPR = _WinAPI_GetWindowRect($hWnd)
                    DllCall('gdi32.dll', "int", "ExcludeClipRect", "handle", $hMemDC, _
                            "int", $tCR.Left - $tPR.Left, "int", $tCR.Top - $tPR.Top - 1, _
                            "int", $tCR.Right - $tPR.Left, "int", $tCR.Bottom - $tPR.Top + 2)
                    DllCall('gdi32.dll', "int", "ExcludeClipRect", "handle", $hDC, _
                            "int", $tCR.Left - $tPR.Left, "int", $tCR.Top - $tPR.Top - 1, _
                            "int", $tCR.Right - $tPR.Left, "int", $tCR.Bottom - $tPR.Top + 2)
                EndIf

                Local $tRect = _WinAPI_GetClientRect($hWnd)
                _WinAPI_SelectObject($hMemDC, $hBrush)
                _WinAPI_SelectObject($hDC, $hBrush)
                _WinAPI_ExtFloodFill($hMemDC, $tRect.right - 1 - $iUpDownWidth, 1, 0xf0f0f0, $FLOODFILLSURFACE)
                _WinAPI_ExtFloodFill($hDC, $tRect.right - 1 - $iUpDownWidth, 1, 0xf0f0f0, $FLOODFILLSURFACE)
                _WinAPI_ExtFloodFill($hMemDC, $tRect.left + 1, 1, 0xf0f0f0, $FLOODFILLSURFACE)
                _WinAPI_ExtFloodFill($hDC, $tRect.left + 1, 1, 0xf0f0f0, $FLOODFILLSURFACE)
            EndIf
            
            _WinAPI_DeleteObject($hBrush)
            _WinAPI_BitBlt($hDC, 0, 0, $iW, $iH, $hMemDC, 0, 0, $SRCCOPY)

            _WinAPI_SelectObject($hMemDC, $hOldBmp)
            _WinAPI_DeleteObject($hBitmap)
            _WinAPI_DeleteDC($hMemDC)
            _WinAPI_EndPaint($hWnd, $tPaint)
            Return 0
        Case $WM_PARENTNOTIFY
            ; Fired when a child window is created inside the tab control.
            ; The tab spinner (msctls_updown32) is created lazily by Windows when tabs overflow -
            ; it doesn't exist at init time, so we theme it here the moment it appears.
            If _WinAPI_LoWord($wParam) = $WM_CREATE Then
                Local $hNewChild = HWnd($lParam) ; lParam carries the new child's HWND as integer - must cast!
                If _WinAPI_GetClassName($hNewChild) = "msctls_updown32" Then
                    If _is24H2Plus() Then _WinAPI_SetWindowTheme($hNewChild, 'DarkMode_DarkTheme')
                EndIf
            EndIf
            Return __WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
    EndSwitch

    Return __WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
EndFunc   ;==>WM_PAINT

Func _is24H2Plus()
    ; check if this OS build is Windows 11 24H2/25H2 to support the newer DarkMode_DarkTheme
    Local $iRevision = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "UBR")
    Local $b24H2Plus = False
    If @OSBuild >= 26100 And $iRevision >= 6899 Then
        $b24H2Plus = True
    Else
        ConsoleWrite("Windows 11 24H2/25H2 (build 26100.6899 or higher) is required to use DarkMode_DarkTheme.")
    EndIf
    Return $b24H2Plus
EndFunc

Func _WinAPI_FindWindowEx($hParent, $sClass, $sTitle = "", $hAfter = 0)
    Local $ret = DllCall('user32.dll', "hwnd", "FindWindowExW", "hwnd", $hParent, "hwnd", $hAfter, "wstr", $sClass, "wstr", $sTitle)
    If @error Or Not IsArray($ret) Then Return 0
    Return $ret[0]
EndFunc   ;==>_WinAPI_FindWindowEx

Func __WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
    Return DllCall('comctl32.dll', 'lresult', 'DefSubclassProc', 'hwnd', $hWnd, 'uint', $iMsg, 'wparam', $wParam, _
            'lparam', $lParam)[0]
EndFunc   ;==>__WinAPI_DefSubclassProc

Func Min($a, $b)
    Return ($a < $b ? $a : $b)
EndFunc   ;==>Min

Func Max($a, $b)
    Return ($a > $b ? $a : $b)
EndFunc   ;==>Max

Func _WM_CTLCOLORBTN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hCtrl = $lParam
    Local $hBrush = _WinAPI_CreateSolidBrush(0x262626)
    Return $hBrush
EndFunc   ;==>_CTLCOLORBTN

 

Edited by WildByDesign

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
×
×
  • Create New...