'标准模块代码:
Option Explicit
'-------------------------------声明部分------------------------------
Private Type Guid
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
英语考研答案
End Type
Private Type DEV_BROADCAST_DEVICEINTERFACE
dbcc_size As Long
dbcc_devicetype As Long
dbcc_rerved As Long
dbcc_classguid As Guid
dbcc_name As Long
End Type
Private Type DEV_BROADCAST_VOLUME
dbcv_size As Long
dbcv_devicetype As Long
dbcv_rerved As Long
dbcv_unitmask As Long幼儿英语学习网
dbcv_flags As Integer
变形金刚1主题曲
End Type
Private Const GWL_WNDPROC = -4
Private Const DEVICE_NOTIFY_WINDOW_HANDLE = 0now什么意思
Private Const WM_DEVICECHANGE = &H219&
Private Const DBT_DEVTYP_DEVICEINTERFACE = &H5&
Private Const DBT_DEVTYP_VOLUME = &H2&
Private Const DBT_DEVICEARRIVAL = &H8000&
Private Const DBT_DEVICEREMOVECOMPLETE = &H8004&
Private Const DBTF_MEDIA = &H1&
competitivePrivate Const DRIVE_REMOVABLE = 2
Private Declare Function SetWindowLong Lib "Ur32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "Ur32.dll" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function RegisterDeviceNotification Lib "Ur32.dll" Alias "RegisterDeviceNotificationA" (ByVal hRecipient As Long, NotificationFilter As Any, ByVal Flags As Long) As Long
Private Declare Function UnregisterDeviceNotification Lib "Ur32.dll" (ByVal Handle As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
'-------------------------------实现部分------------------------------
Private m_hwnd As Long, m_lpPrevWndProc As Long
Private m_hDevNotify As Long
Public Sub RegDevNotify(ByVal hwnd As Long)
Dim dbcc As DEV_BROADCAST_DEVICEINTERFACE
If m_lpPrevWndProc = 0 Then
m_hwnd = hwnd
m_lpPrevWndProc = SetWindowLong(m_hwnd, GWL_WNDPROC, AddressOf WndProc)
dbcc.dbcc_size = Len(dbcc)
dbcc.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE
m_hDevNotify = RegisterDeviceNotification(hwnd, dbcc, DEVICE_NOTIFY_WINDOW_HANDLE)introduction是什么意思
End If
granny 50 60 70 videoEnd Sub
暂存
Public Sub UnregDevNotify()
If m_lpPrevWndProc Then
UnregisterDeviceNotification m_hDevNotify
SetWindowLong m_hwnd, GWL_WNDPROC, m_lpPrevWndProc
m_lpPrevWndProc = 0
End If
End Sub
Private Function WndProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If msg = WM_DEVICECHANGE Then
If wParam = DBT_DEVICEARRIVAL Or wParam = DBT_DEVICEREMOVECOMPLETE Then
Call GetDevInfo(wParam = DBT_DEVICEARRIVAL, lParam)
End If
End If
WndProc = CallWindowProc(m_lpPrevWndProc, m_hwnd, msg, wParam, lParam)
End Function
Private Sub GetDevInfo(ByVal bArrival As Boolean, ByVal lParam As Long)
Dim dbcv As DEV_BROADCAST_VOLUME, sDrv As String
CopyMemory dbcv, ByVal lParam, Len(dbcv)
If dbcv.dbcv_devicetype = DBT_DEVTYP_VOLUME And dbcv.dbcv_flags = DBTF_MEDIA Then
sDrv = GetDrvFromBit(dbcv.dbcv_unitmask)
If GetDriveType(sDrv & ":") = DRIVE_REMOVABLE Then
MsgBox "可移动磁盘" & sDrv & IIf(bArrival, "插入", "拨出")
End If
End If
End Sub
刘欣辩论Private Function GetDrvFromBit(ByVal nBits As Long) As String
Dim i As Long
For i = 0 To 25
vepIf nBits And (2 ^ i) Then
GetDrvFromBit = Chr(vbKeyA + i)
Exit Function
End If
Next
End Function
'窗体模块代码:
Option Explicit
'-------------------------------调用示例------------------------------
Private Sub Form_Load()
RegDevNotify Me.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnregDevNotify
End Sub