Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a component as a property of another?

Status
Not open for further replies.

BubbaDave

Programmer
Dec 30, 2000
23
US
I am new to component building and would like to create one that will draw a line at the bottom of the window then scroll up a few pixels and draw another line, etc.

I think I can do this by deriving from a TScrollingWinControl, but I need a TCanvas control to draw on. Herein lies my problem. I don't know how (if at all) to add a TCanvas control to a derived component.

Can anyone help?
 
Hi, I'm mbekle.

Here is my code.

TMyScrollingWinControl : public TScrollingWinControl
{
private :
TCanvas *FCanvas;
void __fastcall WMPaint(TWMPaint &Msg);

protected:
virtual void __fastcall Paint();

public:
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_PAINT,TWMPaint,WMPaint)
END_MESSAGE_MAP(TScrollingWinControl)

__published:
__property TCanvas *Canvas = {read=FCanvas};
};

in constructor
__fastcall TMyScrollingWinControl::TMyScrollingWinControl(TComponent *Owner)
: TScrollingWinControl(Owner)
{
FCanvas = new TControlCanvas();
dynamic_cast<TControlCanvas *>(FCanvas)->Control=this;
}

in destructor
__fastcall TMyScrollingWinControl::~TMyScrollingWinControl(TComponent *Owner)
: TScrollingWinControl(Owner)
{
if(FCanvas) delete FCanvas;
}

void __fastcall TMyScrollingWinControl::WMPaint(TWMPaint &Msg)
{
if(Msg.DC != NULL)
{
Canvas->Lock();
try
{
Canvas->Handle=Message.DC;
try
{
Paint();
}
__finally { Canvas->Handle=0; }
}
__finally { Canvas->Unlock(); }
}
}

void __fastcall TMyScrollingWinControl::paint()
{
// add you're painting code here.
}

may it be easy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top