質問

次のコードを試しましたが、フォアグラウンドウィンドウからテキストを取得しません!

procedure TForm1.Button1Click(Sender: TObject);
 var
  title : pansichar;
  s : string;
begin
    GetWindowText(GetForegroundWindow(), title,GetWindowTextLength(GetForegroundWindow()) + 1);
    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;

他のヒント

この行を置き換えます:

  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;

はい。

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;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top