Jump to content

How can I detect if a control is within a Tab control?


Go to solution Solved by ioa747,

Recommended Posts

Posted (edited)

And an alternative because it was spinning in my mind  :huh2:
(for more whimsical cases, like $id_CheckBox_Out2)  

; https://www.autoitscript.com/forum/topic/213665-how-can-i-detect-if-a-control-is-within-a-tab-control/#findComment-1552092

#AutoIt3Wrapper_UseX64=n
#include <GuiTab.au3>
#include <GUIConstantsEx.au3>
#include <WinAPISysWin.au3>
#include <Array.au3>

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


Example()

Func Example()
    Local $hGUI_Main = GUICreate("My GUI Tab", 400, 180)

    GUISetBkColor(0x808080)
    GUISetFont(9, 300)

    Local $id_Tab_Main = GUICtrlCreateTab(10, 10, 200, 100)

    Local $id_Tab_0 = GUICtrlCreateTabItem("Tab 0")
    Local $id_CheckBox_0 = GUICtrlCreateCheckbox("Checkbox 0", 20, 50)

    Local $id_Tab_1 = GUICtrlCreateTabItem("Tab 1")
    GUICtrlSetState(-1, $GUI_SHOW) ; will be display first
    Local $id_CheckBox_1 = GUICtrlCreateCheckbox("Checkbox 1", 20, 50)

    Local $id_Tab_2 = GUICtrlCreateTabItem("Tab 2")
    Local $id_CheckBox_2 = GUICtrlCreateCheckbox("Checkbox 2", 20, 50)
    Local $id_CheckBox_Out2 = GUICtrlCreateCheckbox("Checkbox outside the TAB area", 0, 150)

    GUICtrlCreateTabItem("") ; end tabitem definition

    Local $id_CheckBox_Out = GUICtrlCreateCheckbox("Checkbox outside the TAB area", 20, 120)

    GUISetState(@SW_SHOW)

    Local $aTabCtrls = _GetTabControlsMap_Advanced($hGUI_Main, $id_Tab_Main)
;~  _ArrayDisplay($aTabCtrls)

    If Not @error  Then
        For $i = 0 To UBound($aTabCtrls) - 1
            Local $aCtrls = StringSplit($aTabCtrls[$i], "|")
            ConsoleWrite("Tab:" & $i & " -> ctrls: ")
            For $j = 1 To $aCtrls[0]
                ConsoleWrite($aCtrls[$j] & ($j < $aCtrls[0] ? ", " : ""))
            Next
            ConsoleWrite("" & @CRLF)
        Next
    EndIf


    Local $idMsg
    ; Loop until the user exits.
    While 1
        $idMsg = GUIGetMsg()

        If $idMsg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc   ;==>Example

Func _GetTabControlsMap_Advanced($hGUI, $idTab)
    Local $iTotalTabs = _GUICtrlTab_GetItemCount($idTab)
    ConsoleWrite("- $iTotalTabs=" & $iTotalTabs & @CRLF)
    If $iTotalTabs < 2 Then Return SetError(1, 0, 0)
    Local $aTabMap[$iTotalTabs]
    Local $iActiveTab = GUICtrlRead($idTab) ; Save current position
    ConsoleWrite("- $iActiveTab=" & $iActiveTab & @CRLF)

    Local $aHide, $aHide2
    $aHide = _GetControlsByState($hGUI)

    For $i = 0 To UBound($aTabMap) - 1

        If $i = $iActiveTab Then ContinueLoop

        _GUICtrlTab_ClickTab($idTab, $i)
        Sleep(10)
        $aHide2 = _GetControlsByState($hGUI)
        Local $sTabInh = ""

        For $j = 1 To $aHide[0][0]
            If $aHide2[$j][1] & $aHide[$j][1] = "10" Then $sTabInh &= $aHide[$j][0] & "|"
        Next

        If StringRight($sTabInh, 1) = "|" Then $sTabInh = StringTrimRight($sTabInh, 1)
        $aTabMap[$i] = $sTabInh
    Next

    _GUICtrlTab_ClickTab($idTab, $iActiveTab)
    $sTabInh = ""
    If Not IsArray($aHide2) Then Return SetError(2, 0, 0)
    For $j = 1 To $aHide[0][0]
        If $aHide[$j][1] & $aHide2[$j][1] = "10" Then $sTabInh &= $aHide[$j][0] & "|"
    Next

    If StringRight($sTabInh, 1) = "|" Then $sTabInh = StringTrimRight($sTabInh, 1)
    $aTabMap[$iActiveTab] = $sTabInh

    Return $aTabMap
EndFunc   ;==>_GetTabControlsMap_Advanced

Func _GetControlsByState($hGUI)
    Local $aChildHandles = _WinAPI_EnumChildWindows($hGUI, False)
    If @error Then Return SetError(1, 0, 0)

    Local $aFound[$aChildHandles[0][0] + 1][2]
    $aFound[0][0] = $aChildHandles[0][0]

    For $i = 1 To $aChildHandles[0][0]
        Local $hCtrl = $aChildHandles[$i][0]
        ; convert the Handle to ID
        Local $idCtrl = _WinAPI_GetDlgCtrlID($hCtrl)
        Local $bVisible = ControlCommand($hGUI, "", $hCtrl, "IsVisible", "")
        $aFound[$i][0] = $idCtrl
        $aFound[$i][1] = $bVisible
    Next

    Return $aFound
EndFunc

 

Edited by ioa747
update

I know that I know nothing

Posted
3 hours ago, ioa747 said:

And an alternative because it was spinning in my mind  :huh2:
(for more whimsical cases, like $id_CheckBox_Out2

You very often have wonderful and creative ideas. I like the way that you think. Thanks for sharing. :)

 

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