Author: Tomas Rutkauskas
I want load a TFrame from a DLL in the main program. I always get the error "No
parent window", although I already have the main form's handle.
Answer:
1 library DLLFrame;
2
3 uses
4 SysUtils, Classes, Controls, Forms, Windows,
5 DllFrameFrame in 'DllFrameFrame.pas' {Frame1: TFrame};
6
7 procedure AddFrame(ApplicationHandle, ParentHandle: THandle); stdcall;
8 var
9 Frame1: TFrame1;
10 AppHandle: THandle;
11 begin
12 AppHandle := Application.Handle;
13 Application.Handle := ApplicationHandle;
14 Frame1 := TFrame1.Create(Application);
15 Frame1.ParentWindow := ParentHandle;
16 SetParent(Frame1.Handle, ParentHandle);
17 Frame1.Align := alClient;
18 Frame1.Visible := True;
19 Application.Handle := AppHandle;
20 end;
21
22 exports
23 AddFrame;
24
25 begin
26 end.
27
28 unit DllFrameFrame;
29
30 interface
31
32 uses
33 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
34 StdCtrls;
35
36 type
37 TFrame1 = class(TFrame)
38 Label1: TLabel;
39 Label2: TLabel;
40 private
41 { Private declarations }
42 public
43 { Public declarations }
44 end;
45
46 implementation
47
48 {$R *.dfm}
49
50 end.
51
52 program LoadDLLFrame;
53
54 uses
55 Forms, LoadDLLFrameMn in 'LoadDLLFrameMn.pas' {Form1};
56
57 {$R *.res}
58
59 begin
60 Application.Initialize;
61 Application.CreateForm(TForm1, Form1);
62 Application.Run;
63 end.
64
65 unit LoadDLLFrameMn;
66
67 interface
68
69 uses
70 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
71 Dialogs, ExtCtrls, StdCtrls;
72
73 type
74 TForm1 = class(TForm)
75 Panel1: TPanel;
76 Button1: TButton;
77 procedure Button1Click(Sender: TObject);
78 private
79 { Private declarations }
80 public
81 { Public declarations }
82 end;
83
84 var
85 Form1: TForm1;
86
87 procedure AddFrame(ApplicationHandle, ParentHandle: THandle); stdcall;
88
89 implementation
90
91 {$R *.dfm}
92
93 procedure AddFrame(ApplicationHandle, ParentHandle: THandle); stdcall;
94 external 'DLLFrame.dll';
95
96 procedure TForm1.Button1Click(Sender: TObject);
97 begin
98 AddFrame(Application.Handle, Panel1.Handle);
99 end;
100
101 end.
|