当前位置:编程学习 > VB >>

VB高手进,VB程序详细解释。

Option Explicit

Implements iSubclass

Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
Alias "GetLogicalDriveStringsA" ( _
ByVal nBufferLength As Long, _
ByVal lpBuffer As String _
) As Long

Private m_clsSubcls As cSubclass

Private Sub cmdEject_Click()
If EjectDevice(lvwDrives.SelectedItem.Tag) Then
MsgBox "OK,USB设备安全弹出,您可以放心拔出U盘!", vbInformation, "U盘拔出"
Else
MsgBox "该驱动器不支持弹出 " & lvwDrives.SelectedItem.Tag & "!", vbExclamation, "提示"
End If
End Sub

Private Sub Form_Load()
Set m_clsSubcls = New cSubclass

m_clsSubcls.Subclass Me.hwnd, Me
m_clsSubcls.AddMsg Me.hwnd, WM_DEVICECHANGE

RefreshDriveList
End Sub

Private Sub Form_Unload(Cancel As Integer)
m_clsSubcls.Terminate
End Sub

Private Sub RefreshDriveList()
Dim strDriveBuffer As String
Dim strDrives() As String
Dim i As Long
Dim udtInfo As DEVICE_INFORMATION

strDriveBuffer = Space(240)
strDriveBuffer = Left$(strDriveBuffer, GetLogicalDriveStrings(Len(strDriveBuffer), strDriveBuffer))
strDrives = Split(strDriveBuffer, Chr$(0))

lvwDrives.ListItems.Clear

For i = 0 To UBound(strDrives) - 1
With lvwDrives.ListItems.Add(Text:=strDrives(i))
udtInfo = GetDevInfo(strDrives(i))

If udtInfo.Valid Then
Select Case udtInfo.BusType
Case BusTypeUsb: .SubItems(1) = "USB"
Case BusType1394: .SubItems(1) = "1394"
Case BusTypeAta: .SubItems(1) = "ATA"
Case BusTypeAtapi: .SubItems(1) = "ATAPI"
Case BusTypeFibre: .SubItems(1) = "Fibre"
Case BusTypeRAID: .SubItems(1) = "RAID"
Case BusTypeScsi: .SubItems(1) = "SCSI"
Case BusTypeSsa: .SubItems(1) = "SSA"
Case BusTypeUnknown: .SubItems(1) = "未知"
End Select

.SubItems(2) = IIf(udtInfo.Removable, "是", "否")
.SubItems(3) = Trim$(udtInfo.VendorID & " " & udtInfo.ProductID & " " & udtInfo.ProductRevision)

.Tag = strDrives(i)
End If
End With
Next
End Sub

Private Sub iSubclass_WndProc(ByVal bBefore As Boolean, bHandled As Boolean, lReturn As Long, ByVal lng_hWnd As Long, ByVal uMsg As eMsg, ByVal wParam As Long, ByVal lParam As Long, lParamUser As Long)
If uMsg = WM_DEVICECHANGE Then RefreshDriveList
End Sub

Private Sub lvwDrives_BeforeLabelEdit(Cancel As Integer)

End Sub

麻烦逐句解释,谢谢!分数可再加。

追问:可不可以再详细点啊?谢谢!!!再给多你100分,谢谢

答案:

Option Explicit '变量使用前选声明

Implements iSubclass '引入 iSubcass 类

Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
Alias "GetLogicalDriveStringsA" ( _
ByVal nBufferLength As Long, _
ByVal lpBuffer As String _
) As Long 'API函数,获得驱动器相关信息

Private m_clsSubcls As cSubclass '定义类为 cSubcass

Private Sub cmdEject_Click() '弹出按钮点击事件
If EjectDevice(lvwDrives.SelectedItem.Tag) Then '如果能够弹出
MsgBox "OK,USB设备安全弹出,您可以放心拔出U盘!", vbInformation, "U盘拔出"
Else
MsgBox "该驱动器不支持弹出 " & lvwDrives.SelectedItem.Tag & "!", vbExclamation, "提示"
End If
End Sub

Private Sub Form_Load()
Set m_clsSubcls = New cSubclass '初始化 cSubcass类为对象

m_clsSubcls.Subclass Me.hwnd, Me '传递当前窗体的句柄给cSubcass实例对象m_clsSubcls
m_clsSubcls.AddMsg Me.hwnd, WM_DEVICECHANGE '弹回消息处理

RefreshDriveList '更新驱动器列表
End Sub

Private Sub Form_Unload(Cancel As Integer) '窗体卸载
m_clsSubcls.Terminate '取消事件监听
End Sub

Private Sub RefreshDriveList() '更新驱动器列表
Dim strDriveBuffer As String '驱动器信息串
Dim strDrives() As String '驱动器数组
Dim i As Long
Dim udtInfo As DEVICE_INFORMATION '驱动器类型

strDriveBuffer = Space(240)
strDriveBuffer = Left$(strDriveBuffer, GetLogicalDriveStrings(Len(strDriveBuffer), strDriveBuffer)) '得到驱动器信息串
strDrives = Split(strDriveBuffer, Chr$(0)) '字串转数组

lvwDrives.ListItems.Clear '清除驱动器列表项

For i = 0 To UBound(strDrives) - 1 '遍历刚获得的驱动器各项
With lvwDrives.ListItems.Add(Text:=strDrives(i)) '为驱动器列表增加驱动器项并对该项进行设置
udtInfo = GetDevInfo(strDrives(i)) '获得驱动器类型

If udtInfo.Valid Then '如果是驱动器

Select Case udtInfo.BusType '检测类型并加入列表项的第二列中
Case BusTypeUsb: .SubItems(1) = "USB"
Case BusType1394: .SubItems(1) = "1394"
Case BusTypeAta: .SubItems(1) = "ATA"
Case BusTypeAtapi: .SubItems(1) = "ATAPI"
Case BusTypeFibre: .SubItems(1) = "Fibre"
Case BusTypeRAID: .SubItems(1) = "RAID"
Case BusTypeScsi: .SubItems(1) = "SCSI"
Case BusTypeSsa: .SubItems(1) = "SSA"
Case BusTypeUnknown: .SubItems(1) = "未知"
End Select

.SubItems(2) = IIf(udtInfo.Removable, "是", "否") '列表项第三项表示是否可移除
.SubItems(3) = Trim$(udtInfo.VendorID & " " & udtInfo.ProductID & " " & udtInfo.ProductRevision) '驱动器的其它信息

.Tag = strDrives(i) '加入该驱动标识
End If
End With
Next
End Sub

Private Sub iSubclass_WndProc(ByVal bBefore As Boolean, bHandled As Boolean, lReturn As Long, ByVal lng_hWnd As Long, ByVal uMsg As eMsg, ByVal wParam As Long, ByVal lParam As Long, lParamUser As Long)
If uMsg = WM_DEVICECHANGE Then RefreshDriveList '类事件,当驱动器发生变化时更新驱动器列表
End Sub

Private Sub lvwDrives_BeforeLabelEdit(Cancel As Integer)

End Sub

麻烦逐句解释,谢谢!分数可再加。

上一个:怎么用VB.NET编写计算器
下一个:那位VB高手帮我坐下VB试卷

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,