13,826
社区成员
发帖
与我相关
我的任务
分享
Graphics::TBitmap *bmp=NULL;
Graphics::TBitmap *bmpBk=NULL;
POINT pt[1000]; //最多1000点
int pIndex=0; //当前点
char *bmpName="z:\\aa.bmp";
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if(Button==mbRight)//右键,从重开始
{
//重新载入bmp图像
if(bmp!=NULL)
bmpBk->LoadFromFile(bmpName);
bmp->Canvas->Draw(0,0,bmpBk);
Canvas->Draw(0,0,bmp);
pIndex=0;
return;
}
if(bmp==NULL)
{
if(!FileExists(bmpName))
{
ShowMessage("确认图像名");
return;
}
//生成并载入bmp图像
bmp=new Graphics::TBitmap();
bmpBk=new Graphics::TBitmap();
bmp->LoadFromFile(bmpName);
bmpBk->Assign(bmp);
}
if(pIndex==0)
bmpBk->Canvas->MoveTo(X,Y);
//录录点
pt[pIndex].x=X;
pt[pIndex].y=Y;
//画线
if(pIndex>0)
bmpBk->Canvas->LineTo(pt[pIndex].x,pt[pIndex].y);
pIndex++;
if(pIndex>2) //判断是否闭合
{
if(abs(pt[0].x-pt[pIndex-1].x)<4 && abs(pt[0].y-pt[pIndex-1].y)<4)
{
pt[pIndex-1].x=pt[0].x;
pt[pIndex-1].y=pt[0].y;
//闭合,计算面积
double s=0;
for(int i=0;i<pIndex-1;i++)
s+=pt[i+1].x*pt[i].y-pt[i+1].y*pt[i].x;
s/=2.0;
Caption=s; //计算完成,显示面积
Beep(1000,100);
pIndex=0;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(pIndex==0 || bmp==NULL)
return;
bmp->Canvas->Draw(0,0,bmpBk);
bmp->Canvas->MoveTo(pt[pIndex-1].x,pt[pIndex-1].y);
bmp->Canvas->LineTo(X,Y);
Canvas->Draw(0,0,bmp);
}
//---------------------------------------------------------------------------