Lấy danh sách máy in (printer) vào ComboBox có ảnh - Lập trình VBA với BSAC

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,649
Đượ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 VBA trên Userform với ComboBox thông thường không thể tạo màu sắc hay hình ảnh trong các dòng (items). Với BSComboBox của BSAC activeX controls chúng ta dễ dàng đưa màu sắc và hình ảnh vào từng dòng rất đẹp và chuyên nghiệp.
Ví dụ trong video này tôi lấy danh sách các máy in (printer) trong máy tính đưa vào ComboBox có ảnh (image, icon).
Mã:
Private Sub UserForm_Initialize()
    Dim HIcon As Long, I&, n As Long
    'Setup ImageList for display icon in Combobox
    BSImageList1.ListImages.Clear
    BSImageList1.PicHeight = 32
    BSImageList1.PicWidth = 32
    BSImageList1.UseImageListDraw = True 'BSAC v2.0.0.6 - 05-04-2019
    For I = 105 To 108 'get icons from "shell32.dll" and put them to BSImageList
        HIcon = ExtractIcon(Application.HinstancePtr, GetSysDir & "\shell32.dll", I)
        BSImageList1.ListImages.AddIcon HIcon
        DestroyIcon HIcon
    Next I
    'Link BSImageList to BSCombobox
    Set BSComboBox1.ImageList = BSImageList1
    'Setup BSCombobox display image in item
    BSComboBox1.Style = csExpertAutoBuild
    BSComboBox1.ItemHeight = 32
    CollectPrinters
End Sub

Sub CollectPrinters()
    Dim sComputer As String, I&
    Dim objWMIService As Object, objListPrinters As Object, objPrinter As Object

    sComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
                                  & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")

    Set objListPrinters = objWMIService.ExecQuery _
                               ("Select * from Win32_Printer")
    BSComboBox1.Clear
    For Each objPrinter In objListPrinters
        BSComboBox1.Items.Add objPrinter.Name, GetImageIdx(objPrinter)
    Next
End Sub

Mã nguồn đầy đủ: http://atoolspro.com/list-printer-to-combobox-have-image-icon-bsac-controls.html
 
Web KT
Back
Top Bottom