Lập trình VBA tạo File Explorer với BSListView của BSAC - Listview unicode

Liên hệ QC

Nguyễn Duy Tuân

Nghị Hách
Thành viên danh dự
Tham gia
13/6/06
Bài viết
4,650
Được thích
10,138
Giới tính
Nam
Nghề nghiệp
Giáo viên, CEO tại Bluesofts
Lập trình VB6 trước đây người ta rất hay dùng ListView, lập trình trong VBA hình như rất ít người nói đến nó. Lý do ListView là control nằm trong bộ MSCOMCTL.ocx của Microsoft dừng phát triển ở phiên bản 6.0, chỉ hỗ trợ làm ứng dụng 32-bit, không hỗ trợ Unicode. Tôi tạo ra BSListView trong bộ BSAC.ocx hỗ trợ tạo ứng dụng cả 32 và 64-bit, unicode với phương pháp lập trình thuận tiện và dễ dàng, đa dạng về hình thức. Ví dụ dưới đây là cách lập trình BSListView trong VBA để tạo ra giao diện giống như ứng dụng File Explorer của Windows.

FileExplorer1.png


FileExplorer2.png


Hàm chính tạo dữ liệu trên BSListView
Mã:
Private Sub GetAllFiles(sPath As String, ByVal GetFolder As Boolean)
    Dim Files, bf As New BSFunctions
    Dim cl As BSListColumn, li As BSListItem, Idx&, Ext$
    Dim I&, J&, s$
    Dim hIcon, hsmIcon
    ' Type of Files is array 2D= (1..m, 1...n)
    Files = GetFiles(Path:=sPath, Folder:=GetFolder, SubFolder:=False)
    'Add column header
    If BSListView1.Columns.Count = 0 Then 'Create columns for the first time
        For J = LBound(Files, 2) To UBound(Files, 2)
            If J <> 2 Then 'ignore index 2
                BSListView1.Columns.Add Files(LBound(Files, 1), J)
            End If
        Next J
        BSListView1.AutoColumns 'resize columns
    End If
    'Add items
    BSListView1.Items.BeginUpdate
    For I = LBound(Files, 1) + 1 To UBound(Files, 1) 'Get rows
        Ext = Files(I, 3) 'It is key to find image
        If GetFolder Then
            Ext = "folder"
        ElseIf Ext = ".ico" Then
            Idx = iml32.ListImages.AddIcon(sPath & "\" & Files(I, 1)) 'Create and Add icon to Imagelist size 32
            Idx = iml16.ListImages.AddIcon(sPath & "\" & Files(I, 1)) 'Create and Add icon to Imagelist size 16
        ElseIf Ext <> ".exe" And Ext <> ".ico" Then
            Idx = iml16.ListImages.IndexOf(Ext)
        Else
            Idx = -1 'let find icon
        End If

        If Idx = -1 Then 'not exists then add new icon for file type
            If GetAssociatedIcon(sPath & "\" & Files(I, 1), hIcon, hsmIcon) > 0 Then
                Idx = iml32.ListImages.AddIcon(hIcon, Ext) 'Add icon to Imagelist size 32
                iml16.ListImages.AddIcon hsmIcon, Ext 'Add icon to Imagelist size 16
            End If
        End If
      
        Set li = BSListView1.Items.Add(Files(I, 1), Idx)
        For J = 3 To UBound(Files, 2) 'Get columns
            li.SubItems.Add Files(I, J)
        Next J
    Next I
    BSListView1.Items.EndUpdate
End Sub

Toàn bộ mã nguồn và video hướng dẫn tạo để chạy tại đây:
http://atoolspro.com/create-file-explorer-by-bslistview-and-bsimagelist-bsac-controls.html
 
Web KT
Back
Top Bottom