帮助文档就有此类操作,你可以对照参考再修改
; rasim
; http://www.autoitscript.com/forum/topic/79460-usbmon/
#include <GUIConstantsEx.au3>
Global Const $WM_DEVICECHANGE = 0x0219
Global Const $DBT_DEVICEARRIVAL = 0x8000 ; 查找新设备
Global Const $DBT_DEVICEREMOVECOMPLETE = 0x8004 ; 禁用设备
Global Const $DBT_DEVTYP_VOLUME = 0x00000002 ; 逻辑驱动器
Global $k = 0
$Gui = GUICreate("WM_DEVICECHANGE", 370, 140)
GUICtrlCreateLabel('连接或断开设备时发送 WM_DEVICECHANGE 消息.' & @CRLF & @CRLF & '请尝试插/拔 USB 闪存驱动器, 并查看托盘提示.', 5, 5, 360, 70)
GUISetState()
GUIRegisterMsg($WM_DEVICECHANGE, "WM_DEVICECHANGE")
Do
Until GUIGetMsg() = -3
Func WM_DEVICECHANGE($hWnd, $Msg, $wParam, $lParam)
$k += 1
WinSetTitle($Gui, '', '消息发生次数: ' & $k)
If ($wParam = $DBT_DEVICEARRIVAL) Or ($wParam = $DBT_DEVICEREMOVECOMPLETE) Then
Local $DEV_BROADCAST_VOLUME = DllStructCreate("int dbcvsize;int dbcvdevicetype;int dbcvreserved;int dbcvunitmask;" & _
"ushort dbcvflags", $lParam)
Local $iDriveType = DllStructGetData($DEV_BROADCAST_VOLUME, "dbcvdevicetype")
Else
Return $GUI_RUNDEFMSG
EndIf
; 如果该设备不是一个逻辑驱动器, 消息函数返回 $GUI_RUNDEFMSG
If $iDriveType <> $DBT_DEVTYP_VOLUME Then Return $GUI_RUNDEFMSG
Local $iMask = DllStructGetData($DEV_BROADCAST_VOLUME, "dbcvunitmask")
$iMask = Log($iMask) / Log(2)
Local $iDrive = Chr(65 + $iMask) & ":"
Switch $wParam
Case $DBT_DEVICEARRIVAL ; 检测设备
TrayTip("WM_DEVICECHANGE", "设备被连接", 5, 1)
Case $DBT_DEVICEREMOVECOMPLETE ; 关闭设备
TrayTip("WM_DEVICECHANGE", "设备被禁用", 5, 2)
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc
|