关于组件制作的问题:

kingcaiyao 2002-07-10 07:45:18
我从TWinControl派生一个组件,目的是生成一个DBLookupComboBox,但问题出现了,当包编译,安装成功后。我将我的这个组件放到窗体上时,错误出现表现“control has no parent window",但事实上我已经重载CreateWnd函数。令人奇怪的是,如果我不是生成TDBLookupCombobOX,而是其它则没有问题。附代码,希望大家讨论:
//---------------------------------------------------------------------------
#ifndef KingLookupH
#define KingLookupH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <DBCtrls.hpp>
//---------------------------------------------------------------------------
class PACKAGE TKingLookup : public TWinControl
{
private:
TDBLookupComboBox FLookup;
protected:
virtual __fastcall ~TKingLookup();
virtual void __fastcall CreateWnd();
public:
__fastcall TKingLookup(TComponent* Owner);
__published:
};
//---------------------------------------------------------------------------
#endif
.cpp File
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "KingLookup.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TKingLookup *)
{
new TKingLookup(NULL);
}
//---------------------------------------------------------------------------
__fastcall TKingLookup::TKingLookup(TComponent* Owner)
: TDBLookupComboBox(Owner)
{
FLookup=new TDBLookupComboBox(this);
FLookup->Parent=this;
FLookup->Font->Color=clWhite;
FLookup->Color=clBlack;
FLookup->Left=Left;
FLookup->Top=Top+21;
FLookup->Width=100;
FLookup->Height=21;


}
//---------------------------------------------------------------------------
__fastcall TKingLookup::~TKingLookup()
{
if(FLookup)
{
delete FLookup;
}
}
void __fastcall TKingLookup::CreateWnd()
{
TWinControl::CreateWnd();
}
namespace Kinglookup
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TKingLookup)};
RegisterComponents("AKing", classes, 0);
}
}
//---------------------------------------------------------------------------
奇怪的是,这么一个简单的代码,莫非TDBLookupComboBox这个类本身还有其它的问题?
...全文
43 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Borlandor 2002-07-11
  • 打赏
  • 举报
回复
OK,很不错,应该从TCustomControl中继承。
kingcaiyao 2002-07-11
  • 打赏
  • 举报
回复
我将我的代码贴如下:
//---------------------------------------------------------------------------

#ifndef KingKingH
#define KingKingH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <dsgnintf.hpp>
//---------------------------------------------------------------------------
class PACKAGE TKingKing : public TCustomControl
{
private:
TDBLookupComboBox *FLookup;
TEdit *FEdit;
TDataSource *FDataSource;
TDataSource *FListSource;
AnsiString FListField;
AnsiString FKeyField;
AnsiString FDataField;

protected:
virtual void __fastcall Paint();
virtual __fastcall ~TKingKing();
public:
__fastcall TKingKing(TComponent* Owner);
void __fastcall SetDataSource(TDataSource *DataSource);
void __fastcall SetListSource(TDataSource *DataSource);
void __fastcall SetKeyField(AnsiString Field);
void __fastcall SetDataField(AnsiString Field);
void __fastcall SetListField(AnsiString Field);

__published:
__property TDataSource* DataSource={read=FDataSource,write=SetDataSource};
__property TDataSource *ListSource={read=FListSource,write=SetListSource};
__property AnsiString KeyField={read=FKeyField,write=SetKeyField};
__property AnsiString ListField={read=FListField,write=SetListField};
__property AnsiString DataField={read=FDataField,write=SetDataField};
};
//DataField PropertyEditor
class TDataFieldProperty:public TStringProperty
{
public:
TPropertyAttributes __fastcall GetAttributes()
{
return TPropertyAttributes()<<paValueList;
}
void __fastcall GetValues(TGetStrProc Proc)
{
Proc("None");
TDataSet *pDataSet;
TKingKing *pDCBX=dynamic_cast<TKingKing*>(GetComponent(0));
if(pDCBX)
{ if(pDCBX->DataSource)
{
pDataSet=pDCBX->DataSource->DataSet;
if(pDataSet)
{
TStrings *pList=new TStringList();
pDataSet->GetFieldNames(pList);

for(int i=0;i<pList->Count;i++)
{
Proc(pList->Strings[i]);
}
delete pList;
}
}
}
}
};
//ListField PropertyEditor
class TListFieldProperty:public TStringProperty
{
public:
TPropertyAttributes __fastcall GetAttributes()
{
return TPropertyAttributes()<<paValueList;
}
void __fastcall GetValues(TGetStrProc Proc)
{
Proc("None");
TDataSet *pDataSet;
TKingKing *pDCBX=dynamic_cast<TKingKing*>(GetComponent(0));
if(pDCBX)
{ if(pDCBX->ListSource)
{
pDataSet=pDCBX->ListSource->DataSet;
if(pDataSet)
{
TStrings *pList=new TStringList();
pDataSet->GetFieldNames(pList);
for(int i=0;i<pList->Count;i++)
{
Proc(pList->Strings[i]);
}
delete pList;
}
}
}
}
};
//KeyField PropertyEditor
class TKeyFieldProperty:public TStringProperty
{
public:
TPropertyAttributes __fastcall GetAttributes()
{
return TPropertyAttributes()<<paValueList;
}
void __fastcall GetValues(TGetStrProc Proc)
{
Proc("None");
TDataSet *pDataSet;
TKingKing *pDCBX=dynamic_cast<TKingKing*>(GetComponent(0));
if(pDCBX)
{ if(pDCBX->ListSource)
{
pDataSet=pDCBX->ListSource->DataSet;
if(pDataSet)
{
TStrings *pList=new TStringList();
pDataSet->GetFieldNames(pList);
for(int i=0;i<pList->Count;i++)
{
Proc(pList->Strings[i]);
}
delete pList;
}
}
}
}
};

//---------------------------------------------------------------------------
#endif
.cpp File
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "KingKing.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TKingKing *)
{
new TKingKing(NULL);
}
//---------------------------------------------------------------------------
TTypeInfo *AnsiStringInfo()
{
TTypeInfo *pTypeInfo=new TTypeInfo;
pTypeInfo->Kind=tkLString;
pTypeInfo->Name="AnsiString";
return pTypeInfo;
}

__fastcall TKingKing::TKingKing(TComponent* Owner)
: TCustomControl(Owner)
{
FLookup=new TDBLookupComboBox(this);
FEdit=new TEdit(this);
Width = 150;
Height=46;

}
//---------------------------------------------------------------------------
void __fastcall TKingKing::Paint()
{

Width = 150;
Height=46;
FLookup->Parent=this;
FLookup->Left=0;
FLookup->Top=0;
FLookup->Width=145;
FLookup->Height=21;

FEdit->Parent=this;
FEdit->Left=0;
FEdit->Top=FLookup->Height;
FEdit->Width=80;
FEdit->Height=21;
}
void __fastcall TKingKing::SetDataSource(TDataSource *DataSource)
{
FDataSource=DataSource;
}
//--------------------------------------------------------------------------
void __fastcall TKingKing::SetListSource(TDataSource *DataSource)
{
FListSource=DataSource;
}
//--------------------------------------------------------------------------
void __fastcall TKingKing::SetKeyField(AnsiString Field)
{
FKeyField=Field;
}
void __fastcall TKingKing::SetListField(AnsiString Field)
{
FListField=Field;
}
void __fastcall TKingKing::SetDataField(AnsiString Field)
{
FDataField=Field;
}

__fastcall TKingKing::~TKingKing()
{
if(FLookup)
{
delete FLookup;
}
if(FEdit)
{
delete FEdit;
}

}


namespace Kingking
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TKingKing)};
RegisterPropertyEditor(AnsiStringInfo(),__classid(TKingKing),"DataField",__classid(TDataFieldProperty));
RegisterPropertyEditor(AnsiStringInfo(),__classid(TKingKing),"ListField",__classid(TListFieldProperty));
RegisterPropertyEditor(AnsiStringInfo(),__classid(TKingKing),"KeyField",__classid(TKeyFieldProperty));
RegisterComponents("AKing", classes, 0);
}
}
//---------------------------------------------------------------------------
kingcaiyao 2002-07-11
  • 打赏
  • 举报
回复
问题差不多已被我找到了。因为我对两个组件进行比较发现:TDBLookupComboBox和TDBComboBox发现,首先它们的继承的类层次不同,其中TDBComboBox的继承层次有TCustomComboBox(而这个类是TComboBox的父类),而TDBLookupComboBox的最直接父类就是TCustomControl,这恰恰证明TDBLookupComboBox是放在Paint函数中被画出来的。而我的错误代码是从TWinControl继承下来的,而父类为TWinControl控件的最大特点就是自已能够画出自已,而TCustomControl则需要自已画自已(用Paint方法),据此可以判断TDBLookupComboBox为什么在设计期间就已经有了句柄的理由就是在从TCustomControl继承的过程序,TDBLookupComboBox不仅重载了Paint方法,还重载了CreateWnd方法。而如果你从TWinControl继承,再动态生成一个TDBLookupComboBox就会出现错误提示”该控件没有父窗口“。
欢迎各位网友参与讨论。
kingcaiyao 2002-07-10
  • 打赏
  • 举报
回复
看来c++ builder和Delphi论坛对组件有研究的人比较少。
dupenf 2002-07-10
  • 打赏
  • 举报
回复
代码没看到阿
netlib 2002-07-10
  • 打赏
  • 举报
回复
老大,帖错论坛了 。

5,927

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