怎样在delphi中监视文本文件的变化? 一个程序在写一文本文件,(就像用记事本打开一文件,在文件中输入(也可能是自动产生,不是用键盘输入)内容,在不保存的情况下要用一程序读出来)。相当于文件中有改动或增加,就要把改动的内容或增加的内容读出来供另外的程序用。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.aspdao.com/article/336371.html type THead = Packed Record iCount: Cardinal; end; TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Edit1: TEdit; Memo1: TMemo; Button4: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; EventArr:array[0..1000]of EVENTMSG; EventLog:Integer; PlayLog:Integer; hHook,hPlay:Integer; recOK:Integer; canPlay:Integer; bDelay:Bool; FRecordHnd: Integer=-1;const RecordFile = 'C:\Record.dat';implementationuses Math;{$R *.dfm}Function PlayProc (iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;begin canPlay:=1; Result:=0; if iCode < 0 then //必须将消息传递到消息链的下一个接受单元 Result := CallNextHookEx(hPlay,iCode,wParam,lParam) else if iCode = HC_SYSMODALON then canPlay:=0 else if iCode = HC_SYSMODALOFF then canPlay:=1 else if ((canPlay =1 )and(iCode=HC_GETNEXT)) then begin if bDelay then begin bDelay:=False; Result:=50; end; pEventMSG(lParam)^:=EventArr[PlayLog]; end else if ((canPlay = 1)and(iCode = HC_SKIP))then begin bDelay := True; PlayLog:=PlayLog+1; end; if PlayLog>=EventLog then begin UNHookWindowsHookEx(hPlay); end;end;function HookProc (iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;var AHead: THead; AEvent: EVENTMSG;begin recOK:=1; Result:=0; if iCode < 0 then Result := CallNextHookEx(hHook,iCode,wParam,lParam) else if iCode = HC_SYSMODALON then recOK:=0 else if iCode = HC_SYSMODALOFF then recOK:=1 else if ((recOK>0) and (iCode = HC_ACTION)) then begin EventArr[EventLog]:=pEventMSG(lParam)^; EventLog:=EventLog+1; FillChar(AHead, SIZEOF(AHead), #0); FillChar(AEvent, Sizeof(AHead), #0); AEvent := EventArr[EventLog-1]; FileSeek(FRecordHnd, 0, 0); FileRead(FRecordHnd, AHead, Sizeof(AHead)); AHead.iCount := AHead.iCount + 1; FileWrite(FRecordHnd, AHead, Sizeof(AHead)); FileSeek(FRecordHnd, Sizeof(AHead)+100+(EventLog-1)*SizeOf(AEvent), 0); FileWrite(FRecordHnd, AEvent, Sizeof(AEvent)); if EventLog>=1000 then UnHookWindowsHookEx(hHook); end;end;procedure TForm1.FormCreate(Sender: TObject);begin Button1.Caption:='纪录'; Button2.Caption:='停止'; Button3.Caption:='回放'; Button4.Caption:='范例'; Button2.Enabled:=False;// Button3.Enabled:=False;end;procedure TForm1.Button1Click(Sender: TObject);begin If Not FileExists(RecordFile) then FRecordHnd := FileCreate(RecordFile) else FRecordHnd := FileOpen(RecordFile, fmOpenReadWrite) ; EventLog:=0; //建立键盘鼠标操作消息纪录链 hHook:=SetwindowsHookEx (WH_JOURNALRECORD,HookProc,HInstance,0); Button2.Enabled:=True; Button1.Enabled:=False;end;procedure TForm1.Button2Click(Sender: TObject);begin UnHookWindowsHookEx(hHook); hHook:=0; Button1.Enabled:=True; Button2.Enabled:=False; Button3.Enabled:=True; FileClose(FRecordHnd);end;procedure TForm1.Button3Click(Sender: TObject);var I: Integer; AHead: THead; AEvent: EVENTMSG;begin FillChar(AHead, SIZEOF(AHead), #0); if FRecordHnd < 0 then FRecordHnd := FileOpen(RecordFile, fmOpenReadWrite); FileSeek(FRecordHnd, 0, 0); FileRead(FRecordHnd, AHead, SizeOf(AHead)); for I := 0 to AHead.iCount-1 do begin FillChar(AEvent, Sizeof(AHead), #0); FileSeek(FRecordHnd, Sizeof(AHead)+100+I*SizeOf(AEvent), 0); FileRead(FRecordHnd, AEvent, SizeOf(AEvent)); EventArr[I] := AEvent; end; EventLog := AHead.iCount; PlayLog:=0; //建立键盘鼠标操作消息纪录回放链 hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK, PlayProc, HInstance,0); Button3.Enabled:=False;end; 简单一些的,对ReadFile\Openfile\writefile用API hook,或者过一段时间检查一下文件内容,复杂一些的,用文件驱动过滤 简单一些的,对ReadFile\Openfile\writefile用API hook,或者过一段时间检查一下文件内容,复杂一些的,用文件驱动过滤 delphi 的samples 不是 用 TShellChangeNotifier控件 吗 object ShellChangeNotifier1: TShellChangeNotifier NotifyFilters = [nfFileNameChange, nfDirNameChange, nfAttributeChange, nfSizeChange, nfWriteChange, nfSecurityChange] Root = 'C:\' WatchSubTree = True Left = 224 Top = 216 end 如果 你要 监控内容可以 hookapi 其实有几种方法。1、使用文件过滤驱动。2、是采用WINDOWS的通知。object ShellChangeNotifier1: TShellChangeNotifier NotifyFilters = [nfFileNameChange, nfDirNameChange, nfAttributeChange, nfSizeChange, nfWriteChange, nfSecurityChange] Root = 'C:\' WatchSubTree = True Left = 224 Top = 216 end3、采用线程监视然后做文本比对。本方法要求你对文本比对算法比较了解。 报表的表头里打印多记录的表. 保留小数点两位,在线急等.请路过高手指点!!!!! 这个(dxdbtrel.dcu)是什么控件用??(急) 一个程序,要把一些初始值(如:上次关闭窗口的位置。。。。)的消息的话,一般是怎么办? 哪里有金蝶的财务软件下载(^O^)? 关于窗体显示的问题,请指教 这几天有一点困惑,关于程序员 的未来的,特来和大家探讨。 救命啊,我想恢复我的硬盘(因为误格式化),怎么用DEBUG恢复? 如何把文件藏到BMP中去?高手请进->>> 大家帮帮我吧...我急死了......(我要被炒了) 如何把一个bitmap的红色和蓝色对换? 如何控制usb设备删除和复位?
THead = Packed Record
iCount: Cardinal;
end; TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Memo1: TMemo;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
EventArr:array[0..1000]of EVENTMSG;
EventLog:Integer;
PlayLog:Integer;
hHook,hPlay:Integer;
recOK:Integer;
canPlay:Integer;
bDelay:Bool;
FRecordHnd: Integer=-1;const RecordFile = 'C:\Record.dat';implementationuses Math;{$R *.dfm}
Function PlayProc (iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
canPlay:=1;
Result:=0;
if iCode < 0 then //必须将消息传递到消息链的下一个接受单元
Result := CallNextHookEx(hPlay,iCode,wParam,lParam)
else
if iCode = HC_SYSMODALON then canPlay:=0
else
if iCode = HC_SYSMODALOFF then canPlay:=1
else
if ((canPlay =1 )and(iCode=HC_GETNEXT)) then
begin
if bDelay then
begin
bDelay:=False;
Result:=50;
end;
pEventMSG(lParam)^:=EventArr[PlayLog];
end
else
if ((canPlay = 1)and(iCode = HC_SKIP))then
begin
bDelay := True;
PlayLog:=PlayLog+1;
end; if PlayLog>=EventLog then
begin
UNHookWindowsHookEx(hPlay);
end;
end;function HookProc (iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
var
AHead: THead;
AEvent: EVENTMSG;
begin
recOK:=1;
Result:=0; if iCode < 0 then
Result := CallNextHookEx(hHook,iCode,wParam,lParam)
else if iCode = HC_SYSMODALON then recOK:=0
else if iCode = HC_SYSMODALOFF then recOK:=1
else if ((recOK>0) and (iCode = HC_ACTION)) then
begin
EventArr[EventLog]:=pEventMSG(lParam)^;
EventLog:=EventLog+1;
FillChar(AHead, SIZEOF(AHead), #0);
FillChar(AEvent, Sizeof(AHead), #0);
AEvent := EventArr[EventLog-1]; FileSeek(FRecordHnd, 0, 0);
FileRead(FRecordHnd, AHead, Sizeof(AHead));
AHead.iCount := AHead.iCount + 1;
FileWrite(FRecordHnd, AHead, Sizeof(AHead));
FileSeek(FRecordHnd, Sizeof(AHead)+100+(EventLog-1)*SizeOf(AEvent), 0);
FileWrite(FRecordHnd, AEvent, Sizeof(AEvent)); if EventLog>=1000 then UnHookWindowsHookEx(hHook);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption:='纪录';
Button2.Caption:='停止';
Button3.Caption:='回放';
Button4.Caption:='范例';
Button2.Enabled:=False;
// Button3.Enabled:=False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
If Not FileExists(RecordFile) then FRecordHnd := FileCreate(RecordFile)
else FRecordHnd := FileOpen(RecordFile, fmOpenReadWrite) ; EventLog:=0; //建立键盘鼠标操作消息纪录链
hHook:=SetwindowsHookEx (WH_JOURNALRECORD,HookProc,HInstance,0);
Button2.Enabled:=True;
Button1.Enabled:=False;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hHook);
hHook:=0;
Button1.Enabled:=True;
Button2.Enabled:=False;
Button3.Enabled:=True;
FileClose(FRecordHnd);
end;
procedure TForm1.Button3Click(Sender: TObject);
var
I: Integer;
AHead: THead;
AEvent: EVENTMSG;
begin
FillChar(AHead, SIZEOF(AHead), #0); if FRecordHnd < 0 then
FRecordHnd := FileOpen(RecordFile, fmOpenReadWrite); FileSeek(FRecordHnd, 0, 0);
FileRead(FRecordHnd, AHead, SizeOf(AHead)); for I := 0 to AHead.iCount-1 do
begin
FillChar(AEvent, Sizeof(AHead), #0);
FileSeek(FRecordHnd, Sizeof(AHead)+100+I*SizeOf(AEvent), 0);
FileRead(FRecordHnd, AEvent, SizeOf(AEvent));
EventArr[I] := AEvent;
end;
EventLog := AHead.iCount; PlayLog:=0; //建立键盘鼠标操作消息纪录回放链
hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK, PlayProc, HInstance,0);
Button3.Enabled:=False;
end;
复杂一些的,用文件驱动过滤
复杂一些的,用文件驱动过滤
控件 吗
object ShellChangeNotifier1: TShellChangeNotifier
NotifyFilters = [nfFileNameChange, nfDirNameChange, nfAttributeChange, nfSizeChange, nfWriteChange, nfSecurityChange]
Root = 'C:\'
WatchSubTree = True
Left = 224
Top = 216
end
可以
hookapi
object ShellChangeNotifier1: TShellChangeNotifier
NotifyFilters = [nfFileNameChange, nfDirNameChange, nfAttributeChange, nfSizeChange, nfWriteChange, nfSecurityChange]
Root = 'C:\'
WatchSubTree = True
Left = 224
Top = 216
end
3、采用线程监视然后做文本比对。本方法要求你对文本比对算法比较了解。