2

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

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

5 回答 5

11

使用这个:

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;
于 2009-09-16T17:08:38.440 回答
3

替换这一行:

  title : pansichar;

有了这个:

  title: array[0..255] of Char;
于 2009-09-16T16:16:19.387 回答
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;

再见。

于 2009-09-16T16:16:28.010 回答
1
过程 TForm1.Button1Click(Sender: TObject);
变量
  liHwnd,liLength:整数;
  lpChar:PChar;
开始
  liHwnd := GetForegroundWindow();
  liLength := GetWindowTextLength(liHwnd) + 1;
  lpChar := StrAlloc(liLength);
  尝试
    GetWindowText(liHwnd, lpChar, liLength);

    显示消息(lpChar);
  最后
    StrDispose(lpChar);
  结尾;
结尾;
于 2009-09-28T14:37:14.283 回答
0

可以吗,你有这个问题吗?

于 2009-09-17T06:38:31.063 回答