Use the Windows encapsulated in the DLL file
Abstr:
When writing software, DLL files are often used. This article uses the Windows encapsulated in DLL files to describe how to encapsulate windows in DLL in Delphi and how to call windows encapsulated in DLL, and MDI-Child load and use in DLL
1. encapsulate the window in DLL
Open Delphi to create a DLL project, save it as usedll, and generate code
Library usedll; {Important note about DLL memory management: ShareMem must be First unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or Functions that pass strings as parameters or function results. This Applies to all strings passed to and from your DLL -- even those that Are nested in records and classes. ShareMem is the interface unit The BORLNDMM. DLL shared memory manager, which must be deployed along With your DLL. To avoid using BORLNDMM. DLL, pass string information Using PChar or parameter string parameters .} Uses SysUtils, Classes; {$ R *. res} Begin End. |
Create a new form and add a Label and Button. The settings are as follows:
Object Form1: TForm1 Left = 1, 192 Maximum = 133 Width = 334 Height = 221 Caption = 'dll '#20013 #20351 #29992 #31383 #20307 Color = clBtnFace Font. Charset = DEFAULT_CHARSET Font. Color = clWindowText Font. Height =-11 Font. Name = 'Ms Sans serif' Font. Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 Object Label1: TLabel Left = 1, 104 Top = 80 Width = 80 Height = 13 Caption = 'dll '#20013 #20351 #29992 #31383 #20307 End Object Button1: TButton Left = 1, 120 Maximum = 152 Width = 75 Height = 25 Caption = #30830 #23450 TabOrder = 0 OnClick = Button1Click End End |
Add a process: procedure LoadForm; export;
Procedure LoadForm;
Begin
Form1: = TForm1.Create (Application );
Try
Form1.ShowModal;
Finally
Form1.Free;
End;
End;
Complete code:
Library usedll; Uses SysUtils, Classes, Form_Unit in 'form _ Unit. pa' {Form1 }; {$ R *. res} Exports LoadForm index 1; Begin End. |
Unit Form_Unit; Interface Uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; Type TForm1 = class (TForm) Label1: TLabel; Button1: TButton; Procedure Button1Click (Sender: TObject ); Private {Private declarations} Public {Public declarations} End; Var Form1: TForm1; ApplicationName: String; Procedure LoadForm (Handle: THandle; AppName: Role string); export; Implementation {$ R *. dfm} Procedure LoadForm (Handle: THandle; AppName: Role string ); Begin Application. Handle: = Handle; ApplicationName: = AppName; Form1: = TForm1.Create (Application ); Try Form1.ShowModal; Finally Form1.Free; End; End; Procedure TForm1.Button1Click (Sender: TObject ); Begin Self. close; End; End. |
After compilation, generate the usedll. dll file. Now the DLL file is complete.
Ii. Call the Windows encapsulated in DLL
Create a project and add a Button. The layout of the form is as follows:
Object Form1: TForm1 Left = 1, 192 Maximum = 133 Width = 336 Height = 222 Caption = 'form1' Color = clBtnFace Font. Charset = DEFAULT_CHARSET Font. Color = clWindowText Font. Height =-11 Font. Name = 'Ms Sans serif' Font. Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 Object Button1: TButton Left = 1, 128 Top = 88 Width = 75 Height = 25 Caption = #25171 #24320 #31383 #20307 TabOrder = 0 OnClick = Button1Click End End |
The complete code is as follows:
Unit Use_Unit; Interface Uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; Type TForm1 = class (TForm) Button1: TButton; Procedure Button1Click (Sender: TObject ); Private {Private Declarations} Public {Public declarations} End; VaR Form1: tform1; Procedure loadform; External 'usedll. dll 'Index 1; Implementation {$ R *. DFM} Procedure tform1.button1click (Sender: tobject ); Begin Loadform; End; End. |
Iii. Load and use MDI-Child in DLL
If it is MDI-Child, how can I load and use it in the DLL? The following describes how to use the Windows encapsulated in the DLL file.
Create a DLL project, save it as mdidll, and create a new form. Set FormStyle to fsMDIChild, as shown below:
Object Form1: TForm1
Left = 1, 192
Maximum = 133
Width = 344
Height = 234
Caption = 'mdi'
Color = clbtnface
Font. charset = default_charset
Font. Color = clwindowtext
Font. Height =-11
Font. Name = 'Ms sans serif'
Font. Style = []
Formstyle = fsmdichild
Oldcreateorder = false
Position = podefault
Visible = true
Onclose = formclose
Pixelsperinch = 96
Textheight = 13
End
The Code is as follows:
Unit mdi_unit; Interface Uses Windows, messages, sysutils, variants, classes, graphics, controls, forms, Dialogs; Type Tform1 = Class (tform) Procedure formclose (Sender: tobject; var action: tcloseaction ); Private {Private Declarations} Public {Public declarations} MyParentForm: TForm; MyParentApplication: TApplication; End; Var DllApplication: TApplication; Implementation {$ R *. dfm} Procedure TForm1.FormClose (Sender: TObject; var Action: TCloseAction ); Begin Action: = caFree; End; End. |
Library mdidll; Uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Mdi_Unit in 'mdi _ Unit. pa' {Form1 }; Procedure LoadChild (ParentApplication: TApplication; ParentForm: TForm); export; stdcall; Var Form1: TForm1; DllProc: Pointer; Begin Application: = ParentApplication; Form1: = TForm1.Create (ParentForm ); Form1.MyParentForm: = ParentForm; Form1.MyParentApplication: = ParentApplication; Form1.Show; End; Procedure DLLUnloadProc (Reason: Integer); register; Begin If Reason = DLL_PROCESS_DETACH then Application: = DllApplication; End; {$ R *. res} Exports LoadChild; Begin DllApplication: = Application; DLLProc: = @ DLLUnloadProc; End. |
After compilation, the mdidll. dll file is generated.
Use the MDI-Child window in the DLL as follows:
Create a project. Set formstyle to fsmdiform in the main window as follows:
Object form1: tform1
Left = 1, 192
Maximum = 133
Width = 544
Height = 375
Caption = 'form1'
Color = clBtnFace
Font. Charset = DEFAULT_CHARSET
Font. Color = clWindowText
Font. Height =-11
Font. Name = 'Ms Sans serif'
Font. Style = []
FormStyle = fsMDIForm
Menu = MainMenu1
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
Object MainMenu1: TMainMenu
Left = 72
Maximum = 136
Object N1: TMenuItem
Caption = #26032 #24314 '(& N )'
OnClick = N1Click
End
End
End
Code:
Unit usemdi_Unit; Interface Uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus; Type TForm1 = class (TForm) MainMenu1: TMainMenu; N1: TMenuItem; Procedure N1Click (Sender: TObject ); Private {Private declarations} Public {Public declarations} End; T_ProvaChild = procedure (ParentApplication: TApplication; ParentForm: TForm); stdcall; Var Form1: TForm1; Implementation {$ R *. dfm} Procedure TForm1.N1Click (Sender: TObject ); Var DllHandle: THandle; ProcAddr: FarProc; ProvaChild: T_ProvaChild; Begin DllHandle: = LoadLibrary ('mdidll '); ProcAddr: = GetProcAddress (DllHandle, 'loadchild '); If ProcAddr <> nil then Begin ProvaChild: = ProcAddr; ProvaChild (Application, Self ); End; End; End. |
Conclusion:
Here you should use Delphi to call the Windows encapsulated in the DLL file, if you still do not understand, please contact me (Home Page: http://yousoft.home.chinaren.com, mailbox: yousoft@chinaren.com)