﻿;-- TIME_STAMP   2023-03-14 18:17:59

#include <FontConstants.au3>
#include <GuiTab.au3>
#include <WinAPIGdi.au3>
#include <WinAPISys.au3>
#include <WinAPIMisc.au3>
#include <WindowsConstants.au3>


HotKeySet("!{F8}", _End)            ; <Alt><F8> Beenden

AdlibRegister(_CheckSciTE)

Global $gm_SciTE[]
$gm_SciTE.ExistsLast = 0
$gm_SciTE.ExistsCurr = 0


While True
    Sleep(50)
WEnd

Func _End()
    AdlibUnRegister(_CheckSciTE)
    MsgBox(0, 'SciTE TabItem', 'Beendet!')
    Exit
EndFunc


Func _CheckSciTE()
    If ProcessExists("SciTE.exe") Then
        $gm_SciTE.ExistsCurr = 1
        If $gm_SciTE.ExistsLast = 0 Then
            $gm_SciTE.ExistsLast = 1
            _DrawTabItem()
            AdlibRegister(_MouseHoversTab, 150)
        EndIf
    Else
        $gm_SciTE.ExistsCurr = 0
        If $gm_SciTE.ExistsLast = 1 Then
            $gm_SciTE.ExistsLast = 0
            AdlibUnRegister(_MouseHoversTab)
        EndIf
    EndIf
EndFunc


Func _MouseHoversTab()
    Local Static $iHoverLast = 0
    Local $mTab = _SciTE_GetTabInfo()
    If @error Then Return
    Local $tPoint = _WinAPI_GetMousePos(True, $mTab.hTab)
    Local $tRect = $mTab.RectItem
    Local $isHover = _WinAPI_PtInRect($tRect, $tPoint)
    If $isHover = 1 And $iHoverLast = 0 Then
        $iHoverLast = 1
    ElseIf $isHover = 0 And $iHoverLast = 1 Then
        $iHoverLast = 1
        Return _DrawTabItem()
    EndIf
EndFunc

Func _DrawTabItem()
    Local $mTab = _SciTE_GetTabInfo()
    If @error Then Return
    Local $hDC = _WinAPI_GetDC($mTab.hTab)
    Local $hFont = _WinAPI_CreateFont(15, 0, 0, 0, 400, False, False, False, $DEFAULT_CHARSET, _
                $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Arial')
    Local $hOldFont = _WinAPI_SelectObject($hDC, $hFont)
    Local $tRect = $mTab.RectItem   ; in Variable extrahieren - sonst kein ByRef-Zugriff mit _DrawText möglich
    _WinAPI_SetTextColor($hDC, 0xAA0020)
    _WinAPI_SetBkColor($hDC, 0xFFFFFF)
    _WinAPI_DrawText($hDC, $mTab.Item, $tRect, BitOR($DT_BOTTOM,$DT_CENTER))
    _WinAPI_SelectObject($hDC, $hOldFont)
    _WinAPI_DeleteObject($hFont)
    _WinAPI_ReleaseDC(0, $hDC)
EndFunc


Func _SciTE_GetTabInfo()
    Local $hScite, $aResult, $aChild, $hWndTab = Null
    Local $mResult[]
    $hScite = WinGetHandle('[ACTIVE]')
    $aResult = DllCall("user32.dll", "int", "GetClassNameW", "hwnd", $hScite, "wstr", "", "int", 4096)
    If Not @error And $aResult[2] = 'SciTEWindow' Then
        $aChild = _WinAPI_EnumChildWindows($hScite) ; only visible
        If Not @error Then
            For $i = 1 To $aChild[0][0]
                If $aChild[$i][1] = "SciTeTabCtrl" Then
                    $hWndTab = $aChild[$i][0]
                    ExitLoop
                EndIf
            Next
        EndIf
        If $hWndTab <> Null Then
            $mResult.hSciTE   = $hScite
            $mResult.Title    = WinGetTitle($hScite)
            $mResult.hTab     = $hWndTab
            $mResult.RectTab  = _WinAPI_GetWindowRect($hWndTab)
            $mResult.Style    = _WinAPI_GetWindowLong ($hWndTab, $GWL_STYLE)
            $mResult.StyleEx  = _WinAPI_GetWindowLong ($hWndTab, $GWL_EXSTYLE)
            $mResult.Index    = _GUICtrlTab_GetCurFocus($hWndTab)
            $mResult.Item     = _GUICtrlTab_GetItemText($hWndTab, $mResult.Index)
            $mResult.RectItem = _GUICtrlTab_GetItemRectEx($hWndTab, $mResult.Index)
            Return $mResult
        EndIf
    EndIf
    Return SetError(1)
EndFunc