Home arrow Delphi-Kylix arrow Page 4 - Creating an About Box Plug-in
DELPHI-KYLIX

Creating an About Box Plug-in


In the previous article we saw how to call a routine from a package as well as how to dynamically load a package into the main application. In this second part of a two-part article explaining plug-ins, we are going to look at how to create a real world plug-in.

Author Info:
By: Leidago
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
November 20, 2006
TABLE OF CONTENTS:
  1. · Creating an About Box Plug-in
  2. · Continuing with our example
  3. · Creating a package
  4. · Adding some more code

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Creating an About Box Plug-in - Adding some more code
(Page 4 of 4 )

In the host form, double click on the "About" menuitem and add the following code:

procedure TForm1.About1Click(Sender: TObject);
begin
packhandle := LoadPackage('Myplugin1.bpl');
 if packhandle <> 0 then
begin
AClass := GetClass('TForm2');
 if AClass <> nil then
 begin
with TComponentClass(AClass).Create(Application)
    as TCustomForm do
  begin
    Show;
    Free;
  end;
  end
  else
  begin
  showmessage('Could not load class');
  end;
end
  else
  begin
  showmessage('Package handle empty');
  end;
end;

Also add the following two variables to the forms "var" section:

  packhandle: HModule;
  AClass: TPersistentClass;

The code should be familiar to you by now. First we load the plug-in by calling the loadpackage() function, and then store the returned value in the "packhandle" variable:

packhandle := LoadPackage('Myplugin1.bpl');

Then we check to see whether the handle is empty. If it is not empty, we retrieve the class that is exposed by the package:

if packhandle <> 0 then
begin
AClass := GetClass('TForm2');
 if AClass <> nil then
 begin
with TComponentClass(AClass).Create(Application)
    as TCustomForm do
  begin
    Show;
  end;

The AClass variable holds the exposed class(TForm2) and is then instantiated and called "TCustomForm." This new object gives us access to all the usual methods of a normal form. One of these is "show," which as the name implies shows a form.

Then we simply use the "show" function to view the about box, and then free the TCustomForm object. I have not done this in this code, but it would be better to first check to see whether the package exists by using the fileexists() function (or any other method), and then based on that condition continue with the rest of the code.

That is how simple and easy it is to create a plug-in. Of course you could always improve the code by including some kind of interface class that will reduce the chance of errors. But this should give you enough information to start creating your own plug-ins.

Conclusion

Delphi packages are ideal for building plug-ins. They provide a great way to make your applications small in size and modular. They also make your applications more useful by letting other developers extend their capabilities.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials