[ Pobierz całość w formacie PDF ]
.They are fully object-oriented, which means you can easily change or enhance existing components by creatingdescendant objects.They are small, fast, and light, and can be linked directly into your executables.Native BCB components are ordersof magnitude smaller than most ActiveX controls, and they link directly into your programs.In particular, a nativeBCB component is just a particular kind of object, so it links into your programs naturally, just as any other objectwould, without any handwaving.Few people would claim that VBXs aren't groundbreaking, that ActiveX controls aren't going to be very important, or thatOLE2 is not an enormously promising architecture.However, BCB components are relatively easy to create and come in alight, easy-to-use package.You can create BCB components that do nearly anything, from serial communications todatabase links to multimedia.These capabilities give BCB a big advantage over other tools that force you to use large,complex, and unwieldy component models.NOTE: Most publicly available components cost in the range of $50 to $150.Many of these tools encapsulatefunctionality that might cost tens of thousands of dollars to produce in-house.For example, a good communicationlibrary might take a year to build.However, if a company can sell the library in volume, it can afford to charge $100or $200 for the same product.That's a real bargain.And most of these tools are easy to use.Building components is agreat way for relatively small third-party companies to make money, and buying components is a great way to savetime on big projects.These ground-breaking tools are changing everything about the way programs are constructed.BCB components are flexible tools easily built by anyone who knows OOP and the BCB language.In this package, youhave explanations detailing all the prerequisite knowledge that component builders need, from a description of BCB itself,through a description of its language, and on to an overview of its implementation of OOP.From this foundation, you caneasily begin building your own components.Creating Descendants of an Existing ComponentIn this section, you will see how to create a series of custom TEdit, TPanel, and TLabel controls.The changes made tothe standard TEdit and TLabel components involve tweaking their colors, as well as their fonts' colors, names, sizes, andstyles.The goal is to show how to create a suite of custom controls that you can place on the Component Palette and use forspecial effects, or to define the look and feel of a certain set of applications belonging to a particular department orcompany.With projects like this, starting with one simple example is best, and then you can move on to more complex components.Listings 22.1 through 22.3 contain a sample component and a program that allows you to test the new component beforeyou place it on the Component Palette.The component is stored in a module called Unleash1.The Unleash1 sourcecode is the first version of a unit that will be expanded later in the chapter.Scan through it and check out its basic structure.Once it is clear to you how to create and test the component, I will briefly discuss how to place it on the Component Palette.Listing 22.1.The header file for a simple component descending from TEdit.///////////////////////////////////////// Unleash1.h// Simple example of creating a component// Copyright (c) 1997 by Charlie Calvertfile:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch22.htm (2 of 79) [10/10/2000 1:14:52 AM]Ch 22 -- Creating Descendants of Existing Components//#ifndef Unleash1H#define Unleash1H//--------------------------------------------------------------------------#include#include#include#include#include//--------------------------------------------------------------------------class TSmallEdit : public TEdit{private:protected:public:virtual __fastcall TSmallEdit(TComponent* Owner);__published:};//--------------------------------------------------------------------------#endifListing 22.2.The code for a simple component descending from TEdit.///////////////////////////////////////// Unleash1.cpp// Simple example of creating a component// Copyright (c) 1997 by Charlie Calvert//#include#pragma hdrstop#include "Unleash1.h"file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch22.htm (3 of 79) [10/10/2000 1:14:52 AM]Ch 22 -- Creating Descendants of Existing Components//--------------------------------------------------------------------------static inline TSmallEdit *ValidCtrCheck(){return new TSmallEdit(NULL);}//--------------------------------------------------------------------------__fastcall TSmallEdit::TSmallEdit(TComponent* Owner): TEdit(Owner){Color = clBlue;Font->Color = clYellow;Font->Name = "Times New Roman";Font->Size = 12;Font->Style = TFontStyles() Style defined as follows:enum TFontStyle { fsBold, fsItalic, fsUnderline, fsStrikeOut };typedef Set TFontStyles;NOTE: Don't let all this gobbledygook confuse you! All that occurs in this declaration is that a simple enum type isdeclared, and then you see a Set declaration for the type that ranges from a low value of fsBold to a high value offsStrikeOut.Piece of cake!If you want to add the underline and bold style to the text in the edit control, write the following:Font->Style = TFontStyles fsItalic;At this stage, the code is ready to go on the Component Palette.However, most of the time when you write components,you should test them first to see whether they work.Even on a fast machine, the process of recompiling CmpLib32.ccl and adding your component to the Component Palettetakes between 10 seconds and 2 minutes.Perhaps I'm a bit impatient, but that's a little too long a wait for me if all I need totweak is a few aspects of my code.As a result, I test things first in a small program and then add the component to the IDE.To test the new class, drop a button on the program's main form, and create an OnClick handler:void __fastcall TForm1::Button1Click(TObject *Sender){TSmallEdit *MyEdit = new TSmallEdit(this);MyEdit->Parent = this;MyEdit->Show();}Don't forget to use View | Project Manager and add the unit to the project.This ensures that the USEUNIT macro is placedin your project source.This code creates the component and shows it on the main form.this, of course, is the way that TForm1 refers to itselffrom inside one of its own methods.The owner of the new component is Form1, which will be responsible for disposing ofthe component when finished with it.This process happens automatically.You never need to worry about disposing of avisible component shown on a form.The parent of the component is also Form1.The Parent variable is used by Windows when it is trying to decide how todisplay the form on the screen.If you place a panel on a form and drop a button on the panel, the owner of that button is theform, but the parent is the panel.Ownership determines when and how the component is deallocated, and parentalrelationships determine where and how the component is displayed.Ownership is fundamentally a BCB issue, whereasparental relationships are primarily concerns of Windows
[ Pobierz całość w formacie PDF ]