unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, MessengerAPIEvents, StdCtrls, WinSkinData;
type
TForm1 = class(TForm)
MessengerAPIDMessengerEvents1: TMessengerAPIDMessengerEvents;
Button1: TButton;
Edit1: TEdit;
Memo1: TMemo;
SkinData1: TSkinData;
procedure Button1Click(Sender: TObject);
procedure MessengerAPIDMessengerEvents1IMWindowCreated(Sender: TObject;
const pIMWindow: IDispatch);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1; // la form
Window: String; //
Handle: THandle; // le handle
Msg: Tmsg;
IMessage: String = ('je teste fais pas attention , merci ');
hwin:Hwnd ;
Wnd:Hwnd; //le handle de la fenetre msn
Tableau: array of Integer;
i : integer ;
implementation
{$R *.dfm}
Function TrimRight(Const S: String): String; // fonction pour avoir le titre
Var
I: Integer;
Begin
I := Length(S);
While (I > 0) And (S[I] <= ' ') Do Dec(I);
Result := Copy(S, 1, I);
end;
Function ActiveHandle: THandle; // fonction pour avoir le handle active
Begin
Result := GetForeGroundWindow;
End;
Function ActiveCaption: String; // fonction pour avoir le titre active
Var
Handle: THandle; // handle qu'on donnera a la conversation ouverte
Len: LongInt;
Title: String; // titre
Begin
hWin := FindWindowA(PChar('IMWindowClass'), Nil); // on trouve la conversation
Handle := hwin; // c ici qu'on donne le handle a la conversation
Len := GetWindowTextLength(Handle) + 1;
SetLength(Title, Len);
GetWindowText(Handle, pChar(Title), Len);
ActiveCaption := TrimRight(Title);
End;
Procedure SendKeys(Const text: String); // pour envoyer le message
Var
i: Integer;
shift: Boolean;
vk, scancode: Word;
ch: Char;
c, s: Byte;
Const
vk_keys: Array[0..9] Of Byte =
(VK_HOME, VK_END, VK_UP, VK_DOWN, VK_LEFT,
VK_RIGHT, VK_PRIOR, VK_NEXT, VK_INSERT,
VK_DELETE);
vk_shft: Array[0..2] Of Byte =
(VK_SHIFT, VK_CONTROL, VK_MENU);
flags: Array[false..true] Of Integer =
(KEYEVENTF_KEYUP, 0);
Begin
shift := false;
For i := 1 To Length(text) Do
Begin
ch := text[i];
If ch >= #250 Then
Begin
s := Ord(ch) - 250;
shift := Not Odd(s);
c := vk_shft[s Shr 1];
scancode := MapVirtualKey(c, 0);
Keybd_Event(c, scancode, flags[shift], 0);
End
Else
Begin
vk := 0;
If ch >= #240 Then
c := vk_keys[Ord(ch) - 240]
Else If ch >= #228 Then
c := Ord(ch) - 116
Else If ch < #32 Then
c := Ord(ch)
Else
Begin
vk := VkKeyScan(ch);
c := LoByte(vk);
End;
scancode := MapVirtualKey(c, 0);
If Not shift And (Hi(vk) > 0) Then
Keybd_Event(VK_SHIFT, $2A, 0, 0);
Keybd_Event(c, scancode, 0, 0);
Keybd_Event(c, scancode,
KEYEVENTF_KEYUP, 0);
If Not shift And (Hi(vk) > 0) Then
Keybd_Event(VK_SHIFT,
$2A, KEYEVENTF_KEYUP, 0);
End;
End;
End;
function TimerFunc (H: hwnd;uMsg : UINT;idEvent : UINT;dwTime : DWORD ): BOOL; stdcall; // pouyr finir l'envoi de message
begin
If (Handle <> ActiveHandle) Then
If (Window <> ActiveCaption) Then
Begin
Window := ActiveCaption;
If Window = ActiveCaption then
Begin
SendKeys(Pchar('Automessage : ' + IMessage + #13));
End;
End;
end;
procedure SwitchToThisWindow(h1: hWnd; x: bool); stdcall; // fonction pour mettre la fenetre en premier plan
external user32 Name 'SwitchToThisWindow';
procedure TForm1.FormCreate(Sender: TObject);
begin
setlength(tableau,10000000);
SetTimer(wnd,0,1000,@TimerFunc); // chaque 1 seconde on revient a la fonction qui envoi le message c a dire timerfunc
SetTimer(form1.handle,0,1000,@TForm1.MessengerAPIDMessengerEvents1IMWindowCreated); // chaque 1 seconde on revient a la fonction qui regarde si quelqun nous a parlé
end;
procedure TForm1.MessengerAPIDMessengerEvents1IMWindowCreated(Sender: TObject;
const pIMWindow: IDispatch); //la fonction qui regarde si quelqun nous a parlé
begin
wnd:=FindWindowA(PChar('IMWindowClass'), Nil); // on recherche encore la fenetre et on la donne a wnd
for i:=0 to tableau[10000000] do //on parcour le tableau
if i=integer(wnd) then
break // on arrette la boucle
else //sinon
SwitchToThisWindow(wnd, True); // on met la fenetre au premier plan ( a ce moment timerfunc va envoyer le message )
tableau[i+1]:=integer(wnd) // on stocke dans une cellule du tableau le handle
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
imessage:=edit1.text;
end;
end.
Partager