Question

J'ai essayé le code suivant mais il ne récupère pas le texte de la fenêtre au premier plan!

procedure TForm1.Button1Click(Sender: TObject);
 var
  title : pansichar;
  s : string;
begin
    GetWindowText(GetForegroundWindow(), title,GetWindowTextLength(GetForegroundWindow()) + 1);
    s := title;
    showmessage(s);
end;
Était-ce utile?

La solution

Utilisez celui-ci:

var
  hwndForeground: HWND;
  titleLength: Integer;
  title: string;
begin
  hwndForeground := GetForegroundWindow();
  titleLength := GetWindowTextLength(hwndForeground);
  SetLength(title, titleLength);
  GetWindowText(hwndForeground, PChar(title), titleLength + 1);
  title := PChar(title);

  ShowMessage(title);
end;

Autres conseils

Remplacez cette ligne:

  title : pansichar;

avec ceci:

  title: array[0..255] of Char;

Essayez ce code

procedure TForm1.Button1Click(Sender: TObject);
 var
  title : array[0..254] of Char;
  s : string;
begin
    GetWindowText(GetForegroundWindow(), title,255);
    s := title;
    showmessage(s);
end;

Au revoir.

procedure TForm1.Button1Click(Sender: TObject);
var
  liHwnd, liLength : Integer;
  lpChar : PChar;
begin
  liHwnd := GetForegroundWindow();
  liLength := GetWindowTextLength(liHwnd) + 1;
  lpChar := StrAlloc(liLength);
  Try
    GetWindowText(liHwnd, lpChar, liLength);

    showmessage(lpChar);
  Finally
    StrDispose(lpChar);
  End;
end;

Peut-être avez-vous ce numéro ?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top