English 中文(简体)
Where should I begin when building a component?
原标题:

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!

问题回答

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.

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.

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.

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.

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.





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签