views:

209

answers:

6

Hello, I am looking to build my own component and have no idea where to begin. I have some Delphi books but they are old and outdated, and am looking for some recommendations on tutorials/books to help me do this. The component will be pretty simple, basically 2 labels and an image. I need hundreds of these in an array, so I thought a component would be the best route. The text will adjust based on width etc, and have some mouseover events. So basically, where do I begin?

I am using Delphi 2009, this will be a win32 app.

Thanks!

+1  A: 

Take a look at this article, which describes how to build new components made up of a group of existing components.

Mason Wheeler
Didn't follow the link, but does it describe what my answer does? :-)
Ken White
+5  A: 

As far as I know, Delphi Component Design, by Danny Thorpe, is still the best book on the subject. Component design hasn't changed significantly in the last 15 years, so whatever books you have probably aren't as outdated as you think. There are three things to keep in mind while reading older references:

  1. Names of certain units have changed. There's no DsgnIDE anymore, for example. It's DesignIDE instead.

  2. Design-time code is strictly separated from run-time code now. This means you can't use DesignIDE in your component's unit, or else you're barred from using run-time packages. Older Delphi versions didn't have this technical restriction (although it's always been a legal restriction), so old code examples you find might need to change a little bit.

  3. Strings are Unicode now, so as with all old code examples you find, there might be some invalid assumptions about character sizes that you'll need to recognize.

The biggest obstacle to writing components is that you're expected to use various protected members of the classes you descend from, but those frequently aren't documented, so you'll have to be much more willing to go read the VCL source code for examples of how various methods are used.

Rob Kennedy
@Rob: Good recommendation. Ray Konopka's book "Developing Custom Delphi Components" was also good; I don't have a URL, and can't check Amazon from here (proxy issue). IIRC, someone mentioned he was considering re-releasing it via PDF or Lulu...
Ken White
@Rob: If you know where I can buy this book I would be very grateful. I tried @ Amazon, but no luck. So anyone?
Mihaela
Mihaela, the link in my answer goes to Amazon, and it says there are seven used copies. If you can't get it there, try other used-book vendors.
Rob Kennedy
+3  A: 

The easiest way to do what you want is to create a new form. Drop the labels and image and arrange them the way you want; if it suits your need, put them on a panel so they can be moved around as a unit.

Select all the components you want included (and including the panel if you chose to use one), and then click the Component item on the IDE's main menu, and select the "Create Component Template". (It's only enabled if you have selected components on the current form.) A dialog will appear asking you for a name for the new component, and the Component Palette page on which you want it to appear.

Ken White
+1 for a much easier solution, and one I forgot about.
skamradt
+7  A: 

You can order Ray Konopka's book Dev. Custom Delphi 3 Components - PDF for 25$. It's a specialized book on the subject and very good for a beginner too.

The main principles behind developing components is:

  1. Whether the component is visual or not (Does it need a Canvas to paint on)

  2. Does it need a window handle or not (visual or non-visual)

Once you answered those questions you can look at Delphi's source code for examples.

Mihaela
+1 for this - it's a great book.
robsoft
+1 best book I've found on subject. Don't let the Delphi 3 in title scare you, it is still very relavant to any version of Delphi. Hopefully someday Ray will get the time to update it.
Steve Black
+1  A: 

Don't worry about your books being old.

Just about everything from the old days still works fine and what little doesn't is generally due to name conflicts or the addition of Unicode in the 2009 version.

They aren't Microsoft, they don't go breaking old code without good reason. In fact, take some code from the old DOS days--assuming it doesn't try to manipulate the screen it's likely to run with minimal fixup.

Loren Pechtel
A: 

Don't worry about your old books! Since v3, Delphi hasn't changed much. This is why most of the programs compiled with D3 still compiles in D7 or even newer versions. And if it doesn't compile, probably you need to change a line or two, here and then.

I would recommend you to search other VERY simple components on Internet and see how they are made. Then make your own and post it here. Let other take a look at it and suggest improvements or spot bugs.

About your control's design:

1) maybe you DON'T need those two labels. You can just paint the text directly on the image. If you have lots of those components as you say, you may save a little bit of memory.

2) you may NOT want to have lots and lots and lots of images loading in one form. The overhead may be significant. What you can do is to load the pictures ONLY in images that are visible on screen - and you will put on screen ONLY 5-10 images (or whatever number of images you can show on the form without going out of screen). As the user scrolls down, you keep the same same TImage controls on screen but you load new (next) images in them.

3) You may not want to store labels and TImage in an array (I suppose it is an TImage because it seems you want to show them on the screen else you won't need labels - you need to explain your problem in more details if I got it wrong). But you can store a TBitmap and the text (that you want to display in labels) instead.

So, you may need to calculate how much CPU/disk overhead your hundreds of controls will create and how much memory they need. If you stay well under 1GB and the loading time is under 10 seconds, then it is relatively ok. IF not, you may want to think about your control's design before starting to actually implement it.

Hope this was helpful. See ya.

Altar