Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
39
Добавлен:
16.04.2013
Размер:
6 Кб
Скачать
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "modal.h"
#include "inher.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//описание классов
class TSh;
class TMainShape : public TShape
{
 __published:
 __property OnMouseDown;
 __property OnMouseMove;
 __property OnMouseUp;
 __property OnDblClick;
 private:
 DYNAMIC void __fastcall DblClick(void);
 DYNAMIC void _fastcall MouseDown (TMouseButton Button, Classes::TShiftState Shift, int X, int Y);
 DYNAMIC void _fastcall MouseMove (Classes::TShiftState Shift, int X, int Y);
 DYNAMIC void _fastcall MouseUp (TMouseButton Button, Classes::TShiftState Shift, int X, int Y);

 bool Moved;
 int x0,y0;

 public:
  int id;
  bool active;
  TMainShape* next;
  TSh* list;
  bool GetActivity();
  int GetID();

  __fastcall virtual TMainShape (Classes::TComponent *Owner, TShapeType s):TShape(Owner)
  {
   Moved=false;
   next=NULL;
   active=false;
   list=NULL;
   Shape=s;
   Parent=(TWinControl *)Owner;
   Left=random(Parent->Width-20);
   Top=random(Parent->Height-20);
   Height=30+random(30);
   Visible=true;
  };
 __fastcall ~TMainShape()
 {
  if (next!=NULL) {delete next;}
 }
};
class TSh
{
 private:
 int k;
 TMainShape* head;
 TComponent *own;
 bool didntdelete;
 public:
 __fastcall void SetActive(int n);
 __fastcall void Add(TShapeType s);
 __fastcall void Delete(TMainShape *el);
 __fastcall void DeleteActive();
 __fastcall TSh(TComponent* Owner)
 {
  own=Owner;
  head=NULL;
  k=0;
  didntdelete=true;
 };
 __fastcall ~TSh()
  {
  delete head;
  };
};
//----реализация функций
 //---а) класс TMainShape
    void _fastcall TMainShape::MouseDown (TMouseButton Button, Classes::TShiftState Shift, int X, int Y)
    {
      x0=X;
      y0=Y;
      Moved=true;
      list->SetActive(id);
    };
   void _fastcall TMainShape::MouseMove (Classes::TShiftState Shift, int X, int Y)
    {
     if (Moved) {
       Left+=X-x0;
       Top+=Y-y0;
     }
    }
   void _fastcall TMainShape::MouseUp (TMouseButton Button, Classes::TShiftState Shift, int X, int Y)
   {
    Moved=false;
    if (Left<1) {Left=1;};
    if (Top<1) {Top=1;};
    if (Left>(Parent->Width-200)) {Left=(Parent->Width-220);};
    if (Top>(Parent->Height-40)) {Top=(Parent->Height-60);};
   }
   void __fastcall TMainShape::DblClick(void)
   {
   if (Shape==stCircle)
      Form2->ComboBox1->Text="Circle";
   if (Shape==stEllipse)
      Form2->ComboBox1->Text="Ellipse";
   if (Shape==stRectangle)
      Form2->ComboBox1->Text="Rectangle";

    if (Form2->ShowModal()==mrOk)
   {
  if (Form2->ComboBox1->Text!="")
  {
   if (Form2->ComboBox1->Text=="Circle")
       Shape=stCircle;
   if (Form2->ComboBox1->Text=="Ellipse")
        Shape=(stEllipse);
   if (Form2->ComboBox1->Text=="Rectangle")
        Shape=(stRectangle);
  }
   };
    Moved=false;
   };

   bool TMainShape::GetActivity()
   {
     return active;
   };
   int TMainShape::GetID()
   {
    return id;
   };
 //б) TSh
 void __fastcall TSh::SetActive(int n)
  {
   TMainShape *tmp=head;
    while (tmp!=NULL)
    {
     if (tmp->active) {
     tmp->active=false;
     tmp->Pen->Width=1;
     tmp->Pen->Color=clBlack;
     tmp->Brush->Color=clWhite;
     }
     if ((tmp->GetID())==n)
     {
     tmp->active=true;
     tmp->Pen->Width=1;  /**/
     tmp->Brush->Color=clBlue;  /**/
     }
     tmp=tmp->next;
    };
  };
 void __fastcall TSh::Add(TShapeType s)
   {
   TMainShape *Tmp;
   Tmp=new TMainShape(own,s);
   Tmp->next=head;
   Tmp->list=this;
   k++;
   Tmp->id=k;
   head=Tmp;

   };
 void __fastcall TSh::Delete(TMainShape *el)
   {
    if (el!=NULL)
    {
    TMainShape *Tmp=head;
    if (Tmp==el)
      {
       Tmp=head->next;
       head->next=NULL;
       delete head;
       head=Tmp;
      }
    else
      {
       while (Tmp->next!=el) Tmp=Tmp->next;
        Tmp->next=el->next;
        el->next=NULL;
        delete el;
       }
   }
   };
  void __fastcall TSh::DeleteActive()
  {
   TMainShape* tmp=head;
   while (tmp!=NULL&&didntdelete)
   {
    if (tmp->GetActivity())
    {
     Delete(tmp);
     didntdelete=false;
    }
   else
    {
    tmp=tmp->next;
    }
   }
   didntdelete=true;
  };
//------------
TForm1 *Form1;
TMainShape *l1;
TSh *ll;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{randomize();
 ComboBox1->Text="Circle";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
 ll= new TSh(this);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 /*if (ComboBox1->Text!="")       
   if (ComboBox1->Text=="Circle")
        ll->Add(stCircle);
   if (ComboBox1->Text=="Ellipse")
        ll->Add(stEllipse);
   if (ComboBox1->Text=="Rectangle")
        ll->Add(stRectangle);
     }  */
  if (RadioGroup1->ItemIndex==0)
        ll->Add(stCircle);
  if (RadioGroup1->ItemIndex==1)
   ll->Add(stEllipse);
  if (RadioGroup1->ItemIndex==2)
   ll->Add(stRectangle);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 ll->DeleteActive();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
 delete ll;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------

Соседние файлы в папке L5_3