看看哪个是你要的效果:
Private Sub Form_Load()
With TreeView1
.Nodes.Add , , "root", "######"
.Nodes("root").Expanded = True
For i = 1 To 10
.Nodes.Add "root", 4, "a" & i, "************"
Next
.Nodes(5).BackColor = vbRed
.Nodes(7).ForeColor = vbBlue
End With
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSrc As Any, ByVal dwLen As Long)
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_WNDPROC = (-4)
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetProp Lib "user32" Alias "GetPropA" (ByVal hWnd As Long, ByVal lpString As String) As Long
Private Declare Function SetProp Lib "user32" Alias "SetPropA" (ByVal hWnd As Long, ByVal lpString As String, ByVal hData As Long) As Long
Private Declare Function RemoveProp Lib "user32" Alias "RemovePropA" (ByVal hWnd As Long, ByVal lpString As String) As Long
'Halftone created for default palette use
Private m_hpalHalftone As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function SetBkColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
Private Declare Function GetBkColor Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function GetTextColor Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function SelectPalette Lib "gdi32" (ByVal hDC As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
Private Declare Function RealizePalette Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function OleTranslateColor Lib "oleaut32.dll" (ByVal lOleColor As Long, ByVal lHPalette As Long, lColorRef As Long) As Long
Private Declare Function DrawIconEx Lib "user32" (ByVal hDC As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyHeight As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long
Private Declare Function FillRect Lib "user32" (ByVal hDC As Long, lpRect As RECT, ByVal hBrush As Long) As Long
'VB Errors
Private Const giINVALID_PICTURE As Integer = 481
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Type PAINTSTRUCT
hDC As Long
fErase As Long
rcPaint As RECT
fRestore As Long
fIncUpdate As Long
rgbReserved As Byte
End Type
Public Declare Function BeginPaint Lib "user32" (ByVal hWnd As Long, lpPaint As PAINTSTRUCT) As Long
Public Declare Function EndPaint Lib "user32" (ByVal hWnd As Long, lpPaint As PAINTSTRUCT) As Long
Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function InvalidateRect Lib "user32" (ByVal hWnd As Long, ByVal lpRect As Long, ByVal bErase As Long) As Long
Public Const WM_PAINT = &HF
Public Const WM_ERASEBKGND = &H14
Public Const WM_HSCROLL = &H114
Public Const WM_VSCROLL = &H115
Public Const WM_MOUSEWHEEL = &H20A
Public Function TranslateColor(inCol As Long) As Long
'A simple wrapper for OleTranslateColor
Dim retCol As Long
OleTranslateColor inCol, 0&, retCol
TranslateColor = retCol
End Function
Public Sub PaintNormalStdPic(ByVal hdcDest As Long, ByVal xDest As Long, ByVal yDest As Long, ByVal width As Long, ByVal Height As Long, ByVal picSource As Picture, ByVal xSrc As Long, ByVal ySrc As Long, Optional ByVal hPal As Long = 0)
Dim hdcTemp As Long
Dim hPalOld As Long
Dim hbmMemSrcOld As Long
Dim hdcScreen As Long
Dim hbmMemSrc As Long
'Validate that a bitmap was passed in
If picSource Is Nothing Then GoTo PaintNormalStdPic_InvalidParam
Select Case picSource.Type
Case vbPicTypeBitmap
If hPal = 0 Then
hPal = m_hpalHalftone
End If
hdcScreen = GetDC(0&)
'Create a DC to select bitmap into
hdcTemp = CreateCompatibleDC(hdcScreen)
hPalOld = SelectPalette(hdcTemp, hPal, True)
RealizePalette hdcTemp
'Select bitmap into DC
hbmMemSrcOld = SelectObject(hdcTemp, picSource.Handle)
'Copy to destination DC
BitBlt hdcDest, xDest, yDest, width, Height, hdcTemp, xSrc, ySrc, vbSrcCopy
'Cleanup
SelectObject hdcTemp, hbmMemSrcOld
SelectPalette hdcTemp, hPalOld, True
RealizePalette hdcTemp
DeleteDC hdcTemp
ReleaseDC 0&, hdcScreen
Case vbPicTypeIcon
'Create a bitmap and select it into an DC
'Draw Icon onto DC
DrawIconEx hdcDest, xDest, yDest, picSource.Handle, 0, 0, 0&, 0&, DI_NORMAL
Case Else
GoTo PaintNormalStdPic_InvalidParam
End Select
Exit Sub
PaintNormalStdPic_InvalidParam:
Err.Raise giINVALID_PICTURE
Public Sub TreeViewMessage(ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long, RetVal As Long, _
UseRetVal As Boolean)
'Prevent recursion with this variable
Static InProc As Boolean
Dim Ps As PAINTSTRUCT
Dim TVDC As Long, drawDC1 As Long, drawDC2 As Long
Dim oldBMP1 As Long, drawBMP1 As Long
Dim oldBMP2 As Long, drawBMP2 As Long
Dim X As Long, Y As Long, w As Long, H As Long
Dim TVWidth As Long, TVHeight As Long
If wMsg = WM_PAINT Then
If InProc = True Then
Exit Sub
End If
InProc = True
'Prepare some variables we'll use
TVWidth = Tvbg.width \ Screen.TwipsPerPixelX
TVHeight = Tvbg.Height \ Screen.TwipsPerPixelY
w = ScaleX(Img.Picture.width, vbHimetric, vbPixels)
H = ScaleY(Img.Picture.Height, vbHimetric, vbPixels)
'Begin painting. This API must be called in
'response to the WM_PAINT message or you'll see
'some odd visual effects :-)
Call BeginPaint(hWnd, Ps)
TVDC = Ps.hDC
'Create a few canvases in memory to
'draw on
drawDC1 = CreateCompatibleDC(TVDC)
drawBMP1 = CreateCompatibleBitmap(TVDC, TVWidth, TVHeight)
oldBMP1 = SelectObject(drawDC1, drawBMP1)
'This actually causes the TreeView to paint
'itself onto our memory DC!
SendMessage hWnd, WM_PAINT, drawDC1, ByVal 0&
'Tile the bitmap and draw the TreeView
'over it transparently
For Y = 0 To TVHeight Step H
For X = 0 To TVWidth Step w
PaintNormalStdPic drawDC2, X, Y, w, H, _
Img.Picture, 0, 0
Next
Next
PaintTransparentDC drawDC2, 0, 0, TVWidth, TVHeight, _
drawDC1, 0, 0, TranslateColor(vbWindowBackground)
'Draw to the target DC
BitBlt TVDC, 0, 0, TVWidth, TVHeight, _
drawDC2, 0, 0, vbSrcCopy
ElseIf wMsg = WM_HSCROLL Or wMsg = WM_VSCROLL Or wMsg = WM_MOUSEWHEEL Then
'Force a repaint to keep the bitmap
'tiles lined up
InvalidateRect hWnd, 0, 0