⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch22.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 5 页
字号:

</FONT></PRE>
<P>Sorry about bringing 
Object Pascal code into the book again, but I think it helps
to see what is happening behind the scenes in cases like this one. I will talk about
<TT>RegisterPropertyEditor</TT> later in this chapter. Notice that the VCL calls

<TT>RegisterComponents</TT> just as you do. When you recompile <TT>Cmplib</TT>, you
are rebuilding part of BCB itself and simultaneously redefining the way the VCL works.</P>
<P>The DCR files listed in <TT>CmpLib32.cpp</TT> are a series of standard 
Windows
resources that contain bitmaps that are placed in the Component Palette. The bitmaps
representing each component come from here. In the case of <TT>TSmallEdit</TT>, it
descends from <TT>TEdit</TT>, and so it inherits <TT>TEdit</TT>'s bitmap, 
unless
I create a DCR file for the component. I will create and discuss DCR files later
in this chapter and in the next chapter.</P>
<P>The key point to grasp about DCR files is that they are standard Windows resources
containing a bitmap. You can 
build them with the Image Editor that ships with BCB.
<H2><FONT COLOR="#000077">Extending the Unleash Unit</FONT></H2>
<P>In the second version of the <TT>Unleash</TT> unit, a new edit control is added,
along with two labels and two panels. The 
additional edits and labels show how quickly
you can build on an idea or object when you understand where you're headed. One of
the panels shows how you can get rid of the annoying label that always shows up in
the middle of a panel, and the other 
shows a preliminary take on how you can create
a single component that contains other components. Specifically, it shows how to
create a panel that comes already equipped with two radio buttons.</P>
<P>The code for the new version of the 
<TT>Unleash2.cpp</TT> unit is shown in Listings
22.6 and 22.7, and its test bed appears in Listing 22.8. Run <TT>TestBed</TT> first,
and make sure the new components are working correctly. If all is well, then add
the new components to the Component 
Palette.</P>
<P>If you have two units on your system, both of which contain instances of <TT>TSmallEdit</TT>,
uninstalling the first instance before trying to install the new instance is probably
best. For instance, you may have two files on your 
system, one called <TT>Unleash1.cpp</TT>
and the second called <TT>Unleash2.cpp</TT>. Both might contain an instance of <TT>TSmallEdit</TT>.
Under such circumstances, it's best to use Component | Install | Remove to remove
the first version of 
<TT>TSmallEdit</TT> before replacing it with a second version.
A second alternative is just to rename the second version of the component to <TT>TSmallEditTwo</TT>,
which is what I do with the source found on the CD that accompanies this book. If
you 
have only one version of <TT>TSmallEdit</TT>, and you just want to update it,
you don't need to remove the first instance before installing the updated instance.
<H4><FONT COLOR="#000077">Listing 22.6. The header for the second version of the

Unleash2.cpp unit contains a class that comes equipped with two radio buttons.</FONT></H4>
<PRE><FONT COLOR="#0066FF">

///////////////////////////////////////

// Unleash2.h

// Simple components

// Copyright (c) 1997 by Charlie Calvert

//

#ifndef 
Unleash2H

#define Unleash2H

//--------------------------------------------------------------------------

#include &lt;vcl\sysutils.hpp&gt;

#include &lt;vcl\controls.hpp&gt;

#include &lt;vcl\classes.hpp&gt;

#include &lt;vcl\forms.hpp&gt;


#include &lt;vcl\StdCtrls.hpp&gt;

//--------------------------------------------------------------------------

class TSmallEditTwo : public TEdit

{

public:

  virtual __fastcall TSmallEditTwo(TComponent* Owner);

};



class TBigEdit : public 
TSmallEditTwo

{

public:

  virtual __fastcall TBigEdit(TComponent* Owner)

    :TSmallEditTwo(Owner) { Width = 250; Font-&gt;Size = 24; }

};



class TSmallLabel : public TLabel

{

public:

  virtual __fastcall TSmallLabel(TComponent* Owner);

};




class TBigLabel : public TSmallLabel

{

public:

  virtual __fastcall TBigLabel(TComponent *Owner)

    :TSmallLabel(Owner) { Font-&gt;Size = 24; }

};



class TEmptyPanel : public TPanel

{

protected:

  void __fastcall Loaded(void);

public:

  
virtual __fastcall TEmptyPanel(TComponent *Owner);

  virtual void __fastcall SetParent(TWinControl *AParent);

};



class TRadio2Panel : public TEmptyPanel

{

private:

  TRadioButton *FRadio1;

  TRadioButton *FRadio2;

public:

  virtual 
__fastcall TRadio2Panel(TComponent *Owner);

  __property TRadioButton *Radio1={read=FRadio1};

  __property TRadioButton *Radio2={read=FRadio2};

};



//--------------------------------------------------------------------------

</FONT></PRE>

<P><FONT COLOR="#0066FF"><TT>#endif</TT></FONT>
<H4><FONT COLOR="#000077">Listing 22.7. The second version of the Unleash unit, called
Unleash2, contains a class that comes equipped with two radio buttons.</FONT></H4>
<PRE><FONT COLOR="#0066FF">


///////////////////////////////////////

// Unleash2.cpp

// Simple components

// Copyright (c) 1997 by Charlie Calvert

//

#include &lt;vcl\vcl.h&gt;

#pragma hdrstop

#include &quot;Unleash2.h&quot;



///////////////////////////////////////

// 
TSmallEditTwo //////////////////////

///////////////////////////////////////



static inline TSmallEditTwo *ValidCtrCheck()

{

  return new TSmallEditTwo(NULL);

}



__fastcall TSmallEditTwo::TSmallEditTwo(TComponent* Owner)

  : TEdit(Owner)

{

  
Color = clBlue;

  Font-&gt;Color = clYellow;

  Font-&gt;Name = &quot;Times New Roman&quot;;

  Font-&gt;Size = 12;

  Font-&gt;Style = TFontStyles() &lt;&lt; fsBold &lt;&lt; fsItalic;

}



///////////////////////////////////////

// TSmallLabel 
////////////////////////

///////////////////////////////////////



__fastcall TSmallLabel::TSmallLabel(TComponent* Owner)

  : TLabel(Owner)

{

  Color = clBlue;

  Font-&gt;Color = clYellow;

  Font-&gt;Name = &quot;Times New Roman&quot;;

  
Font-&gt;Size = 12;

  Font-&gt;Style = TFontStyles() &lt;&lt; fsBold;

}



///////////////////////////////////////

// TEmptyPanel ////////////////////////

///////////////////////////////////////



__fastcall TEmptyPanel::TEmptyPanel(TComponent 
*Owner)

   :TPanel(Owner)

{

}



void __fastcall TEmptyPanel::Loaded(void)

{

  TPanel::Loaded();

  Caption = &quot;&quot;;

}



void __fastcall TEmptyPanel::SetParent(TWinControl *AParent)

{

  TPanel::SetParent(AParent);

  Caption = 
&quot;&quot;;

}





///////////////////////////////////////

// TRadio2Panel ///////////////////////

///////////////////////////////////////



__fastcall TRadio2Panel::TRadio2Panel(TComponent *Owner)

  :TEmptyPanel(Owner)

{

  Width = 175;

  
Height = 60;



  FRadio1 = new TRadioButton(this);

  FRadio1-&gt;Parent = this;

  FRadio1-&gt;Caption = &quot;Radio1&quot;;

  FRadio1-&gt;Left = 20;

  FRadio1-&gt;Top = 10;

  FRadio1-&gt;Show();



  FRadio2 = new TRadioButton(this);

  
FRadio2-&gt;Parent = this;

  FRadio2-&gt;Caption = &quot;Radio2&quot;;

  FRadio2-&gt;Left = 20;

  FRadio2-&gt;Top = 32;

  FRadio2-&gt;Show();

}



namespace Unleash2

{

  void __fastcall Register()

  {

    TComponentClass 
classes[6]={__classid(TSmallEditTwo),

      __classid(TBigEdit), __classid(TSmallLabel), __classid(TBigLabel),

      __classid(TEmptyPanel), __classid(TRadio2Panel)};

    RegisterComponents(&quot;Unleash&quot;, classes, 5);

  }

}

</FONT></PRE>

<P><FONT COLOR="#0066FF"><TT>//--------------------------------------------------------------------------</TT></FONT>
<H4><FONT COLOR="#000077">Listing 22.8. The test bed for the Unleash2 unit.</FONT></H4>
<PRE><FONT COLOR="#0066FF">


///////////////////////////////////////

// Main.cpp

// Simple components: TestUnleash2

// Copyright (c) 1997 by Charlie Calvert

//

#include &lt;vcl\vcl.h&gt;

#pragma hdrstop

#include &quot;Main.h&quot;

#include &quot;Unleash2.h&quot;

#pragma 
link &quot;Unleash1&quot;

#pragma link &quot;Unleash2&quot;

#pragma resource &quot;*.dfm&quot;

TForm1 *Form1;

__fastcall TForm1::TForm1(TComponent* Owner)

  : TForm(Owner)

{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -