Re[2]: Проблема с опубликованием свойства
От: zloy_babay  
Дата: 30.07.08 07:18
Оценка:
Здравствуйте, Danchik, Вы писали:

D>Без кода это телепатия


Хидер:

#ifndef MyCompH
#define MyCompH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <Controls.hpp>   
#include <DesignEditors.hpp>
//---------------------------------------------------------------------------
class PACKAGE TTestProp : public TPersistent
{
  public:
    TTestProp();

    AnsiString strA;
    AnsiString strB;
};


class PACKAGE TMyComp : public TCustomControl
{
private:
  TTestProp *FTestProp;
protected:
  
  void __fastcall ReadTestProp(TReader *r);
  void __fastcall WriteTestProp(TWriter *w);
  virtual void __fastcall DefineProperties(TFiler* Filer);     
  void __fastcall setTestProp(TTestProp* p);
public:
  __fastcall TMyComp(TComponent* Owner);
  __fastcall ~TMyComp();
__published:
  __property TTestProp* TestProp = {read = FTestProp, write = setTestProp};
};                                                                        


class TTestPropProperty : public TPropertyEditor
{
  public:
    virtual TPropertyAttributes __fastcall GetAttributes(void);
    virtual AnsiString __fastcall GetValue();
};
//---------------------------------------------------------------------------
#endif


Имлпементация:


#include <vcl.h>
#include <DesignIntf.hpp>

#pragma hdrstop

#include "MyComp.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(TMyComp *)
{
  new TMyComp(NULL);
}                                                                            
//---------------------------------------------------------------------------
TTestProp::TTestProp()
  : TPersistent()
{
  strA = "Строка A"; 
  strB = "Строка B";
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
__fastcall TMyComp::TMyComp(TComponent* Owner)
  : TCustomControl(Owner)
{
  FTestProp = new TTestProp;
}
//---------------------------------------------------------------------------
__fastcall TMyComp::~TMyComp()
{
  delete FTestProp;
}
//---------------------------------------------------------------------------
void __fastcall TMyComp::ReadTestProp(TReader *r)
{          
  try
  {
    r->ReadListBegin();
    int c = 0;
    while (!r->EndOfList())
    {
      AnsiString s = r->ReadString();
      if (!c)
        FTestProp->strA = s;
      else                  
        FTestProp->strB = s;
      c++;
    }
    r->ReadListEnd();
  } catch(...) {}
}
//---------------------------------------------------------------------------
void __fastcall TMyComp::WriteTestProp(TWriter *w)
{         
  w->WriteListBegin();
  w->WriteString(FTestProp->strA); 
  w->WriteString(FTestProp->strB);
  w->WriteListEnd();
}
//---------------------------------------------------------------------------
void __fastcall TMyComp::DefineProperties(TFiler* Filer)
{
  TCustomControl::DefineProperties(Filer);
  Filer->DefineProperty("TestProp", ReadTestProp, WriteTestProp, true);
}
//---------------------------------------------------------------------------
void __fastcall TMyComp::setTestProp(TTestProp* p)
{
  if (p)
  {
    delete FTestProp;
    FTestProp = p;
  }
}                               
//--------------------------------------------------------------------------- 
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
TPropertyAttributes __fastcall TTestPropProperty::GetAttributes(void)
{
  TPropertyAttributes res;
  res << paReadOnly;
  return res;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TTestPropProperty::GetValue()
{
  return AnsiString("(TTestProp)");
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
namespace Mycomp
{
  void __fastcall PACKAGE Register()
  {
     TComponentClass classes[1] = {__classid(TMyComp)};
     RegisterComponents("Samples", classes, 0);   
     RegisterPropertyEditor(__typeinfo(TTestProp), __classid(TMyComp), "TestProp", __classid(TTestPropProperty));
  }
}
//---------------------------------------------------------------------------
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.