getwindowtext没有检索文本

时间:2009-09-16 15:57:00

标签: 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;

5 个答案:

答案 0 :(得分:9)

使用这个:

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;

答案 1 :(得分:3)

替换此行:

  title : pansichar;

用这个:

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

答案 2 :(得分:2)

试试此代码

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

再见。

答案 3 :(得分:1)

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;

答案 4 :(得分:0)

可以,你有this issue吗?

相关问题