Thứ cung xin đóng góp chút ít
Cấu trúc chương trình (Theo thứ tự):
InitializeDemo1: Sub đầu tiên khởi nguồn cho mọi thứ trên đời. Sub khai báo một loạt các tinh chỉnh Root, các Keys. Ngay sau đó Sub sẽ dẫn đến
BuildRoot.
BuildRoot: Đây là Sub sẽ tạo ra thanh Keys gốc đầu tiên.
Bạn cần đăng nhập để thấy hình ảnh
Sau khi tạo xong nó sẽ dẫn đến dòng Code để gọi
BuildTree :
'Now add this root's children
If Not cRoot.ChildNodes Is Nothing Then
BuildTree cRoot, 1, True
End If
BuildTree: Sub sẽ đóng vai trò tạo ra các cấp độ con của Keys gốc.
Ví dụ trong đó, đây là đoạn Code tạo ra thanh xuống của Treeview trong Sub BuildTree
'Vertical line
If mbShowLines Then
If cNode.VLine Is Nothing Then
Set cNode.VLine = TreeControl.Controls.Add("Forms.label.1", "", False)
lVLineTopIdx = mlVisCount
With cNode.VLine
.Tag = "VLine"
.ZOrder 1
.Top = msngTopHV + (lVLineTopIdx - 1) * NodeHeight
.Left = msngLineLeft + msngRootLine + msngIndent * (lLevel - 1)
.Width = 0.75
.Height = NodeHeight
.Caption = ""
.BorderColor = vbScrollBars
.BorderStyle = fmBorderStyleSingle
.Visible = True
End With
Else
lVLineTopIdx = mlVisCount
cNode.VLine.Top = msngTopHV + (lVLineTopIdx - 1) * NodeHeight
If mbRedesign Then
cNode.VLine.Left = msngLineLeft + msngRootLine + msngIndent * (lLevel - 1)
End If
End If
End If
Bạn cần đăng nhập để thấy hình ảnh
Sau khi tạo thanh xuống, Sub BuildNodeControls sẽ được gọi.
BuildNodeControls cChild, lLevel
BuildNodeControls: Đây là đoạn code tạo thanh ngang trong Sub
BuildNodeControls
'Horizontal line
If mbShowLines Then
If cNode.HLine Is Nothing Then
Set cNode.HLine = TreeControl.Controls.Add("Forms.label.1", "", False)
With cNode.HLine
.Tag = "HLine"
.Left = msngLineLeft + msngRootLine + msngIndent * (lLevel - 1)
.Top = msngTopHV + mlVisCount * NodeHeight
.Width = msngIndent
.Height = 0.75
.Caption = ""
.BorderStyle = fmBorderStyleSingle
.BorderColor = vbScrollBars
.Visible = True
End With
Else
With cNode.HLine
If mbRedesign Then
.Left = msngLineLeft + msngRootLine + msngIndent * (lLevel - 1)
.Width = msngIndent
End If
.Top = msngTopHV + mlVisCount * NodeHeight
.Visible = True
End With
End If
End If
Bạn cần đăng nhập để thấy hình ảnh
Đây là đoạn code dùng để tạo tiêu đề trong sub
BuildNodeControls
'The node itself
If cNode.Control Is Nothing Then
Set cNode.Control = TreeControl.Controls.Add("Forms.label.1", "", False)
With cNode.Control
.Tag = "NodeLabel"
.WordWrap = False
.AutoSize = True
.Left = 3 + msngRootLine + msngIndent * lLevel + msngChkBoxPad + sngIconPad
.Top = 3 + msngTopLabel + mlVisCount * NodeHeight
If Not mbFullWidth And mbGotIcons Then
If cNode.hasIcon(vKey) Then
.PicturePosition = fmPicturePositionLeftCenter
.Picture = mcolIcons(vKey)
End If
End If
If cNode.Bold Then .Font.Bold = True
.WordWrap = False
.AutoSize = True
.Caption = cNode.Caption
cNode.TextWidth = .Width
If cNode.TextWidth + sngIconPad > msngMaxWidths(lLevel) Then
msngMaxWidths(lLevel) = cNode.TextWidth + sngIconPad
End If
If mbFullWidth Then
.AutoSize = False
If .Width <= 600 Then .Width = 600
End If
If cNode.BackColor Then
.BackColor = cNode.BackColor
End If
If cNode.ForeColor Then
.ForeColor = cNode.ForeColor
End If
.Visible = True
End With
mlNodesCreated = mlNodesCreated + 1
Else
With cNode.Control
If mbRedesign Then
.Left = 3 + msngRootLine + msngIndent * lLevel + sngIconPad + msngChkBoxPad
cNode.TextWidth = .Width
If .Width + sngIconPad > msngMaxWidths(0) Then
msngMaxWidths(lLevel) = .Width + sngIconPad
End If
End If
.Top = mlVisCount * NodeHeight + 3 + msngTopLabel
.Visible = True
End With
End If
Bạn cần đăng nhập để thấy hình ảnh
Sau khi tạo xong sẽ quay trả ta lại Sub
BuildTree
BuildTree:
Đây là đoạn Code sẽ tạo thanh sổ xuống
'Expand/collapse button text (or icon)
If cNode.Expander Is Nothing Then
Set cNode.Expander = TreeControl.Controls.Add("Forms.label.1", False)
With cNode.Expander
.Tag = "ExpText"
.Left = 6 + (lLevel - 2) * msngIndent + msngRootLine
.Top = (mlVisCount - 1) * NodeHeight + msngTopExpT
If mbExpanderImage Then
'Use an image
.BorderStyle = fmBorderStyleNone
.Picture = moExpanderImage(cNode.Expanded)
.PicturePosition = fmPicturePositionLeftTop
.Width = 7.5
.Height = 7.5
Else
'Plus or minus
.Width = 11.25
.Height = 11.25
If cNode.Expanded = True Then
.Caption = "-"
.Font.Bold = True
Else
.Caption = "+"
.Font.Bold = False
End If
.Font.Size = 10
.TextAlign = fmTextAlignCenter
.BackStyle = fmBackStyleTransparent
.Visible = True
End If
End With
Else
With cNode.Expander
If mbRedesign Then .Left = 6 + (lLevel - 2) * msngIndent + msngRootLine
.Top = (mlVisCount - 1) * NodeHeight + msngTopExpT
.Visible = True
End With
End If
Bạn cần đăng nhập để thấy hình ảnh
Cuối Code
BuildTree, nếu cấp độ tiếp theo bằng cấp độ hiện tại sẽ tạo nút sổ ngang:
' extend the vertical line
If mbShowLines Then
With cNode.VLine
.Height = (mlVisCount - lVLineTopIdx + 1) * msngNodeHeight
.Visible = True
End With
End If
Bạn cần đăng nhập để thấy hình ảnh
Đoạn Code này tăng dần cấp độ Keys đến cấp độ Keys cuối cùng còn sót lại của 1 nhánh
If Not cChild.ChildNodes Is Nothing Then
BuildTree cChild, lLevel + 1
End If
Có đoạn Code này Thứ chưa hiểu lắm, Tại Sub
BuildTree, Cuối code, tại sao khi code chạy đến Exit sub rồi thì nó lại quay ngược lại End if?. Nó đã đi qua vòng lặp rồi cơ mà, cơ chế nào làm cho nó quay ngược lại?
Bạn cần đăng nhập để thấy hình ảnh
Trước mắt, Thứ chỉ biết bấy nhiều, Code dài quá đi mất...!