Probleme de Handles

Mxbug Messages postés 47 Date d'inscription mercredi 27 avril 2005 Statut Membre Dernière intervention 9 janvier 2006 - 27 nov. 2005 à 01:06
Mxbug Messages postés 47 Date d'inscription mercredi 27 avril 2005 Statut Membre Dernière intervention 9 janvier 2006 - 2 déc. 2005 à 22:22
Yop
voila j'essaye de faire une ptite application d'auto message sur msn ( msn plus avec tout ses sons fait *** ) .
le code est le suivant avec quelques commentaires vite fait :
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.


le probleme vient du code en rouge , avec mon code le programme est supposé a chaque ouverture de fenetre voir si c une fenetre msn , puis envoyer le message si s'en est une , puis stocker la valeur de son handle dans un tableau et puis reparcourir el tableau si il trouve le handle de la fenetre il la laisse tranquille et ne la met pas au premier plan , sinon il la met au premier plan et envoi le message et puis stock encore son handle dans le tableau .
le probleme ici c'est qu'elle devient toujours au premier plan meme si le handle a été inscrit sur le tableau :?.
voila merci esperant avoir été clair .

22 réponses

Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
28 nov. 2005 à 23:24
Clique ICI ca pourrait peut être t'interesser
0
Mxbug Messages postés 47 Date d'inscription mercredi 27 avril 2005 Statut Membre Dernière intervention 9 janvier 2006
2 déc. 2005 à 22:22
yop
effectivement ;) , merci a toi
0
Rejoignez-nous