c++builder画正方形和圆形
要求:把畫的图形記成不同的class.按一個 redraw 會依記錄畫出不同形狀.要求要用 virtual function.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
TGroupBox *GroupBox1;
TImage *Image1;
int shape;
int posX,posY,X,Y; //初始及终点坐标
class type
{
public:
void zuotu1()
{
Image1->Canvas->Ellipse(posX,posY,X,Y);
}
virtual void zuotu2()
{
}
};
class cycle:public type
{
public:
void zuotu1()
{
}
void zuotu2()
{
Image1->Canvas->Rectangle(posX,posY,X,Y);
}
};
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
shape=1;
StatusBar1->Panels->Items[0]->Text="绘制正方形";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
shape=2;
StatusBar1->Panels->Items[0]->Text="绘制圆形";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
TMouseButton Button, int X, int Y)
{
posX=X;
posY=Y;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if(abs(X-posX)>abs(Y-posY))
Y=X-posX+posY;
else
X=Y-posY+posX;
type a;
cycle b;
switch(shape)
{
case 1:
a.zuotu1();
break;
case 2:
b.zuotu2();
break;
default:
break;
}
}
//---------------------------------------------------------------------------