|
楼主 |
发表于 2023-4-24 23:09:07
|
显示全部楼层
广东省梅州市
好的,以下是用易语言实现监控整个磁盘新增文件的代码:
```
'==============================
'本代码使用监控消息接口
'监听整个磁盘新增文件
'==============================
'定义常量
Const WM_DEVICECHANGE = 537
Const DBT_DEVICEARRIVAL = 32768
Const DBT_DEVICEQUERYREMOVE = 32769
Const DBT_DEVICEREMOVECOMPLETE = 32772
'定义需要监听的磁盘
Dim diskList As String = ""
'主程序
AutoEventLoop
Procedure AutoEventLoop()
'获取监听的磁盘列表
diskList = getDiskList()
'初始化消息监听
Dim hWnd = CreateWindow()
'循环监听消息
Repeat
Dim msg As MSG = PeekMessage(hWnd)
If msg.hWnd <> 0 Then
TranslateMessage(msg)
DispatchMessage(msg)
EndIf
Until msg.message = WM_QUIT
EndProc
'获取需要监听的磁盘列表
Function getDiskList() As String
Dim dl As String = ""
Dim i As Integer = 0
Do
Dim disk$
disk$ = Str(i) + ":\\"
If Dir(disk$) <> "" Then dl += disk$ + "|"
i++
Until Len(disk$) > 3
Return dl
EndFunc
'创建消息监听窗口
Function CreateWindow() As HWnd
'创建窗口类
Dim wndClass As WndClass
wndClass.cbSize = SizeOf(WndClass)
wndClass.style = CS_HREDRAW | CS_VREDRAW
wndClass.lpfnWndProc = EventProc
wndClass.hInstance = GetModuleHandle(0)
wndClass.lpszClassName = "Monitor"
'注册窗口类
RegisterClass(wndClass)
'创建窗口
Dim hWnd = CreateWindowEx(0, "Monitor", "Monitor", 0, 0, 0, 0, 0, HWND_MESSAGE, 0, GetModuleHandle(0), 0)
'启用消息监听
AddClipboardFormatListener(hWnd)
RegisterDeviceNotification(hWnd, DEV_BROADCAST_VOLUME, DEVICE_NOTIFY_WINDOW_HANDLE)
Return hWnd
EndFunc
'消息处理函数
Function EventProc(hWnd As HWnd, message As UINT, wParam As WPARAM, lParam As LPARAM) As LRESULT
Select message
'设备变化消息
Case WM_DEVICECHANGE
Select wParam
'设备已连接
Case DBT_DEVICEARRIVAL
Dim pDevBroadcastHdr = lParam
If pDevBroadcastHdr <> NULL Then
Dim dbcv = *pDevBroadcastHdr
'判断消息类型,并输出变化信息
If dbcv.dbch_devicetype = DBT_DEVTYP_VOLUME Then
Dim pDevBroadcastVolume = lParam
Dim szDrive As String = GetDiskFromMask(pDevBroadcastVolume.dbcv_unitmask)
If InStr(diskList, szDrive) > 0 Then Print(szDrive + " has a new file.")
End If
EndIf
'设备即将移除
Case DBT_DEVICEQUERYREMOVE
'不处理此消息
'设备已经被移除
Case DBT_DEVICEREMOVECOMPLETE
Dim pDevBroadcastHdr = lParam
If pDevBroadcastHdr <> NULL Then
Dim dbcv = *pDevBroadcastHdr
'判断消息类型,并输出变化信息
If dbcv.dbch_devicetype = DBT_DEVTYP_VOLUME Then
Dim pDevBroadcastVolume = lParam
Dim szDrive As String = GetDi |
|