实例介绍
【实例简介】
【实例截图】
【核心代码】
Option Explicit
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type PAINTSTRUCT
hDC As Long
fErase As Long
rcPaint As RECT
fRestore As Long
fIncUpdate As Long
rgbReserved As Byte
End Type
Private Declare Function BeginPaint Lib "user32" _
(ByVal hWnd As Long, lpPaint As PAINTSTRUCT) As Long
Private Declare Function EndPaint Lib "user32" _
(ByVal hWnd As Long, lpPaint As PAINTSTRUCT) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" _
(ByVal hDC As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" _
(ByVal hDC As Long, ByVal nWidth As Long, _
ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" _
(ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private 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
Private Declare Function DeleteObject Lib "gdi32" _
(ByVal hObject As Long) As Long
Private Declare Function InvalidateRect Lib "user32" _
(ByVal hWnd As Long, ByVal lpRect As Long, _
ByVal bErase As Long) As Long
Private Const WM_PAINT = &HF
Private Const WM_ERASEBKGND = &H14
Private Const WM_HSCROLL = &H114
Private Const WM_VSCROLL = &H115
Private Const WM_MOUSEWHEEL = &H20A
Private Sub Form_Load()
'Subclass the TreeView to trap messages
'that we'll need to respond to
Subclass Me, TreeView1
Dim Root As Node
'Add some items
With TreeView1.Nodes
Set Root = .Add(, , , "Top-level Node #1")
.Add Root.Index, tvwChild, , "Child Node #1"
.Add Root.Index, tvwChild, , "Child Node #2"
.Add Root.Index, tvwChild, , "Child Node #3"
Set Root = .Add(, , , "Top-level Node #2")
.Add Root.Index, tvwChild, , "Child Node #1"
.Add Root.Index, tvwChild, , "Child Node #2"
.Add Root.Index, tvwChild, , "Child Node #3"
Set Root = .Add(, , , "Top-level Node #3")
.Add Root.Index, tvwChild, , "Child Node #1"
.Add Root.Index, tvwChild, , "Child Node #2"
.Add Root.Index, tvwChild, , "Child Node #3"
Set Root = .Add(, , , "Top-level Node #4")
.Add Root.Index, tvwChild, , "Child Node #1"
.Add Root.Index, tvwChild, , "Child Node #2"
.Add Root.Index, tvwChild, , "Child Node #3"
End With
End Sub
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 = TreeView1.width \ Screen.TwipsPerPixelX
TVHeight = TreeView1.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)
drawDC2 = CreateCompatibleDC(TVDC)
drawBMP2 = CreateCompatibleBitmap(TVDC, TVWidth, TVHeight)
oldBMP2 = SelectObject(drawDC2, drawBMP2)
'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
'Cleanup
SelectObject drawDC1, oldBMP1
SelectObject drawDC2, oldBMP2
DeleteObject drawBMP1
DeleteObject drawBMP2
EndPaint hWnd, ps
RetVal = 0
UseRetVal = True
InProc = False
ElseIf wMsg = WM_ERASEBKGND Then
'Return TRUE
RetVal = 1
UseRetVal = True
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
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
MsgBox "If you liked it then please vote for me.", , "Treeview Example"
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Kill subclassing routine for exit
UnSubclass TreeView1
End Sub
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论