Jump to content

Transparent Tree View


SkellySoul
 Share

Recommended Posts

Hello Everyone :)

This Transparent Tree View is something that has been requested by a few people in the past and most if not all have never found the answer to this problem but finally it has been solved so far...at first I didn't think it would be possible and it wouldn't have been possible without help from the following people.

So I would like to give a big thanks to

Tvern

Melba23

Guinness

Rover

Example 1

Posted Image

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <WinAPI.au3>
#include <GUITreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>

Global $iTreeView_Flag = False
Global $hChild, $hGUI, $hTreeView, $iTreeView, $iTreeView_Child, $iTreeView_Parent

$hGUI = GUICreate("Test", 800, 500)
GUICtrlCreatePic(@ScriptDir & "\Test.jpg", 0, 0, 800, 500)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState(@SW_SHOW, $hGUI)

$hChild = GUICreate("", 300, 200, 10, 10, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $hGUI)
GUISetBkColor(0xABCDEF, $hChild)
$iTreeView = GUICtrlCreateTreeView(0, 0, 300, 200, BitOR($TVS_HASBUTTONS, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS))
$hTreeView = GUICtrlGetHandle($iTreeView)
GUICtrlSetBkColor($iTreeView, 0xABCDEF)
_WinAPI_SetLayeredWindowAttributes($hChild, 0xABCDEF, 255)
GUISetState(@SW_SHOW, $hChild)

GUISwitch($hGUI)

For $iParent = 0 To 4
    $iTreeView_Parent = GUICtrlCreateTreeViewItem($iParent, $iTreeView)
    For $iChild = 0 To 4
        $iTreeView_Child = GUICtrlCreateTreeViewItem($iParent & "." & $iChild, $iTreeView_Parent)
        For $iChild2 = 0 To 4
            GUICtrlCreateTreeViewItem($iParent & "." & $iChild & "." & $iChild2, $iTreeView_Child)
        Next
    Next
Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch

    If $iTreeView_Flag Then
        $iTreeView_Flag = False
        ConsoleWrite(_GUICtrlTreeView_GetText($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView)) & @CRLF)
        WinSetTitle($hGUI, $hGUI, "You Clicked On: " & _GUICtrlTreeView_GetText($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView)))
    EndIf

WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    Local $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    Local $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hTreeView
            Switch $iCode
                Case $NM_CLICK
                    $iTreeView_Flag = True
            EndSwitch
    EndSwitch
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>WM_NOTIFY

Example 2

Posted Image

;modified from example by Tvern and Melba23
#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <WINAPI.au3>
#include <GUITreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
#include <FontConstants.au3>

Opt('MustDeclareVars', 1)

Global $iTransColor = 0xDADADA ;experiment to get the best look

Global $sFilePath = @ScriptDir & "\BRUSHED-STAINLESS-CMYK.jpg"
If FileExists($sFilePath) = 0 Then
    Global $sURL = "http://www.dreamstime.com/brushed-metal-thumb2509190.jpg"
    InetGet($sURL, $sFilePath)
EndIf
Global $GUI_Parent, $GUI_Child, $Msg, $Tree, $Tree_Parent, $Tree_Handle, $Tree_Child, $Tree_Child2, $iCol, $hDC
Global $hLastItem = 0, $fTVEdit = False, $cPic, $hPic
$GUI_Parent = GUICreate("Transparent Treeview", 300, 300)
$cPic = GUICtrlCreatePic($sFilePath, 0, 0, 300, 300)
;pic must be enabled for treeview item label blank space clicking
$hPic = GUICtrlGetHandle(-1)

;$TVS_TRACKSELECT ; adds underline to item text
$GUI_Child = GUICreate("", 280, 280, 10, 10, BitOR($WS_CLIPCHILDREN, $WS_POPUP), BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $GUI_Parent)
_WinAPI_SetLayeredWindowAttributes($GUI_Child, $iTransColor, 255)
$Tree = GUICtrlCreateTreeView(0, 0, 280, 280, _
BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_CHECKBOXES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $WS_TABSTOP))
GUICtrlSetBkColor($Tree, $iTransColor)
_GUICtrlTreeView_SetLineColor($Tree, 0xFFFFFF)
$Tree_Handle = GUICtrlGetHandle($Tree)
GUISwitch($GUI_Parent)


GUIRegisterMsg($WM_SETCURSOR, "WM_SETCURSOR")
;GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ;optional reseting highlight when editing items
GUISetState(@SW_SHOW, $GUI_Child)
GUISetState(@SW_SHOW, $GUI_Parent)

GUICtrlSendMsg($Tree, $WM_SETREDRAW, 0, 0)
For $iParent = 0 To 4
    $Tree_Parent = GUICtrlCreateTreeViewItem($iParent & " New Item >              <", $Tree)
    For $iChild = 0 To 4
        $Tree_Child = GUICtrlCreateTreeViewItem($iParent & "." & $iChild & " >               <", $Tree_Parent)
        For $iChild2 = 0 To 4
            $Tree_Child2 = GUICtrlCreateTreeViewItem($iParent & "." & $iChild & "." & $iChild2 & " >              <", $Tree_Child)
        Next
    Next
Next
GUICtrlSendMsg($Tree, $WM_SETREDRAW, 1, 0)


While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd


Func WM_SETCURSOR($hWnd, $iMsg, $wParam, $lParam)
    ;transparent treeview item hot track workaround
    ;coded by rover 2k11
    #forceref $hWnd, $iMsg, $wParam, $lParam
    Local $tPoint, $tHitTest, $hItem
    If $wParam = $hPic And $fTVEdit = False Then ;mouse over treeview background image
        $tPoint = _WinAPI_GetMousePos(True, $Tree_Handle)
        $tHitTest = _GUICtrlTreeView_HitTestEx($Tree_Handle, DllStructGetData($tPoint, "X"), DllStructGetData($tPoint, "Y"))
        If BitAND(DllStructGetData($tHitTest, "Flags"), $TVHT_ONITEMLABEL) <> 0 Then
            $hItem = DllStructGetData($tHitTest, "Item")
            If IsPtr($hItem) = 0 Then Return $GUI_RUNDEFMSG
            If BitAND(_GUICtrlTreeView_GetState($Tree_Handle, $hItem), $TVIS_SELECTED) <> $TVIS_SELECTED Then
                If $hLastItem <> $hItem Then
                    _GUICtrlTreeView_SetState($Tree_Handle, $hItem, $TVIS_DROPHILITED, 1)
                    If IsPtr($hLastItem) <> 0 Then _GUICtrlTreeView_SetState($Tree_Handle, $hLastItem, $TVIS_DROPHILITED, 0)
                    $hLastItem = $hItem
                EndIf
            EndIf
        Else
            If IsPtr($hLastItem) <> 0 Then
                _GUICtrlTreeView_SetState($Tree_Handle, $hLastItem, $TVIS_DROPHILITED, 0)
                $hLastItem = 0
            EndIf
        EndIf
    ElseIf $wParam = $Tree_Handle And $fTVEdit = False Then ;mouse over treeview elements
        $tPoint = _WinAPI_GetMousePos(True, $Tree_Handle)
        $tHitTest = _GUICtrlTreeView_HitTestEx($Tree_Handle, DllStructGetData($tPoint, "X"), DllStructGetData($tPoint, "Y"))
        If BitAND(DllStructGetData($tHitTest, "Flags"), $TVHT_ONITEMLABEL) <> 0 Then
            $hItem = DllStructGetData($tHitTest, "Item")
            If IsPtr($hItem) = 0 Then Return $GUI_RUNDEFMSG
            If BitAND(_GUICtrlTreeView_GetState($Tree_Handle, $hItem), $TVIS_SELECTED) = $TVIS_SELECTED Then
                If IsPtr($hLastItem) <> 0 Then
                    _GUICtrlTreeView_SetState($Tree_Handle, $hLastItem, $TVIS_DROPHILITED, 0)
                    $hLastItem = 0
                EndIf
            EndIf
        EndIf
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_SETCURSOR



;optional
;=======================================================================================

; Font Quality
;~ Global Const $DEFAULT_QUALITY = 0x0
;~ Global Const $DRAFT_QUALITY = 0x1
;~ Global Const $PROOF_QUALITY = 0x2
;~ Global Const $NONANTIALIASED_QUALITY = 0x3
;~ Global Const $ANTIALIASED_QUALITY = 0x4
;~ Global Const $CLEARTYPE_QUALITY = 0x05

;you could use the pixel functions or read from the the bitmap to get the approx. colour of background
;of the treeview item area for $iTransColor.
;if you cannot get a good blend of font to background, especially if the background is not a texture
;(e.g. picture), then set font with _CtrlGetFont()/GUICtrlSetFont() and set font quality with
;CLEARTYPE_QUALITY, ANTIALIASED_QUALITY or NONANTIALIASED_QUALITY.
;The results are not as good as a smoothed font, and you will need to add a WM_THEMECHANGED message handler
;to call _CtrlGetFont() and GUICtrlSetFont(), as the system control font can change with the theme.


;optional set font and get pixel functions
;~ Local $aFont = _CtrlGetFont(-1) ;get current font, either system font for gui elements or font set by theme
;~ GUICtrlSetFont($Tree, $aFont[1], $aFont[2], $aFont[3], $aFont[4], $CLEARTYPE_QUALITY)

;~ For $i = 0 To UBound($aFont) -1
;~  ConsoleWrite("+> $aFont[" & $i & "]: "&$aFont[$i] & @CRLF)
;~ Next
;~ ConsoleWrite(@CRLF)

;~ Opt("PixelCoordMode", 2);2=client
;~ $iCol = PixelGetColor(15, 20)
;~ ConsoleWrite('+PixelGetColor: 0x' & Hex($iCol, 6) & @CRLF)

