实现了画图的功能!(BC++ 程序转换 to Delphi! ) ( 积分: 100 )

  • 主题发起人 主题发起人 我不是专家
  • 开始时间 开始时间

我不是专家

Unregistered / Unconfirmed
GUEST, unregistred user!
class YObjects
{
public:
YObject *head,*tail;
YObjects(){head=tail=NULL;}
~YObjects(){DeleteAll();}
void Add(YObject *obj);
void Delete(YObject *obj);
void Delete(int index);
void DeleteAll();
void UnSelectAll();
void SelectAll();
void MoveToBottom(YObject *obj);
void MoveToFront(YObject *obj){};
void Draw();
void Save(String path);
YObject *WhoIsIt(int x,int y);
virtual void LeftPressed(int X,int Y);
virtual void MouseMoved(int X,int Y);
virtual void LeftReleased(int X,int Y);
};

///////////
class YObject
{
protected:
int sx,sy;
public:
static bool needredraw;
static int originx,originy;
int X,Y;
bool selected,pressed;
enum YWHICHBOX which;
TCanvas *canvas;
YObject *next,*prior;
YObject(){X=Y=0;next=prior=NULL;canvas=NULL;selected=false;pressed=false;}
virtual void Draw();
virtual String Save();
virtual void DrawFocus(){};
virtual bool IsMe(int x,int y){return false;}
virtual enum YWHICHBOX InWhichBox(int x,int y);
virtual void MoveTo(int x,int y);
virtual void Move(int dx,int dy);
static bool IsInBox(int x,int y,int left,int top,int width,int height);
virtual void LeftPressed(int X,int Y){};
virtual void MouseMoved(int X,int Y){};
virtual void LeftReleased(int X,int Y){};
};





bool YObject::needredraw=false;
int YObject::originx=0;
int YObject:: originy=0;
void YObject::MoveTo(int x,int y)
{
X=x;Y=y;
}
void YObject::Move(int dx,int dy)
{
X+=dx;Y+=dy;
}

String YObject::Save()
{
return "NULL";
}
enum YWHICHBOX YObject::InWhichBox(int x,int y)
{
return EXTE;
}
bool YObject::IsInBox(int x,int y,int left,int top,int width,int height)
{
return x>=left &amp;&amp; x<=left+width &amp;&amp; y>=top &amp;&amp; y<=top+height;
}
void YObject::Draw()
{
if(canvas==NULL) return;
if(selected) DrawFocus();
//canvas->TextOutA(X,Y,&quot;obj&quot;);
}
class YLine:public YObject
{
public:
int EX,EY;
YLine(){EX=EY=10;}
void Draw();
String Save();
void DrawFocus();
void Move(int dx,int dy);
void MoveTo(int x,int y);
bool IsMe(int x,int y);
enum YWHICHBOX InWhichBox(int x,int y);
virtual void LeftPressed(int X,int Y);
virtual void MouseMoved(int X,int Y);
virtual void LeftReleased(int X,int Y);
};










String YLine::Save()
{
String str;
str=&quot;L|&quot;+IntToStr(X)+&quot;|&quot;;
str=str+IntToStr(Y)+&quot;|&quot;;
str=str+IntToStr(EX)+&quot;|&quot;;
str=str+IntToStr(EY);
return str;
}


