Mirror

Display the window of another application in a Delphi form (Views: 100)


Problem/Question/Abstract:

I would like to have an application (f.e. notepad.exe) to run in a specified frame (TPanel,..) within my application.

Answer:

It does not work well but you can try this:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ApplicationEvents1: TApplicationEvents;
    procedure FormCreate(Sender: TObject);
    procedure ApplicationEvents1Activate(Sender: TObject);
  private
    { Private declarations }
    FNotepad: HWND;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  wnd: HWND;
  tries: Integer;
begin
  WinExec('notepad.exe', SW_HIDE);
  tries := 0;
  repeat
    wnd := Findwindow('notepad', nil);
    if wnd = 0 then
    begin
      inc(tries);
      sleep(100);
    end;
  until
    (wnd <> 0) or (tries > 10);
  if wnd <> 0 then
  begin
    windows.setparent(wnd, handle);
    application.title := 'Notepad';
    MoveWindow(wnd, 0, 0, clientwidth, clientheight, false);
    ShowWindow(wnd, SW_SHOW);
    SetForegroundWindow(wnd);
    FNotepad := wnd;
  end
  else
    showmessage('Failed');
end;

procedure TForm1.ApplicationEvents1Activate(Sender: TObject);
begin
  if IsWindow(FNotepad) then
    SetForegroundWindow(FNotepad)
  else
    Close;
end;

end.

To wire the notepad window to a panel you would simlpy use the panels handle in the SetParent call.

<< Back to main page