我是第一次在此社区提问:在窗口关闭时将内容保存到指定文件,比如:Notepad 在关闭时,内容保存到指定文件如何实现。请各位老哥赐教

解决方案 »

  1.   

    你在程序里放一个MEMO。在程序关闭事件里写
    MEMO有一个保存内容到指定文本里。
    这个比较简单的保存
      

  2.   

    sorry,我没说清楚。我是要在一个钩子中进行此项功能。
      

  3.   

    附钩子如下:function CBTProc(nCode:integer; wParam: WParam; lParam: lparam): lresult;stdcall;export;
    var
      hWnd: Thandle;
      nLen: Integer;
      Buff: array[0..4095] of Char;
      filestream:TFilestream;
    begin
      if nCode<0 then
      begin
        Result := CallNextHookEx(hOldCbthook, nCode, wParam, lParam);
        Exit;
      end;  if nCode=HCBT_DESTROYWND then
      begin
        hWnd := Thandle(wparam);
        GetClassName(hWnd, @Buff, 128);
        if CompareStr(Buff,'Notepad')=0 then
        begin
          hWnd := FindWindowEx(hWnd, 0, @Buff,nil );
          nLen := GetWindowText(hWnd, @Buff, SizeOf(Buff));
           // 这儿保存Buff里的文字??此处如何做,下面的代码行不通
          filestream:=TFilestream.Create('E:\b.txt',fmcreate);
          try
            filestream.WriteBuffer(buff,nlen);
          finally
            filestream.Free;
          end;
        end;
      end;
      ncode:=-1;
      Result := CallNextHookEx(hOldCbthook, nCode, wParam, lParam);
    end;
      

  4.   

    写文件byte用 blockwrite 文本用 writevar i:Integer;
    PackFile:File;
    FileName,JmString:String;
    TPByte:array[1..2] of Byte;
    begin
    AssignFile(PackFile,saveDir+FileName);
    Rewrite(PackFile,1);
    tryfor i:=1 to 2 do begin
      TPByte[i]:=Byte(TP[i]);
      blockwrite(PackFile,TPByte[i],1);
    end;finally
    closeFile(PackFile);
    end;
    end;
      

  5.   

    是在钩子中实现,初学,望赐教附钩子如下:
    function  CBTProc(nCode:integer;  wParam:  WParam;  lParam:  lparam):  lresult;stdcall;export;  
    var  
       hWnd:  Thandle;  
       nLen:  Integer;  
       Buff:  array[0..4095]  of  Char;  
       filestream:TFilestream;  
    begin  
       if  nCode  <0  then  
       begin  
         .......  
       end;  
       if  nCode=HCBT_DESTROYWND  then  
       begin  
           hWnd  :=  Thandle(wparam);  
           GetClassName(hWnd,  @Buff,  128);  
           if  CompareStr(Buff,'Notepad')=0  then  
           begin  
               hWnd  :=  FindWindowEx(hWnd,  0,  @Buff,nil  );  
               nLen  :=  GetWindowText(hWnd,  @Buff,  SizeOf(Buff));  
                 //  这儿保存Buff里的文字??此处如何做?????? 
           end;  
       end;  
         
    end;
      

  6.   

    那位大哥大姐帮我看看吧!我照下面编写也行不通(在????处)
    function  CBTProc(nCode:integer;  wParam:  WParam;  lParam:  lparam):  lresult;stdcall;export;
    var
       hWnd:  Thandle;
       nLen:  Integer;
       Buff:  array[0..4095]  of  Char;
       PackFile:textFile;
       i:integer;
    begin
       if  nCode  <0  then
       begin
           Result  :=  CallNextHookEx(hOldCbthook,  nCode,  wParam,  lParam);
           Exit;
       end;   if  nCode=HCBT_DESTROYWND  then
       begin
           hWnd  :=  Thandle(wparam);
           GetClassName(hWnd,  @Buff,  128);
           if  CompareStr(Buff,'Notepad')=0  then
           begin
               hWnd  :=  FindWindowEx(hWnd,  0,  @Buff,nil  );
               nLen  :=  GetWindowText(hWnd,  @Buff,  SizeOf(Buff));
                 //  这儿保存Buff里的文字??此处如何做,下面的代码也行不通
               begin
               AssignFile(PackFile,'E:\b.txt');
               Rewrite(PackFile);
               try
               for  i:=0  to  nlen-1 do  begin
               write(PackFile,buff[i]);
               end;
               finally
               closeFile(PackFile);
               end;
             end;
           end;
       end;
       ncode:=-1;
       Result  :=  CallNextHookEx(hOldCbthook,  nCode,  wParam,  lParam);
    end;