void YLine::Draw()
{
if(canvas==NULL) return;
YObject::Draw();
canvas->MoveTo(X,Y);
canvas->LineTo(EX,EY);
}
void YLine::DrawFocus()
{
int d=RECTLETW,w,h;
if(canvas==NULL) return;
canvas->Rectangle(X-d,Y-d,X+d,Y+d);
w=(EX-X)/2;h=(EY-Y)/2;
canvas->Rectangle(X+w-d,Y+h-d,X+w+d,Y+h+d);
canvas->Rectangle(EX-d,EY-d,EX+d,EY+d);
}
enum YWHICHBOX YLine::InWhichBox(int x,int y)
{
int d=RECTLETW,w,h;
w=(EX-X);h=(EY-Y);
if(IsInBox(x,y,X-d,Y-d,2*d,2*d)) return LT;
if(IsInBox(x,y,X+w/2-d,Y+h/2-d,2*d,2*d)) return MT;
if(IsInBox(x,y,X+w-d,Y+h-d,2*d,2*d)) return RB;
return EXTE;
}
void YLine::LeftPressed(int X,int Y)
{
pressed=true;
sx=X;sy=Y;
if(!selected){
if(IsMe(X,Y)) selected=true;
}
if(selected){
which=InWhichBox(X,Y);
if(which==EXTE) selected=false;
}
}
void YLine::MouseMoved(int x,int y)
{
if(!pressed) return;
if(selected){
switch(which)
{
case LT:
X+=x-sx;Y+=y-sy;
sx=x;sy=y;
needredraw=true;
break;
case MT:
X+=x-sx;Y+=y-sy;
EX+=x-sx;EY+=y-sy;
sx=x;sy=y;
needredraw=true;
break;
case RB:
EX+=x-sx;EY+=y-sy;
sx=x;sy=y;
needredraw=true;
break;
}
}
}
void YLine::LeftReleased(int X,int Y)
{
pressed=false;
}

void YLine::Move(int dx,int dy)
{
X+=dx;Y+=dy;
EX+=dx;EY+=dy;
}
void YLine::MoveTo(int x,int y)
{
int dx,dy;
dx=x-X;dy=y-Y;
Move(dx,dy);
}
bool YLine::IsMe(int x,int y)
{
int d=RECTLETW;
if(x>=X-d &amp;&amp; x<=X+d &amp;&amp; y>=Y-d &amp;&amp; y<=Y+d) return true;
if(x>=EX-d &amp;&amp; x<=EX+d &amp;&amp; y>=EY-d &amp;&amp; y<=EY+d) return true;
return false;
}




class YRectangle:public YObject
{
public:
int width,height;
YRectangle(){width=10;height=10;}
void Draw();
void DrawFocus();
String Save();
enum YWHICHBOX InWhichBox(int x,int y);
bool IsMe(int x,int y){return (x>=X &amp;&amp; x<=X+width &amp;&amp; y>=Y &amp;&amp; y<=Y+height);}
virtual void LeftPressed(int X,int Y);
virtual void MouseMoved(int X,int Y);
virtual void LeftReleased(int X,int Y);

};



class YEllipse:public YRectangle
{
public:
void Draw();
};

void YRectangle::Draw()
{
if(canvas==NULL) return;
YObject::Draw();
canvas->Rectangle(X+originx,Y+originy,X+originx+width,Y+originy+height);
}
String YRectangle::Save()
{
String str;
str=&quot;R|&quot;+IntToStr(X)+&quot;|&quot;;
str=str+IntToStr(Y)+&quot;|&quot;;
str=str+IntToStr(width)+&quot;|&quot;;
str=str+IntToStr(height);
return str;
}

