Let your program support plugin

xiaoxiao2021-03-06  107

Source code http://www.aidelphi.com/6to23/docu/plugin.zip

I believe that you should have used Winamp, and it is also believed that it supports the plugin is the main reason why it is ultimately popular. Can you make our own procedures also support plugins, with Delphi to use Delphi to make our first support plug-in program.

For general users, the plug-in is a DLL file, but different from the general DLL is that the plugin supports the extension of the main program function, and the main program can be operated, but most of the DLL is indispensable. Some. When you need to update your application version, the plugin may be your good choice.

First, before writing the application, you need to know what main features you can do. What kind of feature is needed, because the plugin itself interacts with the main program by the specific interface provided by the main program, when writing the main program You need to determine these interfaces. In this example, two interfaces are defined, and the initialization of the plug-in is mainly referring to the selection and load corresponding pictures, the other is to draw the corresponding pictures when the master Form is redrawn, where the second interface needs to be transmitted. Canvas handle and draw area.

The following is the definition of the main formal form interface segment:

TPLugindescribe = Function: Boolean; stdcall;

TPLugindrawDescribe = Procedure (DC: HDC; Rect: TRECT); stdcall;

TFORM1 = Class (TFORM)

Listbox1: tlistbox;

Procedure formcreate (Sender: TOBJECT);

Procedure ListBox1dblclick (Sender: TOBJECT);

Procedure FormPaint (Sender: TOBJECT);

Procedure formclose (Sender: Tobject; VAR Action: Tclosection);

Private

LinHandle: hmodule;

DWARPROC: TPLUGINDRAWDESCRIBE;

Procedure loadplugins;

Procedure loadingPlugin (SR: tsearchrec);

{Private Declarations}

public

{Public declarations}

END;

Among them, a process type and a function type are defined, where procedure type has two variables, an HDC type canvas handle, and the other is a TRECT type drawing area. TFORM1 has two private fields librandle and dwarproc to save the handle and drawing process handle of the loaded plugin DLL.

Check if there is a plug-in when the master form is created, list the available plugins, TFORM1's oncreate event handle FormCreate implements code as follows:

Procedure TFORM1.FormCreate (Sender: TOBJECT);

Begin

LibHandle: = 0; // Initialize the value of libHandle.

LoadPlugins; // Turn into the loadPlugins process.

END;

The following is the implementation code of LoadPlugins:

Procedure TFORM1.LOADPLUGINS;

VAR

SR: TSEARCHREC;

PATH: STRING;

Found: integer;

Begin

PATH: = ExtractFilePath (Application.exename);

Try

Found: = FindFirst (Path '* .dll', 0, SR);

While Found = 0 do // Find files.

BeginLoadPlugin (SR);

Found: = FindNext (SR);

END;

Finally

FindClose (SR);

END;

END;

Find all files under the current path to DLL, if found, check whether it is this case plugin.

Check the process of the plugin file to implement the code as follows:

Procedure TFORM1.LOADPLUGIN (SR: tsearchrec);

VAR

ILIBHANDLE: HMODULE;

PDESCRIBEPROC: TPLUGINDESCRIBE;

PDWARPROC: TPLUGINDRAWDESCRIBE;

Begin

ILIBHANDLE: = loadingLibrary (Pchar (sr.name));

IF iLibhandle <> 0 THEN

Begin

Try

PDESCRIBEPROC: = GetProcadDress (iLibHandle, 'loadingPicture);

PDWARPROC: = GetProcaddress (iLibhandle, 'DrawPicture);

IF assigned (pdescribeproc) and assigned (pdwarproc) THEN

ListBox1.Items.add (sr.name);

Finally

Freelibrary (ILIBHANDLE);

END;

END ELSE

ShowMessage ('Loading Dll File Error!');

END;

First dynamically load the plug-in library Using the LoadLibrary API function, if there is an interface process, add the file name to listbox1. Where GetProcAddress is the address of a routine in the DLL. Uninstall it with FreeELibrary without using a DLL.

In the Double-click event in ListBox1, you really start the plugin:

Procedure TFORM1.ListBox1dblclick (Sender: TOBJECT);

VAR PDESCRIBEPROC: TPLUGINDESCRIBE;

Begin

IF ListBox1.ItemIndex <> - 1 THEN

Begin

IF libhandle <> 0 THEN

Freelibrary (librandle);

DWARPROC: = NIL;

LibHandle: = loadingLibrary (pchar (listbox1.items [listbox1.itemindex));

IF libhandle <> 0 THEN

Begin

Try

PDESCRIBEPROC: = GetProcaddress (libhandle, 'loadingPicture');

IF pdescribeproc.

Begin

DWARPROC: = GetProcaddress (libhandle, 'DrawPicture');

INVALIDATE;

END;

Except

Freelibrary (librandle);

LibHandle: = 0;

END;

end

Else

ShowMessage ('Loading Dll File Error!');

END;

END;

// This code is mainly called the plug-in process that is loaded into the picture. If it is successful, the process address of the picture is taken. Only librandle <> 0 and the plugin is really started after being assigned to DWARPROC.

Tune the DWARPROC process in TForm1 onpaint events, start the drawing process in the plugin:

Procedure TFORM1.FORMPAINT (Sender: TOBJECT); Begin

Try

IF (LibHandle <> 0) And assigned (dwarproc) THEN

DWARPROC (Self.canvas.Handle, Self.ClientRect);

Except

IF libhandle <> 0 THEN

Begin

Freelibrary (librandle);

LibHandle: = 0;

END;

END;

END;

Finally, don't forget to uninstall the DLL in the TFORM1 on the onClose event.

After running, it is found that there is nothing in ListBox1, because the plug-in has not yet been established.

This example has two plugins, one completed pictures of tile drawing on the main form, and the other completes the zoom drawing.

The plug-in main unit is as follows:

Library Plugin1;

Uses

SYSUTILS,

Classes,

PluginUnit1 in 'PluginUnit1.Pas';

Exports LoadPicture, DrawPicture;

// Export two procedures.

Begin

End.

The definition of the two processes and implements code in unit pluginunit1:

Unit pluginUnit1;

Interface

Uses graphics, EXTDLGS, Windows

Function loadPicture: Boolean;

Procedure DrawPicture (DC: HDC; Rect: TRECT); export; stdcall;

VAR PICTURE: TPICTURE

IMPLEMENTATION

Procedure DrawPicture (DC: HDC; RECT: TRECT);

Var ccanvas: tcanvas;

Begin

IF (Picture <> NIL) and not pictures.graphic.empty the

Begin

CCANVAS: = Tcanvas.create;

Try

CCANVAS.HANDLE: = DC;

CCANVAS.STRETCHDRAW (Rect, Picture.graphic);

Finally

CCANVAS.FREE;

END;

END;

END;

Function loadPicture: Boolean;

Begin

Result: = FALSE;

Picture: = TPICTURE.CREATE;

With TopenPictureDialog.create (NIL) DO

Begin

Try

IF execute kil

Begin

Picture.loadfromfile (filename);

RESULT: = TRUE

END;

Finally

FREE;

END;

END;

END;

End.

The above plug-in completes the scaling drawing of the picture, the other plug-in code divided by the unit name and DrawPicture implementation, the rest is the same. Not repeated here. I believe that the reader can also complete itself. The author's homepage http://www.aidelphi.com has the most complete code on this routine.

转载请注明原文地址:https://www.9cbs.com/read-124729.html

New Post(0)