Delphi FindowWindow,FindowWindowEx

1433 ワード

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    btnFindWindow: TButton;
    btnFindWindowEx: TButton;
    procedure btnFindWindowClick(Sender: TObject);
    procedure btnFindWindowExClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnFindWindowClick(Sender: TObject);
var
    hNotepad : HWND;
begin
    //Find Window
    hNotepad := FindWindow('Notepad',nil);
    ShowMessage(format('Notepad HWND:$%x',[hNotepad]));
end;

procedure TForm1.btnFindWindowExClick(Sender: TObject);
var
    p : pchar;
    hNotepad,hEdit : HWND;
begin
    GetMem(p,255);
    hNotepad := FindWindow('Notepad',nil);  // ClassName, WindowName
    hEdit := FindWindowEx(hNotepad,0,'Edit',nil);
    //ShowMessageFmt('Edit handle: $%x',[hEdit]);
    //GetWindowText(hEdit, p, 255); //  Edit , 
    SendMessage(hEdit,WM_GETTEXT,255,Integer(p));
    ShowMessage(p);
    FreeMem(p);
end;

end.