;~ $hDC = _WinAPI_GetDC($hPic)
;~ $iCol = _WinAPI_GetPixel($hDC, 15, 20)
;~ _WinAPI_ReleaseDC($hPic, $hDC)
;~ ConsoleWrite('+GetPixel: 0x' & Hex($iCol, 6) & @CRLF)



Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hItem, $tINFO
    #forceref $hWndFrom
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $iIDFrom
        Case $Tree
            Switch $iCode
                Case $TVN_BEGINLABELEDITW
                    _GUICtrlTreeView_SetState($Tree_Handle, $hLastItem, $TVIS_DROPHILITED, 0)
                    $hLastItem = 0
                    $fTVEdit = True ;prevents other items from mouse highlighting when item in edit mode
                Case $TVN_ENDLABELEDITW ;clear item selection if edit cancelled by click on other item
                    $tINFO = DllStructCreate($tagNMTVDISPINFO, $lParam)
                    $hItem = DllStructGetData($tINFO, "hItem")
                    If $hItem <> $hLastItem Then
                        _GUICtrlTreeView_SetState($Tree_Handle, $hItem, $TVIS_FOCUSED, 0)
                        _GUICtrlTreeView_SetState($Tree_Handle, $hItem, $TVIS_SELECTED, 0)
                    EndIf
                    $fTVEdit = False
                    $hLastItem = 0
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY


Func _CtrlGetFont($hWnd)
    ;modified from __GUIGetFont()in _GUICtrlCreateTFLabel.au3
    ;Author G.Sandler a.k.a MrCreatoR
    ;Forum link...:  http://www.autoitscript.com/forum/index.php?showtopic=96986
    ; [0] - 5
    ; [1] - Size
    ; [2] - Weight
    ; [3] - Attribute
    ; [4] - Name
    ; [5] - Quality
    Local $Ret, $hDC, $hFont, $tFont
    Local $aFont[1] = [0]
    If IsHWnd($hWnd) = 0 Then $hWnd = GUICtrlGetHandle($hWnd)
    $hDC = _WinAPI_GetDC($hWnd)
    $hFont = _SendMessage($hWnd, $WM_GETFONT)
    $tFont = DllStructCreate($tagLOGFONT)
    $Ret = DllCall('gdi32.dll', 'int', 'GetObjectW', 'ptr', $hFont, 'int', DllStructGetSize($tFont), 'ptr', DllStructGetPtr($tFont))
    If (Not @error) And ($Ret[0]) Then
        Dim $aFont[6] = [5]
        $aFont[1] = -Round(DllStructGetData($tFont, 'Height') / _WinAPI_GetDeviceCaps($hDC, $LOGPIXELSY) * 72, 1)
        $aFont[2] = DllStructGetData($tFont, 'Weight')
        $aFont[3] = BitOR(2 * (DllStructGetData($tFont, 'Italic') <> 0), 4 * (DllStructGetData($tFont, 'Underline') <> 0), 8 * (DllStructGetData($tFont, 'Strikeout') <> 0))
        $aFont[4] = DllStructGetData($tFont, 'FaceName')
        $aFont[5] = DllStructGetData($tFont, 'Quality')
    EndIf
    _WinAPI_ReleaseDC($hWnd, $hDC)
    Return $aFont
EndFunc   ;==>_CtrlGetFont


; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_GetPixel
; Description....: Retrieves the color value of the pixel at the specified coordinates.
; Syntax.........: _WinAPI_GetPixel ( $hDC, $iX, $iY )
; Parameters.....: $hDC    - Handle to the device context.
;                  $iX     - The x-coordinate, in logical units, of the pixel to be examined.
;                  $iY     - The y-coordinate, in logical units, of the pixel to be examined.
; Return values..: Success - The color of the pixel, in RGB.
;                  Failure - (-1) and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: None
; Related........:
; Link...........: @@MsdnLink@@ GetPixel
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_GetPixel($hDC, $iX, $iY)
    Local $Ret = DllCall('gdi32.dll', 'dword', 'GetPixel', 'hwnd', $hDC, 'int', $iX, 'int', $iY)
    If (@error) Or ($Ret[0] = 0xFFFFFFFF) Then
        Return SetError(1, 0, -1)
    EndIf
    Return _WinAPI_SwitchColor($Ret[0])
EndFunc   ;==>_WinAPI_GetPixel

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_SwitchColor
; Description....: Converts a color from BGR to RGB and vice versa.
; Syntax.........: _WinAPI_SwitchColor ( $iColor )
; Parameters.....: $iColor - The color to conversion.
; Return values..: Converted color (RGB or BGR - depends on the $iColor value, BGR > RGB > BGR etc).
; Author.........: Yashied
; Modified.......:
; Remarks........: None
; Related........:
; Link...........: None
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_SwitchColor($iColor)
    Return BitOR(BitAND($iColor, 0x00FF00), BitShift(BitAND($iColor, 0x0000FF), -16), BitShift(BitAND($iColor, 0xFF0000), 16))
EndFunc   ;==>_WinAPI_SwitchColor

Download both Examples + Resources

Hope everyone enjoys this example and please feel free to improve on this example to make it a full UDF ;)

Link to comment
Share on other sites

  • 6 months later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...