示例 2:
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
* 监控某个目录下的文件或目录的改动、增删事件,消息+计时器
Public oForm
Clear
* oForm = Createobject("Tform", SYS(2023)) && 监控系统 Temp 目录
oForm = Createobject("Tform", "C:\Temp") && 监控 C:\Temp 目录
oForm.Visible = .T.
* 结束主程序
Define Class Tform As Form
#Define FILE_NOTIFY_CHANGE_FILE_NAME 1
#Define FILE_NOTIFY_CHANGE_DIR_NAME 2
#Define FILE_NOTIFY_CHANGE_ATTRIBUTES 4
#Define FILE_NOTIFY_CHANGE_SIZE 8
#Define FILE_NOTIFY_CHANGE_LAST_WRITE 16
#Define FILE_NOTIFY_CHANGE_LAST_ACCESS 32
#Define FILE_NOTIFY_CHANGE_CREATION 64
#Define FILE_NOTIFY_CHANGE_SECURITY 128
#Define INVALID_HANDLE_VALUE -1
#Define WAIT_OBJECT_0 0
#Define WATCHING_INTERVAL 1000
&& 间隔时间,单位:毫秒
Protected hNotify, PathBeingWatched
Width = 400
Height = 150
MaxButton = .F.
BorderStyle = 2
AutoCenter = .T.
Caption = "正在监控目录"
hNotify = INVALID_HANDLE_VALUE
PathBeingWatched = ""
Add Object lblTarget As Label With Left = 10, Top = 7, AutoSize = .T.
Add Object tm As Timer With Interval = 0
Add Object lblAlert As Label With Left = 10, Top = 30, ;
Autosize = .T., Caption = "通知 : "
Function Init(cPath)
This.Declare
This.PathBeingWatched = Fullpath(m.cPath)
If Not This.StartWatching()
= Messagebox("通知句柄错误,可能不存在该目录。")
Return .F.
Endif
Procedure Destroy
This.StopWatching
Clear Dlls
Protected Function StartWatching
Local lResult
* 监控的目录不存在
This.hNotify = FindFirstChangeNotification(;
THIS.PathBeingWatched, 0,;
FILE_NOTIFY_CHANGE_FILE_NAME +;
FILE_NOTIFY_CHANGE_LAST_WRITE )
lResult = (This.hNotify <> INVALID_HANDLE_VALUE)
If lResult
This.lblTarget.Caption = "监控目录 : " +;
THIS.PathBeingWatched
This.tm.Interval = WATCHING_INTERVAL
Endif
Return
lResult
Protected Procedure ContinueWatching
If FindNextChangeNotification(This.hNotify) = 0
This.StopWatching
= Messagebox("响应错误.")
This.Release
Endif
This.tm.Interval = WATCHING_INTERVAL
Protected Function StopWatching
This.tm.Interval = 0
If This.hNotify <> INVALID_HANDLE_VALUE
= FindCloseChangeNotification(This.hNotify)
Endif
Function _signaled
&& 返回信号状态
Return (WaitForSingleObject(This.hNotify, 0) = WAIT_OBJECT_0)
Procedure _notify
&& 触发通知事件
Local cMessage
cMessage = "通知 : " + Ttoc(Datetime()) + " 该目录的文件有增删或改动!"
This.lblAlert.Caption = cMessage
Activate Screen
? cMessage
? FILE_NOTIFY_CHANGE_FILE_NAME
* 这里可以写获取数据的程序......
This.ContinueWatching
Procedure tm.Timer
If Thisform._signaled()
Thisform._notify
Endif
Protected Procedure Declare
Declare SHORT FindNextChangeNotification In kernel32;
INTEGER hChangeHandle
Declare SHORT FindCloseChangeNotification In kernel32;
INTEGER hChangeHandle
Declare Integer FindFirstChangeNotification In kernel32;
STRING lpPathName, Integer bWatchSubtree,;
INTEGER dwNotifyFilter
Declare Integer WaitForSingleObject In kernel32;
INTEGER hHandle, Integer dwMilliseconds
Enddefine