⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 USB设备监测插入及安全弹出 demo
💻 FRM
字号:
VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form Form1 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "USB设备监测插入及安全弹出示例"
   ClientHeight    =   3945
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   9165
   BeginProperty Font 
      Name            =   "Tahoma"
      Size            =   8.25
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   Icon            =   "Form1.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3945
   ScaleWidth      =   9165
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton cmdEject 
      Caption         =   "安全弹出移动设备"
      Height          =   390
      Left            =   7080
      TabIndex        =   1
      Top             =   3480
      Width           =   1950
   End
   Begin MSComctlLib.ListView lvwDrives 
      Height          =   3285
      Left            =   75
      TabIndex        =   0
      Top             =   75
      Width           =   8985
      _ExtentX        =   15849
      _ExtentY        =   5794
      View            =   3
      LabelEdit       =   1
      LabelWrap       =   -1  'True
      HideSelection   =   0   'False
      FullRowSelect   =   -1  'True
      _Version        =   393217
      ForeColor       =   -2147483640
      BackColor       =   8438015
      BorderStyle     =   1
      Appearance      =   1
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      NumItems        =   4
      BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
         Text            =   "驱动器"
         Object.Width           =   1041
      EndProperty
      BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
         SubItemIndex    =   1
         Text            =   "总线类型"
         Object.Width           =   1481
      EndProperty
      BeginProperty ColumnHeader(3) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
         SubItemIndex    =   2
         Text            =   "可移动"
         Object.Width           =   1834
      EndProperty
      BeginProperty ColumnHeader(4) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
         SubItemIndex    =   3
         Text            =   "名称"
         Object.Width           =   5186
      EndProperty
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'****************************************************************************
'安全警戒线整理
'网    站:http://www.hackeroo.com/
'e-mail  :hackeroo@hotmail.com
'OICQ    :266370
'****************************************************************************

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -