Displaying TForm from a new component

Hello,
a question, I have some basic component in the form of a bit image, and when I click it I want it to open a new TForm with a button and a label on it, so that the label displays the state of the component.
The question is how to do it? Hwo to create dynamicallt a TForm with some elements on it and manage the form from the component level.
Here is the code of my element.
[code]#ifndef ShapeSampleH
#define ShapeSampleH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
// type of the SampleShape shape
enum TSampleShapeType { sstRectangle, sstSquare, sstRoundRect, sstRoundSquare, sstEllipse, sstCircle };

class PACKAGE TShapeSample : public TGraphicControl
{
private:
TSampleShapeType FShape;
unsigned int iSize;
TPen *FPen; // a data member for the pen object
TBrush *FBrush; // a data member for the brush object
void __fastcall SetShape(TSampleShapeType Value);
void __fastcall SetSize(unsigned int iValue);
void __fastcall SetBrush(TBrush *Value);
void __fastcall SetPen(TPen *Value);
void __fastcall StyleChanged(TObject* Sender);
void __fastcall MouseClick(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y);
protected:
virtual void __fastcall Paint();
public:
__fastcall TShapeSample(TComponent* Owner);
__fastcall ~TShapeSample();
__published:
__property DragCursor ;
__property DragMode ;
__property OnDragDrop ;
__property OnDragOver ;
__property OnEndDrag ;
__property OnMouseDown ;
__property OnMouseMove ;
__property OnMouseUp;
__property OnClick;
__property Height;
__property Width;
__property TSampleShapeType Shape = {read=FShape, write=SetShape, default=sstEllipse};
__property unsigned int Size = {read = iSize, write=SetSize, default=100};
__property TBrush* Brush = {read=FBrush, write=SetBrush, nodefault};
__property TPen* Pen = {read=FPen, write=SetPen, nodefault};
};[/code]
and implementation file
[code]#include
#pragma hdrstop

#include "ShapeSample.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(TShapeSample *)
{
new TShapeSample(NULL);
}
//---------------------------------------------------------------------------
__fastcall TShapeSample::TShapeSample(TComponent* Owner)
: TGraphicControl(Owner)
{
this->Width = 65;
this->Height = 65;
this->Size = 100;
FBrush = new TBrush();
FBrush->OnChange = StyleChanged;
FPen = new TPen();
FPen->OnChange = StyleChanged;
this->OnMouseDown = MouseClick;
}

__fastcall TShapeSample::~TShapeSample()
{
delete FPen; // delete the pen object
delete FBrush;
}
//---------------------------------------------------------------------------
namespace Shapesample
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TShapeSample)};
RegisterComponents("Samples", classes, 0);
}
}
//---------------------------------------------------------------------------

void __fastcall TShapeSample::SetShape(TSampleShapeType Value)
{
if (FShape != Value) // ignore if this isn't a change
{
FShape = Value; // store the new value
Invalidate(); // force a repaint with the new shape
}
}

void __fastcall TShapeSample::SetSize(unsigned int iValue)
{
this->iSize = iValue;
this->Invalidate();
}

void __fastcall TShapeSample::SetBrush( TBrush* Value)
{
FBrush->Assign(Value);
}

void __fastcall TShapeSample::SetPen( TPen* Value)
{
FPen->Assign(Value);
}

void __fastcall TShapeSample::StyleChanged( TObject* /*Sender*/)
{
Invalidate(); // repaints the component
}

void __fastcall TShapeSample::Paint(void)
{
int X,Y,W,H,S;
Canvas->Pen = FPen; // copy the component's pen
Canvas->Brush = FBrush; // copy the component's brush
W=Width; // use the component width
H=Height; // use the component height
X=Y=0; // save smallest for circles/squares
if( WRectangle(X,Y,X+W,Y+H);
break;
case sstRoundSquare: // draw rounded rectangles and squares
W=H=S;
case sstRoundRect:
Canvas->RoundRect(X,Y,X+W,Y+H,S/4,S/4);
break;
case sstCircle: // draw circles and ellipses
W=H=S;
case sstEllipse:
Canvas->Ellipse(X,Y,X+W,Y+H);

break;
default:
break;
}
}

void __fastcall TShapeSample::MouseClick(TObject * /*Sender*/, TMouseButton Button, TShiftState Shift, int X, int Y)
{

if (Button == mbLeft)
{
this->Canvas->TextOutA(0,0,"Left");
}
} [/code]
Thanks for any help / suggestions.
I do apologize for any stupid coding errors. This is my first component.
Best wishes
Marek
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories