Jump to content

Recommended Posts

Posted (edited)

I have been creating my custom tabs with a little help from MattyD 

I would be happy to receive suggestions for improvement.

 

 

tabs.png.a2307c7cb3e2b3d38074f8555d1f49b6.png

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/mo
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <Array.au3>
#include <Misc.au3>
#include <WinAPISysWin.au3>
#include <StructureConstants.au3>
#include <GuiTab.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>
#include <GuiScrollBars.au3>
#include <ScrollBarsConstants.au3>


Global $aTabBarItems[1][10], $hTabbar


$hGui = GUICreate("", 800, 450)
GUISetBkColor(0x3f3f3f, $hGui)

$hTabbar = _TabInit($hGui, 10, 20, 725, 22, 0x3f3f3f)

$hButtonLeftScrollTabs = GUICtrlCreateLabel("<", 720 + 25, 20, 22, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
GUICtrlSetBkColor($hButtonLeftScrollTabs, 0x252525)
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetFont(-1, 11, 800)
$hButtonRightScrollTabs = GUICtrlCreateLabel(">", 744 + 25, 20, 22, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
GUICtrlSetBkColor(-1, 0x252525)
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetFont(-1, 11, 800)

$iTab = GUICtrlCreateTab(10, 33, 700, 400)
GUICtrlSetState($iTab, $GUI_HIDE)
GUICtrlCreateLabel("", 10, 43, 725, 400)
GUICtrlSetBkColor(-1, 0x4f4f4f)
GUICtrlSetState(-1, $gui_disable)

GUISetState(@SW_SHOW, $hGui)
GUISetState(@SW_SHOW, $hTabbar)

For $t = 1 To 20
    _TabAdd($hTabbar, $iTab, "Tab  " & $t)
Next


While 1
    Sleep(10)
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit

    For $t = 1 To $aTabBarItems[0][0]
        If $t > $aTabBarItems[0][0] Then ExitLoop
        If $msg = $aTabBarItems[$t][1] Or $msg = $aTabBarItems[$t][2] Then
            _GuiCtrlTabButtonSetActive($aTabBarItems[$t][1])
            _GUICtrlTab_ActivateTab($iTab, $t - 1)
        EndIf
        If $msg = $aTabBarItems[$t][4] Then
            _GuiCtrlTabButtonDelete($aTabBarItems[$t][1])
        EndIf
    Next

    ;----simple mouse hover-----------------------------
    $posCusorMenu = GUIGetCursorInfo($hTabbar)
    $iCtrlIDMenu = $posCusorMenu[4]
    For $t = 1 To $aTabBarItems[0][0]
        If $t > $aTabBarItems[0][0] Then ExitLoop
        If $iCtrlIDMenu = $aTabBarItems[$t][4] And $aTabBarItems[$t][16] = 0 Then
            GUICtrlSetColor($aTabBarItems[$t][4], 0xC40A0A)
            $aTabBarItems[$t][16] = 1
        ElseIf Not $iCtrlIDMenu = $aTabBarItems[$t][4] Or $iCtrlIDMenu = $aTabBarItems[$t][1] Or $iCtrlIDMenu = $aTabBarItems[$t][2] Then
            If $aTabBarItems[$t][16] = 1 Then
                GUICtrlSetColor($aTabBarItems[$t][4], 0xffffff)
                $aTabBarItems[$t][16] = 0
            EndIf
        EndIf
    Next
    ;--------------------------------------------------

    If $msg = $hButtonRightScrollTabs Then
        Sleep(100)
        If _IsPressed("01") Then
            While _IsPressed("01")
                _NextTab($hTabbar)
                Sleep(100)
            WEnd
        Else
            _NextTab($hTabbar)
        EndIf
    EndIf

    If $msg = $hButtonLeftScrollTabs Then
        Sleep(100)
        If _IsPressed("01") Then
            While _IsPressed("01")
                _PreviewsTab($hTabbar)
                Sleep(100)
            WEnd
        Else
            _PreviewsTab($hTabbar)
        EndIf
    EndIf
WEnd




;----NEW TAB-----------------------------------------------------------------------------------------------------------------------------
Func _TabInit($hParent, $iX, $iY, $iW, $ih, $x_BKcolor)
    $h_Gui_Tab = GUICreate("", $iW, $ih, $iX, $iY, $WS_POPUP, BitOR($WS_EX_TRANSPARENT, $WS_EX_CONTROLPARENT, $WS_EX_MDICHILD), $hParent)
    GUISetState(@SW_HIDE, $h_Gui_Tab)
    _GUIScrollBars_Init($h_Gui_Tab, 400)
    _GUIScrollBars_ShowScrollBar($h_Gui_Tab, $SB_VERT, False)
    GUISetBkColor($x_BKcolor, $h_Gui_Tab)
    GUISwitch($hParent)
    Return $h_Gui_Tab
EndFunc   ;==>_TabInit

Func _TabAdd($h_Tabbar, $i_Tab, $s_txt)
    GUISwitch($h_Tabbar)
    Local $iCountTabButtons = $aTabBarItems[0][0], $iWtB = 120, $iMinusCloseX = 10, $iX = 1
    ReDim $aTabBarItems[UBound($aTabBarItems) + 1][20]

    $iRetSize = _GetTextWidth($s_txt, 9, 500) + 10
    If $iRetSize > 0 Then
        $iWtB = $iRetSize
    EndIf
    GUISwitch($h_Tabbar)

    $BG_Color = 0x2E2E2E
    $LineColorDown = 0x7160E8

    If $iCountTabButtons > 0 Then
        $iX = $aTabBarItems[$iCountTabButtons][10] + $aTabBarItems[$iCountTabButtons][12] + 1
    EndIf

    $i_LabelTabBase = GUICtrlCreateLabel("", $iX, 0, $iWtB + $iMinusCloseX, 22, 0)
    GUICtrlSetBkColor(-1, 0xff0000)
    GUICtrlSetBkColor(-1, $BG_Color)
    GUICtrlSetState(-1, $gui_disable)

    $i_LabelTabTxt = GUICtrlCreateLabel($s_txt, $iX, 1, $iWtB, 18, BitOR($SS_CENTER, $SS_CENTERIMAGE), 0)
    GUICtrlSetFont(-1, 9, 500)
    GUICtrlSetColor(-1, 0xaaaaaa)
    GUICtrlSetBkColor(-1, 0x00ff00)
    GUICtrlSetBkColor(-1, $BG_Color)

    $i_LabelBtnTabBottom = GUICtrlCreateLabel("", $iX, 19, $iWtB + $iMinusCloseX, 3, BitOR($SS_CENTER, $SS_CENTERIMAGE), 0)
    GUICtrlSetBkColor(-1, $LineColorDown)

    $i_LabelTabBtnX = GUICtrlCreateLabel('', $iX + $iWtB + 1, 1, $iMinusCloseX - 2, $iMinusCloseX - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE), 0)
    GUICtrlSetFont($i_LabelTabBtnX, (($iMinusCloseX + 2) / 2), 100, 0, "Segoe MDL2 Assets", 0)
    GUICtrlSetData($i_LabelTabBtnX, ChrW(0xEF2C))
    GUICtrlSetColor(-1, 0xffffff)
    GUICtrlSetBkColor(-1, $BG_Color)

    GUISwitch(_WinAPI_GetParent($h_Tabbar))
    ;GUISwitch($hGui)
    $i_TabItem = GUICtrlCreateTabItem("tab " & $iCountTabButtons)

    GUICtrlCreateLabel($s_txt, 350, 200, 100, 25)
    GUICtrlSetFont(-1, 16)
    GUICtrlSetColor(-1, 0xffffff)
    GUICtrlCreateTabItem("")


    $iCountTabButtons += 1
    $aTabBarItems[0][0] = $iCountTabButtons
    $aTabBarItems[$iCountTabButtons][0] = $h_Tabbar
    $aTabBarItems[$iCountTabButtons][1] = $i_LabelTabBase
    $aTabBarItems[$iCountTabButtons][2] = $i_LabelTabTxt
    $aTabBarItems[$iCountTabButtons][3] = $i_LabelBtnTabBottom
    $aTabBarItems[$iCountTabButtons][4] = $i_LabelTabBtnX
    $aTabBarItems[$iCountTabButtons][5] = $BG_Color
    $aTabBarItems[$iCountTabButtons][6] = $LineColorDown
    $aTabBarItems[$iCountTabButtons][7] = 0x3D3D3D ; selected color
    $aTabBarItems[$iCountTabButtons][10] = $iX
    $aTabBarItems[$iCountTabButtons][11] = 0
    $aTabBarItems[$iCountTabButtons][12] = $iWtB + $iMinusCloseX
    $aTabBarItems[$iCountTabButtons][13] = 22
    $aTabBarItems[$iCountTabButtons][15] = 1
    $aTabBarItems[$iCountTabButtons][16] = 0 ; hover
    $aTabBarItems[$iCountTabButtons][17] = $i_TabItem

    _GuiCtrlTabButtonSetActive($i_LabelTabBase)
    _TabSetNewScrollMaxPos($hTabbar)
    _GoToTabButton($hTabbar, $iCountTabButtons)
    Return $i_LabelTabBase
EndFunc   ;==>_TabAdd

Func _GuiCtrlTabButtonDelete($i_Button)
    $iFind = _ArraySearch($aTabBarItems, $i_Button, 0, 0, 0, 2, 1, 1)
    If @error = 0 Then
        $iFirstVisible = _GetFirstVisible($hTabbar)
        _GUIScrollBars_SetScrollInfoPos($aTabBarItems[$iFind][0], $SB_HORZ, 0) ; reset scroll pos !!!!
        $aPosControlDelete = ControlGetPos($aTabBarItems[$iFind][0], "", $aTabBarItems[$iFind][1])
        GUICtrlDelete($aTabBarItems[$iFind][1])
        GUICtrlDelete($aTabBarItems[$iFind][2])
        GUICtrlDelete($aTabBarItems[$iFind][3])
        GUICtrlDelete($aTabBarItems[$iFind][4])
        GUICtrlDelete($aTabBarItems[$iFind][17])
        _ArrayDelete($aTabBarItems, $iFind)
        $aTabBarItems[0][0] = UBound($aTabBarItems, 1) - 1
        $iNewXPos = $aPosControlDelete[0]
        For $t = $iFind To $aTabBarItems[0][0]
            GUICtrlSetPos($aTabBarItems[$t][1], $iNewXPos)
            GUICtrlSetPos($aTabBarItems[$t][2], $iNewXPos)
            GUICtrlSetPos($aTabBarItems[$t][3], $iNewXPos)
            $aPosControl = ControlGetPos($aTabBarItems[$t][0], "", $aTabBarItems[$t][1])
            GUICtrlSetPos($aTabBarItems[$t][4], $aPosControl[0] + $aPosControl[2] - 10)
            $aTabBarItems[$t][10] = _GetGuiScrollStartXpoint($aTabBarItems[$t][0]) + $iNewXPos
            $iNewXPos = $aPosControl[0] + $aPosControl[2] + 1
        Next
        If $aTabBarItems[0][0] > 0 Then
            _TabSetNewScrollMaxPos($hTabbar)
            _GoToTabButton($hTabbar, $iFirstVisible)
        EndIf
    EndIf
EndFunc   ;==>_GuiCtrlTabButtonDelete


Func _GuiCtrlTabButtonSetActive($h_TabButton)
    $iFind = _ArraySearch($aTabBarItems, $h_TabButton, 0, 0, 0, 2, 1, 1)     ;, 1)
    If @error = 0 Then
        For $h = 1 To $aTabBarItems[0][0]
            If $aTabBarItems[$h][15] = 1 Then
                GUICtrlSetBkColor($aTabBarItems[$h][1], $aTabBarItems[$h][5])
                GUICtrlSetBkColor($aTabBarItems[$h][2], $aTabBarItems[$h][5])
                GUICtrlSetColor($aTabBarItems[$h][2], 0xcccccc)
                GUICtrlSetBkColor($aTabBarItems[$h][4], $aTabBarItems[$h][5])
                GUICtrlSetBkColor($aTabBarItems[$h][3], $aTabBarItems[$h][5])
                GUICtrlSetBkColor($aTabBarItems[$h][4], $aTabBarItems[$h][5])
                $aTabBarItems[$h][15] = 0
            EndIf
        Next

        GUICtrlSetBkColor($aTabBarItems[$iFind][1], $aTabBarItems[$iFind][7])
        GUICtrlSetBkColor($aTabBarItems[$iFind][2], $aTabBarItems[$iFind][7])
        GUICtrlSetColor($aTabBarItems[$iFind][2], 0xffffff)
        GUICtrlSetBkColor($aTabBarItems[$iFind][3], $aTabBarItems[$iFind][6])
        GUICtrlSetBkColor($aTabBarItems[$iFind][4], 0x55555)
        $aTabBarItems[$iFind][15] = 1
    EndIf
EndFunc   ;==>_GuiCtrlTabButtonSetActive




Func _GetTextWidth($s_Text, $i_FontSize, $i_FontWidth)
    Local $iX, $idListview
    $h_GuiGetTextWidth = GUICreate("ListView Approximate View Width", 10, 10, -100, 0)
    $idListview = GUICtrlCreateListView("", 2, 2, 394, 268)
    GUICtrlSetFont(-1, $i_FontSize, $i_FontWidth)
    GUISetState(@SW_HIDE)

    _GUICtrlListView_InsertColumn($idListview, 0, "Column 1", 100)
    _GUICtrlListView_AddItem($idListview, $s_Text)
    GUICtrlSendMsg($idListview, $LVM_SETCOLUMNWIDTH, 0, -1)      ; $LVSCW_AUTOSIZE

    $iX = _GUICtrlListView_GetColumnWidth($idListview, 0)
    GUIDelete($h_GuiGetTextWidth)
    Return $iX
EndFunc   ;==>_GetTextWidth



Func _TabSetNewScrollMaxPos($hWndScrollGui, $i_ShowWindow = 0)
    Local $iXpos = 0 ;
    $iXpos = $aTabBarItems[UBound($aTabBarItems) - 1][10] + $aTabBarItems[UBound($aTabBarItems) - 1][12] + 2

    GUISetState(@SW_LOCK, $hWndScrollGui)
    _GUIScrollBars_SetScrollInfoPos($hWndScrollGui, $SB_HORZ, 0) ; reset scroll pos !!!!
    $iMaxScrollPos = _GetScrollInfoMax($hWndScrollGui, $iXpos + 2)
    _GUIScrollBars_SetScrollInfoMax($hWndScrollGui, $SB_HORZ, $iMaxScrollPos)
    _GUIScrollBars_SetScrollInfoMin($hWndScrollGui, $SB_HORZ, 0)

    If _GUIScrollBars_GetScrollBarXYLineButton($hWndScrollGui, $OBJID_HSCROLL) > 0 Or _GUIScrollBars_GetScrollBarXYLineButton($hWndScrollGui, $OBJID_VSCROLL) > 0 Then
        _GUIScrollBars_ShowScrollBar($hWndScrollGui, $SB_HORZ, False)
        _GUIScrollBars_ShowScrollBar($hWndScrollGui, $SB_VERT, False)
    EndIf

    GUISetState(@SW_UNLOCK, $hWndScrollGui)
    If $i_ShowWindow = 1 Then GUISetState(@SW_SHOWNA, $hWndScrollGui)

    $aPosTab = WinGetPos($hWndScrollGui)
    If $iXpos <= $aPosTab[2] Then
        GUICtrlSetState($hButtonLeftScrollTabs, $GUI_HIDE)
        GUICtrlSetState($hButtonRightScrollTabs, $GUI_HIDE)
    Else
        GUICtrlSetState($hButtonLeftScrollTabs, $GUI_SHOW)
        GUICtrlSetState($hButtonRightScrollTabs, $GUI_SHOW)
    EndIf
EndFunc   ;==>_TabSetNewScrollMaxPos


Func _NextTab($hWndScrollGui)
    $iStartXpoint = _GetGuiScrollStartXpoint($hWndScrollGui)
    For $t = 1 To $aTabBarItems[0][0]
        If $iStartXpoint + 10 < $aTabBarItems[$t][10] Then
            _GoToTabButton($hWndScrollGui, $t)
            ExitLoop
        EndIf
    Next
EndFunc   ;==>_NextTab

Func _PreviewsTab($hWndScrollGui)
    $iStartXpoint = _GetGuiScrollStartXpoint($hWndScrollGui)
    If $iStartXpoint <= 0 Then Return
    For $t = $aTabBarItems[0][0] To 1 Step -1
        If $aTabBarItems[$t][10] < $iStartXpoint - 10 Then
            _GoToTabButton($hWndScrollGui, $t)
            ExitLoop
        EndIf
    Next
EndFunc   ;==>_PreviewsTab

Func _GoToTabButton($hWndScrollGui, $i_Tab)
    If $i_Tab > $aTabBarItems[0][0] Then Return
    _GUIScrollBars_SetScrollInfoPos($hWndScrollGui, $SB_HORZ, Floor(($aTabBarItems[$i_Tab][10] / _GetGuiScrollCharWidth($hWndScrollGui))))
EndFunc   ;==>_GoToTabButton

Func _GetScrollInfoMax($h_Gui, $iWith)
    Local $aCall = DllCall("user32.dll", "handle", "GetDC", "hwnd", $h_Gui)
    $hDC = $aCall[0]
    Local $tTEXTMETRIC = DllStructCreate($tagTEXTMETRIC)
    DllCall("gdi32.dll", "bool", "GetTextMetricsW", "handle", $hDC, "struct*", $tTEXTMETRIC)

    Local $iXAmount = DllStructGetData($tTEXTMETRIC, "tmAveCharWidth")
    Local $iYAmount = DllStructGetData($tTEXTMETRIC, "tmHeight") + DllStructGetData($tTEXTMETRIC, "tmExternalLeading")
    DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $h_Gui, "handle", $hDC)

    $iClW = _WinAPI_GetClientWidth($h_Gui)
    $iFullW = $iWith
    $iPageW = _GUIScrollBars_GetScrollInfoPage($h_Gui, $SB_HORZ)
    $iMaxValRng = Round(($iFullW - $iClW) / $iXAmount)
    $iMaxScrollPos = $iMaxValRng + ($iPageW - 1)
    If $iMaxScrollPos < 0 Then $iMaxScrollPos = 0
    Return $iMaxScrollPos
EndFunc   ;==>_GetScrollInfoMax

Func _GetFirstVisible($hWndScrollGui)
    $iStartXpoint = _GetGuiScrollStartXpoint($hWndScrollGui)
    For $t = 1 To $aTabBarItems[0][0]
        If $iStartXpoint + 10 > $aTabBarItems[$t][10] And $iStartXpoint + 10 < $aTabBarItems[$t][10] + $aTabBarItems[$t][12] Then
            Return $t
            ExitLoop
        EndIf
    Next
EndFunc   ;==>_GetFirstVisible

Func _GetGuiScrollStartXpoint($hWndScrollGui)
    $iGetScrollPos = _GUIScrollBars_GetScrollPos($hWndScrollGui, $SB_HORZ)
    $iH_Scroll_Pos = $iGetScrollPos * _GetGuiScrollCharWidth($hWndScrollGui)
    Return $iH_Scroll_Pos
EndFunc   ;==>_GetGuiScrollStartXpoint

Func _GetGuiScrollCharWidth($hWndScrollGui)
    Local $tTEXTMETRIC = DllStructCreate($tagTEXTMETRIC)
    ; Determine text size
    Local $hDC = DllCall("user32.dll", "handle", "GetDC", "hwnd", $hWndScrollGui)
    If Not @error Then
        $hDC = $hDC[0]
        DllCall("gdi32.dll", "bool", "GetTextMetricsW", "handle", $hDC, "ptr", DllStructGetPtr($tTEXTMETRIC))
        If @error Then
            $iError = @error
            $iExtended = @extended
            DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $hWndScrollGui, "handle", $hDC)
            Return SetError($iError, $iExtended, -2)
        EndIf
        DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $hWndScrollGui, "handle", $hDC)
    Else
        Return SetError(@error, @extended, -1)
    EndIf
    Return DllStructGetData($tTEXTMETRIC, "tmAveCharWidth")
EndFunc   ;==>_GetGuiScrollCharWidth

 

Edited by bladem2003
Posted

Excellent work. Thank you for sharing.

I never had much luck with tabs in AutoIt because of theming issues and things like that. But your script is nice for the fact that you can completely customize the colors of everything. This is very beneficial.

The only suggestion that I have at the moment is to have an option to not have a close button on the tabs.

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