본문 바로가기

카테고리 없음

[윈도우즈 API] 현재 실행된 모든 프로그램의 Caption 읽어오기

반응형
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

function GetTitle(WinHandle: HWND): String;
var
  TitleLn : integer;
begin
  Result := '';
  TitleLn := GetWindowTextLength(WinHandle);
  if TitleLn > 0 then
  begin
    // GetWindowText()가 문자열의 맨 끝에 null 문자를 추가하므로 1증가 시킨다
    inc(TitleLn);
    SetLength(Result, TitleLn);
    // 명시한 윈도우 핸들의 title bar를 읽어온다
    GetWindowText(WinHandle, PChar(Result), TitleLn);
  end;
end;

// EnumWindows 를 위한 Callback function
function enumcall(awin, lparam: longint): Boolean; stdcall;
var
  buffer: String;
begin
  buffer := GetTitle(awin);
  if buffer<>'' then
    TListBox(lparam).Items.Add(buffer);
  Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  EnumWindows(@enumcall, Integer(ListBox1));
end;

end.
반응형