getwindowtext没有检索文本

Oma*_*bal 2 delphi api delphi-7

我尝试了以下代码,但它不从前台窗口检索文本!

procedure TForm1.Button1Click(Sender: TObject);
 var
  title : pansichar;
  s : string;
begin
    GetWindowText(GetForegroundWindow(), title,GetWindowTextLength(GetForegroundWindow()) + 1);
    s := title;
    showmessage(s);
end;
Run Code Online (Sandbox Code Playgroud)

act*_*ual 10

使用这个:

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;
Run Code Online (Sandbox Code Playgroud)