给QQ窗口发消息
unit FrmUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CheckLst, ExtCtrls, ComCtrls;
type
TSendFrm = class(TForm)
SendBtn: TButton;
Label1: TLabel;
Memo1: TMemo;
Label2: TLabel;
Memo2: TMemo;
Label3: TLabel;
Edit1: TEdit;
Label4: TLabel;
Edit2: TEdit;
procedure SendBtnClick(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
end;
var
SendFrm: TSendFrm;
NeedLoop: Bool = FALSE;
implementation
{$R *.dfm}
procedure LoopStop(TimeOut: DWORD);
var
StartTick: DWORD;
begin
StartTick := GetTickCount();
repeat
Application.ProcessMessages;
Sleep(1);
until (GetTickCount() - StartTick > TimeOut) or (not NeedLoop);
end;
procedure SendText(hWnd: HWND;
Text: string);
var
hDialog, hButton, hAfxWnd42, hRICHEDIT: DWORD;
J: Integer;
begin
hDialog := FindWindowEx(hWnd, 0, '#32770', nil);
if (hDialog = 0) then
Exit;
hButton := FindWindowEx(hDialog, 0, 'Button', '发送(&S)');
if (hButton = 0) then
Exit;
hAfxWnd42 := 0;
repeat
hAfxWnd42 := FindWindowEx(hDialog, hAfxWnd42, 'AfxWnd42', nil);
if (hAfxWnd42 = 0) then
Exit;
hRICHEDIT := FindWindowEx(hAfxWnd42, 0, 'RICHEDIT', nil);
until (hRICHEDIT <> 0);
J := 1;
while (J <= StrToInt(SendFrm.Edit1.Text)) and (NeedLoop)do
begin
SendMessage(hRICHEDIT, EM_REPLACESEL, 0, Integer(@Text[1]));
SendMessage(hButton, BM_CLICK, 0 , 0);
J := J + 1;
LoopStop(StrToInt(SendFrm.Edit2.Text) * 1000);
end;
end;
function EnumFunc(hWnd: HWND;
lParam: LPARAM): BOOL;
stdcall;
var
Buffer: array[0..50] of Char;
J: Integer;
begin
Result := NeedLoop;
Buffer[GetWindowText(hWnd, Buffer, 50)] := #0;
if (SendFrm.Memo2.Lines.Count = 0) then
begin
if (Pos('聊天中', Buffer) > 0) then
SendText(hWnd, SendFrm.Memo1.Text);
end else
begin
for J := 0 to SendFrm.Memo2.Lines.Countdo
if (Buffer = '与 ' + Trim(SendFrm.Memo2.Lines.Strings[J]) + ' 聊天中') then
SendText(hWnd, SendFrm.Memo1.Text);
end;
end;
procedure TSendFrm.SendBtnClick(Sender: TObject);
begin
if (SendBtn.Caption = '发送') then
begin
SendBtn.Caption := '停止';
NeedLoop := TRUE;
EnumWindows(@EnumFunc, 0);
SendBtn.Caption := '发送';
end else
begin
NeedLoop := FALSE;
end;
end;
procedure TSendFrm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
NeedLoop := FALSE;
end;
procedure TSendFrm.FormCreate(Sender: TObject);
begin
SetWindowLong(Edit1.Handle, GWL_STYLE, GetWindowLong(Edit1.Handle, GWL_STYLE) or ES_NUMBER);
SetWindowLong(Edit2.Handle, GWL_STYLE, GetWindowLong(Edit2.Handle, GWL_STYLE) or ES_NUMBER);
end;
end.