钩子编程问题(200分)

  • 主题发起人 主题发起人 stuwei
  • 开始时间 开始时间
S

stuwei

Unregistered / Unconfirmed
GUEST, unregistred user!
现在有一问题:<br>&nbsp;如何写程序1 读取程序2的窗口上的Memo的内容,在不修改程序2的前提下如何做?<br>
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2457722<br>参考一下
 
搞了好一会儿,终于搞定!<br>利用剪贴板来传递memo中的数据。<br>======================<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils,Clipbrd,//Clipbrd是剪贴板的定义单元。加入。<br>&nbsp; Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; PfindWindowStruct =^ TFindWindowStruct;<br>&nbsp; TFindWindowStruct =record<br>&nbsp; &nbsp; &nbsp;caption: string;<br>&nbsp; &nbsp; &nbsp;classname:string;<br>&nbsp; &nbsp; &nbsp;windowsHandle: Thandle;<br>&nbsp; end;<br><br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; btn1: TButton;<br>&nbsp; &nbsp; mmo1: TMemo;<br>&nbsp; &nbsp; procedure btn1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>implementation<br><br>{$R *.DFM}<br><br>//枚举所有的窗体的回调函数<br>function EnumwindowsProc(hWindow :hWnd;lParam:longInt):Bool ;<br>{$IFDEF WIN32} stdcall; {$ELSE};export; {$ENDIF}<br>VAR<br>&nbsp;lpBuffer:Pchar;<br>&nbsp;windowcaptionFound:bool;<br>&nbsp;classNameFound:bool;<br>begin<br>&nbsp; &nbsp;GetMem(lpbuffer,255);<br>&nbsp; &nbsp;result:=true;<br>&nbsp; &nbsp;WindowCaptionFound:=false;<br>&nbsp; &nbsp;ClassNameFound:=false;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; if GetWindowText(hWindow,lpBuffer,255)&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp;if pos (pFindwindowstruct(lparam).caption,strPas(lpBuffer))&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; windowcaptionfound :=true;<br>&nbsp; &nbsp; &nbsp; if PfindWindowstruct(lParam).classname ='' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Classnamefound:=true<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if GetClassName (hwindow,lpBuffer,255) &gt;0 then<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if pos (pFindwindowstruct(lparam).caption,strPas(lpBuffer))&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; classnamefound:=true;<br>&nbsp; &nbsp; &nbsp;if (windowcaptionfound and classnamefound) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PfindWindowstruct(lparam).windowsHandle:=hwindow;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp;freemem (lpbuffer,sizeof(lpbuffer^));<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>//寻找窗体<br>function FindAwindow(caption:string;ClassName:string):Thandle;<br>var<br>&nbsp; WindowInfo: TfindWindowstruct;<br>begin<br>&nbsp; &nbsp;windowInfo.caption:=caption;<br>&nbsp; &nbsp;windowinfo.classname :=classname;<br>&nbsp; &nbsp;windowinfo.windowsHandle :=0;<br>&nbsp; &nbsp;EnumWindows (@EnumWindowsProc,LongInt(@windowInfo));<br>&nbsp; &nbsp;findAwindow := windowInfo.windowsHandle ;<br>end;<br><br>function EnumChildProc (Hwnd:integer;lParam:longint):boolean;<br>{$IFDEF WIN32} STDCALL;{$else}; export;{$endif}<br>var<br>&nbsp; MemoData :array[1..5000] of char; //可以根据需要再增大<br>&nbsp; clsName:array[0..5] of char;<br>begin<br>&nbsp; &nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; &nbsp; fillchar(MemoData,5000,#0);<br>&nbsp; &nbsp; &nbsp; GetClassName(Hwnd,@clsName,6);<br>&nbsp; &nbsp; &nbsp;if strcomp(clsname,'TMemo')=0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;//将MEMO中的内容全选<br>&nbsp; &nbsp; &nbsp; &nbsp;SendMessage(Hwnd, EM_SETSEL, 0, -1);<br>&nbsp; &nbsp; &nbsp; &nbsp;//拷贝内容,拷贝到剪贴板<br>&nbsp; &nbsp; &nbsp; &nbsp;SendMessage(Hwnd, WM_COPY, 0, 0);<br>&nbsp; &nbsp; &nbsp; &nbsp;Clipboard.GetTextBuf(@MemoData,5000);//将简帖板的内容给MemoData<br>&nbsp; &nbsp; &nbsp; &nbsp;form1.Mmo1.Lines.add(MemoData); //显示结果。<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;Result:=true;<br>end;<br><br>procedure TForm1.btn1Click(Sender: TObject);<br>var<br>&nbsp; h :word;<br>&nbsp; cp:pchar;<br>begin<br>&nbsp; cp:='目标窗体的Caption';<br>&nbsp; h:=FindAWindow(cp,'');<br>&nbsp; enumchildwindows(h,@enumchildProc,0);<br>end;<br>end.<br><br>其他用途应该可以融会贯通。
 
楼主老兄,行不行啊?
 
谢谢各位
 
后退
顶部