void YRectangle::DrawFocus()
{
int d=RECTLETW;
int w=width,h=height;
int x=X+originx,y=Y+originy;
if(canvas==NULL) return;
canvas->Rectangle(x-d,y-d,x+d,y+d);
canvas->Rectangle(x+w/2-d,y-d,x+w/2+d,y+d);
canvas->Rectangle(x+w-d,y-d,x+w+d,y+d);
canvas->Rectangle(x+w-d,y+h/2-d,x+w+d,y+h/2+d);
canvas->Rectangle(x+w-d,y+h-d,x+w+d,y+h+d);
canvas->Rectangle(x+w/2-d,y+h-d,x+w/2+d,y+h+d);
canvas->Rectangle(x-d,y+h-d,x+d,y+h+d);
canvas->Rectangle(x-d,y+h/2-d,x+d,y+h/2+d);
}
enum YWHICHBOX YRectangle::InWhichBox(int x,int y)
{
int d=RECTLETW;
int w=width,h=height;
if(IsInBox(x,y,X-d,Y-d,2*d,2*d)) return LT;
if(IsInBox(x,y,X+w/2-d,Y-d,2*d,2*d)) return MT;
if(IsInBox(x,y,X+w-d,Y-d,2*d,2*d)) return RT;
if(IsInBox(x,y,X+w-d,Y+h/2-d,2*d,2*d)) return RM;
if(IsInBox(x,y,X+w-d,Y+h-d,2*d,2*d)) return RB;
if(IsInBox(x,y,X+w/2-d,Y+h-d,2*d,2*d)) return MB;
if(IsInBox(x,y,X-d,Y+h-d,2*d,2*d)) return LB;
if(IsInBox(x,y,X-d,Y+h/2-d,2*d,2*d)) return LM;
if(IsInBox(x,y,X,Y,w,h)) return INNER;
return EXTE;
}
void YRectangle::LeftPressed(int X,int Y)
{
pressed=true;
sx=X;sy=Y;
if(!selected){
if(IsMe(X,Y)) selected=true;
}
if(selected){
which=InWhichBox(X,Y);
if(which==EXTE) selected=false;
}
}
void YRectangle::MouseMoved(int x,int y)
{
if(!pressed) return;
if(selected){
int dx,dy;
dx=x-sx;dy=y-sy;
switch(which)
{
case LT:
width-=dx;height-=dy;
X=x;Y=y;
sx=x;sy=y;
needredraw=true;
break;
case MT:
height-=dy;Y=y;
sx=x;sy=y;
needredraw=true;
break;
case RT:
width+=dx;height-=dy;
Y=y;
sx=x;sy=y;
needredraw=true;
break;
case RM:
width+=dx;
sx=x;sy=y;
needredraw=true;
break;
case RB:
width+=dx;height+=dy;
sx=x;sy=y;
needredraw=true;
break;
case MB:
height+=dy;
sx=x;sy=y;
needredraw=true;
break;
case LB:
width-=dx;height+=dy;
X=x;
sx=x;sy=y;
needredraw=true;
break;
case LM:
width-=dx;X=x;
sx=x;sy=y;
needredraw=true;
break;
case INNER:
Move(dx,dy);
sx=x;sy=y;
needredraw=true;
break;
}
}
}
void YRectangle::LeftReleased(int X,int Y)
{
pressed=false;
}

