Delphi getwindowtext未检索文本

Delphi getwindowtext未检索文本,delphi,api,delphi-7,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); e

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

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

  title : pansichar;
为此:

  title: array[0..255] of Char;
试试这个代码

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

用这个:

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;
是不是,你有

程序TForm1.按钮1单击(发送方:TObject); 变量 liHwnd,liLength:整数; lpChar:PChar; 开始 liHwnd:=GetForegroundWindow(); liLength:=GetWindowTextLength(liHwnd)+1; lpChar:=StrAlloc(liLength); 尝试 GetWindowText(liHwnd、lpChar、liLength); showmessage(lpChar); 最后 StrDispose(lpChar); 结束; 结束;
你的密码对我说“Form1”。这是当前活动窗口的标题(=文本)。该“标题”指针不应该指向正在进入的某个对象吗?它给我访问权限错误,如果我初始化标题,它只给初始化值始终指定delphi的版本-这通常是至关重要的建议:如果您的一些问题已得到回答,您应该将答案标记为“已接受”。现在我收到了以下错误:[error]Unit1.pas(31):不兼容类型:“Array”和“PAnsiChar”顺便说一句,我使用的是delphi 7,可能不支持widechars是的,删除“wide”使其工作,但为什么widechar不工作?如果要告诉函数数组中有
GetWindowTextLength+1
字符,然后您应该使用动态分配来确保数组中确实有那么多字符,而不是使用包含255个元素的数组,这是完全任意的。避免GetWindowTextLength出现问题。;)我对其进行了编辑,使其也适用于Delphi的Unicode版本。 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;