插入窗体到别的程序里
Form1代码
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses Unit2; procedure TForm1.FormCreate(Sender: TObject); var h: HWND; s : string; begin h := 0; // s := 'USB cable serial number manager 1.00'; // s := 'ir-750 Interface Program Ver1.02'; s := 'PDF-XChange viewer'; h := FindWindow(nil,PChar(s)); if h <> 0 then begin Form2 := TForm2.CreateParented(h);//关键 Form2.Show; end else begin ShowMessage('未找到宿主程序,程序无法继续进行。'); Application.Terminate; end; end; end.
Form2代码
unit Unit2; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm2 = class(TForm) Label1: TLabel; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation {$R *.dfm} procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction); begin Application.Terminate; end; procedure TForm2.FormCreate(Sender: TObject); begin end; end.