void YEllipse::Draw()
{
if(canvas==NULL) return;
if(selected){
YRectangle::Draw();
}
canvas->Ellipse(X,Y,X+width,Y+height);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void __fastcall TForm1::PaintMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
int x,y;
x=X-YObject::originx;y=Y-YObject::originy;
if(RBrect->Checked){
YRectangle *p=new YRectangle();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBcircle->Checked){
YCircle *p=new YCircle();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBellipse->Checked){
YEllipse *p=new YEllipse();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBline->Checked)
{
YLine *p=new YLine();
p->X=x;p->Y=y;p->EX=x+10;p->EY=y+10;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBselect->Checked){
myobjs.LeftPressed(x,y);
}
if(RBdelete->Checked){
YObject *p=myobjs.WhoIsIt(x,y);
if(p!=NULL) myobjs.Delete(p);
}
Paint->Invalidate();

}
void __fastcall TForm1::PaintMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
myobjs.MouseMoved(X-YObject::originx,Y-YObject::originy);
if(YObject::needredraw){
Paint->Invalidate();
YObject::needredraw=false;
}
}
void __fastcall TForm1::PaintMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
myobjs.LeftReleased(X-YObject::originx,Y-YObject::originy);
}
void __fastcall TForm1::PaintPaint(TObject *Sender)
{
myobjs.Draw();
}
void __fastcall TForm1::ScrollBar1Change(TObject *Sender)
{
YObject::originx=-ScrollBar1->Position;
Paint->Invalidate();
}
void __fastcall TForm1::ScrollBar2Change(TObject *Sender)
{
YObject::originy=-ScrollBar2->Position;
Paint->Invalidate();
}
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
String path; char * str; int i;
TStringList * T =new TStringList();
TStringList * Temp =new TStringList();
path=ExtractFilePath(Application->ExeName)+&quot;Mysave.tst&quot;;
myobjs.DeleteAll();
T->LoadFromFile(path);
for (i=0;i<T->Count ;i++)
{
str=T->Strings.c_str() ;
if (str[0]=='L'){
YLine *p=new YLine();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->canvas=Paint->Canvas;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->EX=Temp->Strings[3].ToInt();
p->EY=Temp->Strings[4].ToInt();
myobjs.Add(p);
}
if(str[0]=='C'){
YCircle *p=new YCircle();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->canvas=Paint->Canvas;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->radius=Temp->Strings[3].ToInt();
myobjs.Add(p);
}
if(str[0]=='R')
{
YRectangle *p=new YRectangle();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->width=Temp->Strings[3].ToInt();
p->height=Temp->Strings[4].ToInt();
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(str[0]=='E')
{
YEllipse *p=new YEllipse();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->width=Temp->Strings[3].ToInt();
p->height=Temp->Strings[4].ToInt();
p->canvas=Paint->Canvas;
myobjs.Add(p);
}

}
Temp->Free();
T->Free();
myobjs.Draw() ;
ShowMessage(&quot;打开成功!&quot;);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
String str; str=ExtractFilePath(Application->ExeName)+&quot;Mysave.tst&quot;;
myobjs.Save(str); }
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
Close();
}
void __fastcall TForm1::PaintMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
int x,y;
x=X-YObject::originx;y=Y-YObject::originy;
if(RBrect->Checked){
YRectangle *p=new YRectangle();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBcircle->Checked){
YCircle *p=new YCircle();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBellipse->Checked){
YEllipse *p=new YEllipse();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBline->Checked)
{
YLine *p=new YLine();
p->X=x;p->Y=y;p->EX=x+10;p->EY=y+10;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBselect->Checked){
myobjs.LeftPressed(x,y);
}
if(RBdelete->Checked){
YObject *p=myobjs.WhoIsIt(x,y);
if(p!=NULL) myobjs.Delete(p);
}
Paint->Invalidate();

}
void __fastcall TForm1::PaintMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
myobjs.MouseMoved(X-YObject::originx,Y-YObject::originy);
if(YObject::needredraw){
Paint->Invalidate();
YObject::needredraw=false;
}

}
void __fastcall TForm1::PaintMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
myobjs.LeftReleased(X-YObject::originx,Y-YObject::originy);
}
void __fastcall TForm1::PaintPaint(TObject *Sender)
{
myobjs.Draw();
}
void __fastcall TForm1::ScrollBar1Change(TObject *Sender)
{
YObject::originx=-ScrollBar1->Position;
Paint->Invalidate();
}
void __fastcall TForm1::ScrollBar2Change(TObject *Sender)
{
YObject::originy=-ScrollBar2->Position;
Paint->Invalidate();
}
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
String path;
char * str;
int i;
TStringList * T =new TStringList();
TStringList * Temp =new TStringList();

path=ExtractFilePath(Application->ExeName)+&quot;Mysave.tst&quot;;
myobjs.DeleteAll();
T->LoadFromFile(path);

for (i=0;i<T->Count ;i++)
{
str=T->Strings.c_str() ;
if (str[0]=='L'){
YLine *p=new YLine();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->canvas=Paint->Canvas;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->EX=Temp->Strings[3].ToInt();
p->EY=Temp->Strings[4].ToInt();
myobjs.Add(p);
}
if(str[0]=='C'){
YCircle *p=new YCircle();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->canvas=Paint->Canvas;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->radius=Temp->Strings[3].ToInt();
myobjs.Add(p);
}
if(str[0]=='R')
{
YRectangle *p=new YRectangle();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->width=Temp->Strings[3].ToInt();
p->height=Temp->Strings[4].ToInt();
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(str[0]=='E')
{
YEllipse *p=new YEllipse();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->width=Temp->Strings[3].ToInt();
p->height=Temp->Strings[4].ToInt();
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
}
Temp->Free(); T->Free(); myobjs.Draw() ;}
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
String str;
str=ExtractFilePath(Application->ExeName)+&quot;Mysave.tst&quot;;
myobjs.Save(str);
}
 
class YObjects
{
public:
YObject *head,*tail;
YObjects(){head=tail=NULL;}
~YObjects(){DeleteAll();}
void Add(YObject *obj);
void Delete(YObject *obj);
void Delete(int index);
void DeleteAll();
void UnSelectAll();
void SelectAll();
void MoveToBottom(YObject *obj);
void MoveToFront(YObject *obj){};
void Draw();
void Save(String path);
YObject *WhoIsIt(int x,int y);
virtual void LeftPressed(int X,int Y);
virtual void MouseMoved(int X,int Y);
virtual void LeftReleased(int X,int Y);
};

///////////
class YObject
{
protected:
int sx,sy;
public:
static bool needredraw;
static int originx,originy;
int X,Y;
bool selected,pressed;
enum YWHICHBOX which;
TCanvas *canvas;
YObject *next,*prior;
YObject(){X=Y=0;next=prior=NULL;canvas=NULL;selected=false;pressed=false;}
virtual void Draw();
virtual String Save();
virtual void DrawFocus(){};
virtual bool IsMe(int x,int y){return false;}
virtual enum YWHICHBOX InWhichBox(int x,int y);
virtual void MoveTo(int x,int y);
virtual void Move(int dx,int dy);
static bool IsInBox(int x,int y,int left,int top,int width,int height);
virtual void LeftPressed(int X,int Y){};
virtual void MouseMoved(int X,int Y){};
virtual void LeftReleased(int X,int Y){};
};





bool YObject::needredraw=false;
int YObject::originx=0;
int YObject:: originy=0;
void YObject::MoveTo(int x,int y)
{
X=x;Y=y;
}
void YObject::Move(int dx,int dy)
{
X+=dx;Y+=dy;
}

String YObject::Save()
{
return &quot;NULL&quot;;
}
enum YWHICHBOX YObject::InWhichBox(int x,int y)
{
return EXTE;
}
bool YObject::IsInBox(int x,int y,int left,int top,int width,int height)
{
return x>=left &amp;&amp; x<=left+width &amp;&amp; y>=top &amp;&amp; y<=top+height;
}
void YObject::Draw()
{
if(canvas==NULL) return;
if(selected) DrawFocus();
//canvas->TextOutA(X,Y,&quot;obj&quot;);
}
class YLine:public YObject
{
public:
int EX,EY;
YLine(){EX=EY=10;}
void Draw();
String Save();
void DrawFocus();
void Move(int dx,int dy);
void MoveTo(int x,int y);
bool IsMe(int x,int y);
enum YWHICHBOX InWhichBox(int x,int y);
virtual void LeftPressed(int X,int Y);
virtual void MouseMoved(int X,int Y);
virtual void LeftReleased(int X,int Y);
};










String YLine::Save()
{
String str;
str=&quot;L|&quot;+IntToStr(X)+&quot;|&quot;;
str=str+IntToStr(Y)+&quot;|&quot;;
str=str+IntToStr(EX)+&quot;|&quot;;
str=str+IntToStr(EY);
return str;
}


void YLine::Draw()
{
if(canvas==NULL) return;
YObject::Draw();
canvas->MoveTo(X,Y);
canvas->LineTo(EX,EY);
}
void YLine::DrawFocus()
{
int d=RECTLETW,w,h;
if(canvas==NULL) return;
canvas->Rectangle(X-d,Y-d,X+d,Y+d);
w=(EX-X)/2;h=(EY-Y)/2;
canvas->Rectangle(X+w-d,Y+h-d,X+w+d,Y+h+d);
canvas->Rectangle(EX-d,EY-d,EX+d,EY+d);
}
enum YWHICHBOX YLine::InWhichBox(int x,int y)
{
int d=RECTLETW,w,h;
w=(EX-X);h=(EY-Y);
if(IsInBox(x,y,X-d,Y-d,2*d,2*d)) return LT;
if(IsInBox(x,y,X+w/2-d,Y+h/2-d,2*d,2*d)) return MT;
if(IsInBox(x,y,X+w-d,Y+h-d,2*d,2*d)) return RB;
return EXTE;
}
void YLine::LeftPressed(int X,int Y)
{
pressed=true;
sx=X;sy=Y;
if(!selected){
if(IsMe(X,Y)) selected=true;
}
if(selected){
which=InWhichBox(X,Y);
if(which==EXTE) selected=false;
}
}
void YLine::MouseMoved(int x,int y)
{
if(!pressed) return;
if(selected){
switch(which)
{
case LT:
X+=x-sx;Y+=y-sy;
sx=x;sy=y;
needredraw=true;
break;
case MT:
X+=x-sx;Y+=y-sy;
EX+=x-sx;EY+=y-sy;
sx=x;sy=y;
needredraw=true;
break;
case RB:
EX+=x-sx;EY+=y-sy;
sx=x;sy=y;
needredraw=true;
break;
}
}
}
void YLine::LeftReleased(int X,int Y)
{
pressed=false;
}

void YLine::Move(int dx,int dy)
{
X+=dx;Y+=dy;
EX+=dx;EY+=dy;
}
void YLine::MoveTo(int x,int y)
{
int dx,dy;
dx=x-X;dy=y-Y;
Move(dx,dy);
}
bool YLine::IsMe(int x,int y)
{
int d=RECTLETW;
if(x>=X-d &amp;&amp; x<=X+d &amp;&amp; y>=Y-d &amp;&amp; y<=Y+d) return true;
if(x>=EX-d &amp;&amp; x<=EX+d &amp;&amp; y>=EY-d &amp;&amp; y<=EY+d) return true;
return false;
}




class YRectangle:public YObject
{
public:
int width,height;
YRectangle(){width=10;height=10;}
void Draw();
void DrawFocus();
String Save();
enum YWHICHBOX InWhichBox(int x,int y);
bool IsMe(int x,int y){return (x>=X &amp;&amp; x<=X+width &amp;&amp; y>=Y &amp;&amp; y<=Y+height);}
virtual void LeftPressed(int X,int Y);
virtual void MouseMoved(int X,int Y);
virtual void LeftReleased(int X,int Y);

};



class YEllipse:public YRectangle
{
public:
void Draw();
};

void YRectangle::Draw()
{
if(canvas==NULL) return;
YObject::Draw();
canvas->Rectangle(X+originx,Y+originy,X+originx+width,Y+originy+height);
}
String YRectangle::Save()
{
String str;
str=&quot;R|&quot;+IntToStr(X)+&quot;|&quot;;
str=str+IntToStr(Y)+&quot;|&quot;;
str=str+IntToStr(width)+&quot;|&quot;;
str=str+IntToStr(height);
return str;
}

void YRectangle::DrawFocus()
{
int d=RECTLETW;
int w=width,h=height;
int x=X+originx,y=Y+originy;
if(canvas==NULL) return;
canvas->Rectangle(x-d,y-d,x+d,y+d);
canvas->Rectangle(x+w/2-d,y-d,x+w/2+d,y+d);
canvas->Rectangle(x+w-d,y-d,x+w+d,y+d);
canvas->Rectangle(x+w-d,y+h/2-d,x+w+d,y+h/2+d);
canvas->Rectangle(x+w-d,y+h-d,x+w+d,y+h+d);
canvas->Rectangle(x+w/2-d,y+h-d,x+w/2+d,y+h+d);
canvas->Rectangle(x-d,y+h-d,x+d,y+h+d);
canvas->Rectangle(x-d,y+h/2-d,x+d,y+h/2+d);
}
enum YWHICHBOX YRectangle::InWhichBox(int x,int y)
{
int d=RECTLETW;
int w=width,h=height;
if(IsInBox(x,y,X-d,Y-d,2*d,2*d)) return LT;
if(IsInBox(x,y,X+w/2-d,Y-d,2*d,2*d)) return MT;
if(IsInBox(x,y,X+w-d,Y-d,2*d,2*d)) return RT;
if(IsInBox(x,y,X+w-d,Y+h/2-d,2*d,2*d)) return RM;
if(IsInBox(x,y,X+w-d,Y+h-d,2*d,2*d)) return RB;
if(IsInBox(x,y,X+w/2-d,Y+h-d,2*d,2*d)) return MB;
if(IsInBox(x,y,X-d,Y+h-d,2*d,2*d)) return LB;
if(IsInBox(x,y,X-d,Y+h/2-d,2*d,2*d)) return LM;
if(IsInBox(x,y,X,Y,w,h)) return INNER;
return EXTE;
}
void YRectangle::LeftPressed(int X,int Y)
{
pressed=true;
sx=X;sy=Y;
if(!selected){
if(IsMe(X,Y)) selected=true;
}
if(selected){
which=InWhichBox(X,Y);
if(which==EXTE) selected=false;
}
}
void YRectangle::MouseMoved(int x,int y)
{
if(!pressed) return;
if(selected){
int dx,dy;
dx=x-sx;dy=y-sy;
switch(which)
{
case LT:
width-=dx;height-=dy;
X=x;Y=y;
sx=x;sy=y;
needredraw=true;
break;
case MT:
height-=dy;Y=y;
sx=x;sy=y;
needredraw=true;
break;
case RT:
width+=dx;height-=dy;
Y=y;
sx=x;sy=y;
needredraw=true;
break;
case RM:
width+=dx;
sx=x;sy=y;
needredraw=true;
break;
case RB:
width+=dx;height+=dy;
sx=x;sy=y;
needredraw=true;
break;
case MB:
height+=dy;
sx=x;sy=y;
needredraw=true;
break;
case LB:
width-=dx;height+=dy;
X=x;
sx=x;sy=y;
needredraw=true;
break;
case LM:
width-=dx;X=x;
sx=x;sy=y;
needredraw=true;
break;
case INNER:
Move(dx,dy);
sx=x;sy=y;
needredraw=true;
break;
}
}
}
void YRectangle::LeftReleased(int X,int Y)
{
pressed=false;
}

void YEllipse::Draw()
{
if(canvas==NULL) return;
if(selected){
YRectangle::Draw();
}
canvas->Ellipse(X,Y,X+width,Y+height);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void __fastcall TForm1::PaintMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
int x,y;
x=X-YObject::originx;y=Y-YObject::originy;
if(RBrect->Checked){
YRectangle *p=new YRectangle();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBcircle->Checked){
YCircle *p=new YCircle();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBellipse->Checked){
YEllipse *p=new YEllipse();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBline->Checked)
{
YLine *p=new YLine();
p->X=x;p->Y=y;p->EX=x+10;p->EY=y+10;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBselect->Checked){
myobjs.LeftPressed(x,y);
}
if(RBdelete->Checked){
YObject *p=myobjs.WhoIsIt(x,y);
if(p!=NULL) myobjs.Delete(p);
}
Paint->Invalidate();

}
void __fastcall TForm1::PaintMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
myobjs.MouseMoved(X-YObject::originx,Y-YObject::originy);
if(YObject::needredraw){
Paint->Invalidate();
YObject::needredraw=false;
}
}
void __fastcall TForm1::PaintMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
myobjs.LeftReleased(X-YObject::originx,Y-YObject::originy);
}
void __fastcall TForm1::PaintPaint(TObject *Sender)
{
myobjs.Draw();
}
void __fastcall TForm1::ScrollBar1Change(TObject *Sender)
{
YObject::originx=-ScrollBar1->Position;
Paint->Invalidate();
}
void __fastcall TForm1::ScrollBar2Change(TObject *Sender)
{
YObject::originy=-ScrollBar2->Position;
Paint->Invalidate();
}
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
String path; char * str; int i;
TStringList * T =new TStringList();
TStringList * Temp =new TStringList();
path=ExtractFilePath(Application->ExeName)+&quot;Mysave.tst&quot;;
myobjs.DeleteAll();
T->LoadFromFile(path);
for (i=0;i<T->Count ;i++)
{
str=T->Strings.c_str() ;
if (str[0]=='L'){
YLine *p=new YLine();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->canvas=Paint->Canvas;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->EX=Temp->Strings[3].ToInt();
p->EY=Temp->Strings[4].ToInt();
myobjs.Add(p);
}
if(str[0]=='C'){
YCircle *p=new YCircle();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->canvas=Paint->Canvas;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->radius=Temp->Strings[3].ToInt();
myobjs.Add(p);
}
if(str[0]=='R')
{
YRectangle *p=new YRectangle();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->width=Temp->Strings[3].ToInt();
p->height=Temp->Strings[4].ToInt();
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(str[0]=='E')
{
YEllipse *p=new YEllipse();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->width=Temp->Strings[3].ToInt();
p->height=Temp->Strings[4].ToInt();
p->canvas=Paint->Canvas;
myobjs.Add(p);
}

}
Temp->Free();
T->Free();
myobjs.Draw() ;
ShowMessage(&quot;打开成功!&quot;);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
String str; str=ExtractFilePath(Application->ExeName)+&quot;Mysave.tst&quot;;
myobjs.Save(str); }
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
Close();
}
void __fastcall TForm1::PaintMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
int x,y;
x=X-YObject::originx;y=Y-YObject::originy;
if(RBrect->Checked){
YRectangle *p=new YRectangle();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBcircle->Checked){
YCircle *p=new YCircle();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBellipse->Checked){
YEllipse *p=new YEllipse();
p->X=x;p->Y=y;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBline->Checked)
{
YLine *p=new YLine();
p->X=x;p->Y=y;p->EX=x+10;p->EY=y+10;
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(RBselect->Checked){
myobjs.LeftPressed(x,y);
}
if(RBdelete->Checked){
YObject *p=myobjs.WhoIsIt(x,y);
if(p!=NULL) myobjs.Delete(p);
}
Paint->Invalidate();

}
void __fastcall TForm1::PaintMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
myobjs.MouseMoved(X-YObject::originx,Y-YObject::originy);
if(YObject::needredraw){
Paint->Invalidate();
YObject::needredraw=false;
}

}
void __fastcall TForm1::PaintMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
myobjs.LeftReleased(X-YObject::originx,Y-YObject::originy);
}
void __fastcall TForm1::PaintPaint(TObject *Sender)
{
myobjs.Draw();
}
void __fastcall TForm1::ScrollBar1Change(TObject *Sender)
{
YObject::originx=-ScrollBar1->Position;
Paint->Invalidate();
}
void __fastcall TForm1::ScrollBar2Change(TObject *Sender)
{
YObject::originy=-ScrollBar2->Position;
Paint->Invalidate();
}
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
String path;
char * str;
int i;
TStringList * T =new TStringList();
TStringList * Temp =new TStringList();

path=ExtractFilePath(Application->ExeName)+&quot;Mysave.tst&quot;;
myobjs.DeleteAll();
T->LoadFromFile(path);

for (i=0;i<T->Count ;i++)
{
str=T->Strings.c_str() ;
if (str[0]=='L'){
YLine *p=new YLine();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->canvas=Paint->Canvas;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->EX=Temp->Strings[3].ToInt();
p->EY=Temp->Strings[4].ToInt();
myobjs.Add(p);
}
if(str[0]=='C'){
YCircle *p=new YCircle();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->canvas=Paint->Canvas;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->radius=Temp->Strings[3].ToInt();
myobjs.Add(p);
}
if(str[0]=='R')
{
YRectangle *p=new YRectangle();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->width=Temp->Strings[3].ToInt();
p->height=Temp->Strings[4].ToInt();
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
if(str[0]=='E')
{
YEllipse *p=new YEllipse();
Temp->Delimiter ='|';
Temp->DelimitedText =str;
p->X=Temp->Strings[1].ToInt();
p->Y=Temp->Strings[2].ToInt();
p->width=Temp->Strings[3].ToInt();
p->height=Temp->Strings[4].ToInt();
p->canvas=Paint->Canvas;
myobjs.Add(p);
}
}
Temp->Free(); T->Free(); myobjs.Draw() ;}
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
String str;
str=ExtractFilePath(Application->ExeName)+&quot;Mysave.tst&quot;;
myobjs.Save(str);
}
 
给100$我给你转换。
(开个玩笑)
 
帮忙啊 大侠们!
 
后退
顶部