我尝试了以下代码但它没有从前台窗口检索文本!
procedure TForm1.Button1Click(Sender: TObject);
var
title : pansichar;
s : string;
begin
GetWindowText(GetForegroundWindow(), title,GetWindowTextLength(GetForegroundWindow()) + 1);
s := title;
showmessage(s);
end;
答案 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吗?