[最終更新日 1999/03/14]
ここでは、実際にコンポーネントを作成する作業に入ります。
最初はコンポーネントを作成するフォルダを決めておきます。これから自作コンポーネントを色々作るときのベースとなるフォルダなので、自分の分かりやすいフォルダ名にしておきましょう。またコンポーネント毎にフォルダを下層に作成することをお勧めします。テストプログラムなど多数のファイルが作成されるので、複数のコンポーネントを同一フォルダで作成するのはファイル破損の元です。
ここでは、C:\Component\TUIntEdit100 とします。後ろにつけた”100”はバージョンを示します。
とはいっても、これは自分で決めることです。ご自由にどうぞ(笑)
Delphi、C++Builderを立ち上げてすぐに、メニューの[ファイル(F)|すべて保存(V)]で先ほど作成したフォルダにプロジェクトをすべて保存してください。このプロジェクトをテスト用のベースアプリケーションとします。
メニューの[コンポーネント(C)|コンポーネントの新規作成(N)]で”コンポーネントの新規作成”ダイアログを表示し、それぞれの項目を入力していきます。
この入力が完了したら[ユニット作成(C)]または[OK]ボタンを押してください。
次にメニューの[ファイル(F)|すべて保存(V)]で作成したコンポーネントユニットを保存してください。
プロジェクトにコンポーネントユニットを追加します。メニューの[プロジェクト(P)|プロジェクトに追加(A)]で先ほど作成したユニットを追加してください。
Unit1.pas または Unit1.cpp にコンポーネントを動的生成するコードを入力します。オブジェクトインスペクタで[Form1: TForm1]を選び、[イベント]タブを押して、[OnCreate]をダブルクリックします。
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, UIntEdit; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private 宣言 } public { Public 宣言 } UIntEdit1: TUIntEdit; end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin UIntEdit1 := TUIntEdit.Create(Self); UIntEdit1.Parent := Self; UIntEdit1.Name := 'UIntEdit1'; UIntEdit1.Top := 10; UIntEdit1.Left := 10; end; end.
//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include "UIntEdit.h" //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE 管理のコンポーネント void __fastcall FormCreate(TObject *Sender); private: // ユーザー宣言 public: // ユーザー宣言 TUIntEdit *UIntEdit1; __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { UIntEdit1 = new TUIntEdit(this); UIntEdit1->Parent = this; UIntEdit1->Name = "UIntEdit1"; UIntEdit1->Top = 10; UIntEdit1->Left = 10; } //---------------------------------------------------------------------------
メニューの[実行(R)|実行(R)]で実行してみてください。入力ミスが無ければフォームが表示されます。設計時にコンポーネントを1つも配置していないのにEditコンポーネントが左上にあるはずです。これは動的に生成されたTUIntEditコンポーネントです。
これで最初のステップであるコンポーネントの作成が完了しました。
Delphiのコンポーネントソース
”UIntEdit.pas”
unit UIntEdit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TUIntEdit = class(TEdit) private { Private 宣言 } protected { Protected 宣言 } public { Public 宣言 } published { Published 宣言 } end; procedure Register; implementation procedure Register; begin RegisterComponents('MyCompo', [TUIntEdit]); end; end.
C++Builder3のコンポーネントソース(ヘッダーファイル)
”UIntEdit.h”
//--------------------------------------------------------------------------- #ifndef UIntEditH #define UIntEditH //--------------------------------------------------------------------------- #include <SysUtils.hpp> #include <Controls.hpp> #include <Classes.hpp> #include <Forms.hpp> #include <StdCtrls.hpp> //--------------------------------------------------------------------------- class PACKAGE TUIntEdit : public TEdit { private: protected: public: __fastcall TUIntEdit(TComponent* Owner); __published: }; //--------------------------------------------------------------------------- #endif
C++Builder3のコンポーネントソース(ユニット本体)
”UIntEdit.cpp”
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "UIntEdit.h" #pragma package(smart_init) //--------------------------------------------------------------------------- // ValidCtrCheck は作成されたコンポーネントが純粋仮想関数を持たないことを // 保証するために使用されます。 // static inline void ValidCtrCheck(TUIntEdit *) { new TUIntEdit(NULL); } //--------------------------------------------------------------------------- __fastcall TUIntEdit::TUIntEdit(TComponent* Owner) : TEdit(Owner) { } //--------------------------------------------------------------------------- namespace Uintedit { void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TUIntEdit)}; RegisterComponents("MyCompo", classes, 0); } } //---------------------------------------------------------------------------
ここまでやってみてどうでしょうか?
何これ、簡単じゃないか。と思えばあなたはコンポーネント作者としての素質があるかもしれません(笑)
でもこのままでは、TEditと同じ機能しか持っていません。
さて次はプロパティの追加にチャレンジです。
レッツ、トライ!コンポーネント