📄 treeview
字号:
VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form Form1
Caption = "TreeView 的基本操作"
ClientHeight = 5775
ClientLeft = 60
ClientTop = 345
ClientWidth = 7500
LinkTopic = "Form1"
ScaleHeight = 5775
ScaleWidth = 7500
StartUpPosition = 1 'CenterOwner
Begin VB.CommandButton Command4
Caption = "改变背景"
Height = 495
Left = 5880
TabIndex = 4
Top = 3960
Width = 1215
End
Begin MSComctlLib.ImageList ImageList1
Left = 6720
Top = 4560
_ExtentX = 1005
_ExtentY = 1005
BackColor = -2147483643
ImageWidth = 32
ImageHeight = 32
MaskColor = 12632256
_Version = 393216
BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628}
NumListImages = 1
BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628}
Picture = "TreeView的基本.frx":0000
Key = ""
EndProperty
EndProperty
End
Begin VB.CommandButton Command3
Caption = "删除节点"
Height = 495
Left = 5880
TabIndex = 3
Top = 2960
Width = 1215
End
Begin VB.CommandButton Command2
Caption = "增加分节点"
Height = 495
Left = 5880
TabIndex = 2
Top = 1960
Width = 1215
End
Begin VB.CommandButton Command1
Caption = "增加顶节点"
Height = 495
Left = 5880
TabIndex = 1
Top = 960
Width = 1215
End
Begin MSComctlLib.TreeView TreeView1
Height = 5295
Left = 240
TabIndex = 0
Top = 240
Width = 5175
_ExtentX = 9128
_ExtentY = 9340
_Version = 393217
LineStyle = 1
Style = 7
ImageList = "ImageList1"
Appearance = 1
End
Begin VB.Menu mnuPopUp
Caption = "aa"
Visible = 0 'False
Begin VB.Menu a1
Caption = "增加顶节点"
End
Begin VB.Menu a2
Caption = "增加分节点"
End
Begin VB.Menu a3
Caption = "删除节点"
End
Begin VB.Menu a4
Caption = "改变背景"
End
Begin VB.Menu end
Caption = "程序结束"
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim index As Integer
Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) 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_STYLE = -16&
Private Const TVM_SETBKCOLOR = 4381&
Private Const TVM_GETBKCOLOR = 4383&
Private Const TVS_HASLINES = 2&
Private Sub a1_Click()
add1
End Sub
Private Sub a2_Click()
add2
End Sub
Private Sub a3_Click()
del
End Sub
Private Sub a4_Click()
color
End Sub
Private Sub Command1_Click() '增加顶节点
add1
End Sub
Private Sub Command2_Click() '在你所点击的Node下新增分节点
add2
End Sub
Private Sub Command3_Click() '删除节点
del
End Sub
Private Sub Command4_Click()
color
End Sub
Private Sub end_Click()
End
End Sub
Private Sub Form_Load()
index = 1
add1
add1
End Sub
Private Sub TreeView1_Click() '点击TreeView时发生
index = TreeView1.SelectedItem.index '得到你所选中的节点
End Sub
Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) '单击右键时发生
Dim nod As Node
If Button = vbRightButton Then '检测鼠标的点击
Set nod = TreeView1.HitTest(x, y) '返回你所点击的Node对象的坐标,不过也可以不要(程序运行没有出现问题)
On Error GoTo EmptyNode
nod.Selected = True '设置你所点击的Node对象被选中,不过也可以不要(程序运行没有出现问题)
On Error GoTo 0
'<<下面是你的自定义菜单>>
Me.PopupMenu mnuPopUp
EmptyNode:
On Error GoTo 0
End If
End Sub
Sub add2() '增加分节点
On Error GoTo err
TreeView1.Nodes.Add index, tvwChild, , "分节点", 1 '增加顶节点
Exit Sub
err:
TreeView1.Nodes.Add , tvwLast, , "顶节点 ", 1 '增加顶节点
End Sub
Sub add1() '增加顶节点
TreeView1.Nodes.Add , tvwLast, , "顶节点", 1
End Sub
Sub del() '删除所选节点
On Error GoTo err
TreeView1.Nodes.Remove index '删除你所选中的节点
err:
Exit Sub
End Sub
Sub color()
Dim lngStyle As Long
Call SendMessage(TreeView1.hWnd, TVM_SETBKCOLOR, 0, ByVal RGB(255, 0, 0))
'改变背景到红色
lngStyle = GetWindowLong(TreeView1.hWnd, GWL_STYLE)
Call SetWindowLong(TreeView1.hWnd, GWL_STYLE, lngStyle - TVS_HASLINES)
Call SetWindowLong(TreeView1.hWnd, GWL_STYLE, lngStyle)
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -