读书人

自建了一个画线的组件现在出现内存有

发布时间: 2012-02-05 12:07:15 作者: rapoo

自建了一个画线的组件,现在出现内存问题请制做过组件的大侠们指点
建了一个画线的组件,基本调试通过,源码:
.H文件
class PACKAGE TLineBase : public TWinControl
{
private:
TPoint StartPoint; //线在工作区的起始点
TPoint EndPoint; //线在工作区的终止点
protected:
public:
String Source; //起始图形名子
String Dest; //终止图形名子
TStringList *Corner;//拐点列表
bool IsDashed;//虚线实现标志
//Graphics::TBitmap *tempBit,*tempBit1; //绘图区
//TCanvas *LineCancas; //绘图区
void __fastcall SetStartPoint(TPoint point);
void __fastcall SetEndPoint(TPoint point);
void __fastcall PointToRect();//将起始点终止点转换为线图形组件的工作区
void __fastcall DrawGraphics();
void __fastcall AddCorner(int x,int y); //添加拐点坐标
void __fastcall DelCorner(int x,int y); //删除拐点坐标
void __fastcall DrawDashed(); //画虚线
void __fastcall Paint(void); //绘线
//void __fastcall DrawGraphics();
void __fastcall DrawRealLine();//画实线
void __fastcall PointToPoint(int x,int y);
__fastcall TLineBase(TComponent* Owner);
__fastcall ~TLineBase();

__published:
__property PopupMenu;
__property Color;
__property Font;
__property Cursor;
__property Parent;
__property Name;
__property Brush;
__property ParentFont;
__property OnClick;
__property OnDblClick;
__property OnMouseDown;
__property OnMouseMove;
__property OnMouseUp;
__property OnDragDrop;
__property OnDragOver;
__property OnEndDock;
__property OnEndDrag;
__property OnStartDock;


__property OnStartDrag;
__property OnKeyUp;
__property OnKeyPress;
__property OnKeyDown;
__property OnResize;
};


.cpp文件


static inline void ValidCtrCheck(TLineBase *)
{
new TLineBase(NULL);
}
//---------------------------------------
__fastcall TLineBase::TLineBase(TComponent* Owner)
: TWinControl(Owner)
{
//Corner=new TStringList();
//LineCancas=new TCanvas();

}
//---------------------------------------
__fastcall TLineBase::~TLineBase()

{

//Corner-> Free();
// LineCancas-> Free();
}
//---------------------------------------

namespace Linebase
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TLineBase)};
RegisterComponents( "Samples ", classes, 0);
}
}
//---------------------------------------
void __fastcall TLineBase::AddCorner(int x,int y)
{
//Corner-> Add(IntToStr(x)+ ', '+IntToStr(y));//将拐点座标添加到座标列表中
}
//---------------------------------------
void __fastcall TLineBase::DelCorner(int x,int y)
{

//Corner-> Delete(Corner-> IndexOf(IntToStr(x)+ ', '+IntToStr(y))); //从座标列表中删除拐点座标
}
//---------------------------------------
void __fastcall TLineBase::DrawDashed() //随着鼠标移动画出的线,是直线。
{
HRGN WndRgn,TempRgn,tepRgn;
POINT *LinePoint;
/*TBrush *brush;
brush=new TBrush;
brush-> Color=clRed; */

/*
初始化绘线区域的四个点,算法为起始点的横纵座标减3加3,终止点的横纵座标减3加3.
*/
LinePoint=(POINT *)malloc(4*2*(sizeof(POINT))); //创建点对象分配内存
if (((StartPoint.x> EndPoint.x)&&(StartPoint.y <EndPoint.y))||((StartPoint.x <EndPoint.x)&&(StartPoint.y> EndPoint.y))) //如果起点座标不是全大于或全小于终点座标则将四个点定义为画二三象限的线
{
LinePoint[0].x=1;
LinePoint[0].y=this-> Height;
LinePoint[1].x=0;
LinePoint[1].y=this-> Height-1;


LinePoint[2].x=this-> Width-1;
LinePoint[2].y=0;
LinePoint[3].x=this-> Width;
LinePoint[3].y=1;

}
else//如果起点座标全大于或全小于终点座标则将四个点定义为画一四象限线
{
LinePoint[0].x=1;
LinePoint[0].y=0;
LinePoint[1].x=0;
LinePoint[1].y=1;
LinePoint[2].x=this-> Width-1;
LinePoint[2].y=this-> Height;
LinePoint[3].x=this-> Width;
LinePoint[3].y=this-> Height-1;

}
/*
画没有拐点的线
*/



WndRgn=CreateRectRgn(0,0,this-> Width,this-> Height);
TempRgn=CreatePolygonRgn(LinePoint,4,ALTERNATE);
CombineRgn(WndRgn,WndRgn,TempRgn,RGN_AND);
//FillRgn(WndRgn,TempRgn,brush);
DeleteObject(TempRgn);
free(LinePoint);
SetWindowRgn(Handle,WndRgn,true);
SetWindowPos(Handle,HWND_TOP,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
DeleteObject(WndRgn);




}
//---------------------------------------
void __fastcall TLineBase::Paint(void)
{
DrawDashed();
//DrawRealLine();
}

//---------------------------------------
void __fastcall TLineBase::DrawRealLine()
{
HRGN WndRgn,TempRgn,tepRgn;
POINT *LinePoint;
LinePoint=(POINT *)malloc(Corner-> Count*2*(sizeof(POINT)));//线是画在由若干个点组成的多边形里面的
/*
将点X座标从拐点列表中取出存在点集中
*/
for(int i=0;i <Corner-> Count;i++)
{
LinePoint[i].x=StrToInt(Corner-> Strings[i].SubString(1,Corner-> Strings[i].Pos( ', ')-1));
LinePoint[i].y=StrToInt(Corner-> Strings[i].SubString(Corner-> Strings[i].Pos( ', ')+1,Corner-> Strings[i].Length()));
}
/*
通过多边形组成一条线
*/
WndRgn=CreateRectRgn(0,0,this-> Width,this-> Height);
TempRgn=CreatePolygonRgn(LinePoint,Corner-> Count,ALTERNATE);
CombineRgn(WndRgn,WndRgn,TempRgn,RGN_AND);
DeleteObject(TempRgn);
free(LinePoint);
SetWindowRgn(Handle,WndRgn,true);
SetWindowPos(Handle,HWND_TOP,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);

}

//---------------------------------------
void __fastcall TLineBase::DrawGraphics()
{

if (IsDashed)
{
DrawDashed();
}
else
{
DrawRealLine();
}
}

//---------------------------------------
void __fastcall TLineBase::PointToRect()
{
if (StartPoint.x> EndPoint.x)
{
Left=EndPoint.x;
Width=StartPoint.x-EndPoint.x;
}
else
{
Left=StartPoint.x;
Width=EndPoint.x-StartPoint.x;
}

if (StartPoint.y> EndPoint.y)
{
Top=EndPoint.y;
Height=StartPoint.y-EndPoint.y;
}
else
{
Top=StartPoint.y;
Height=EndPoint.y-StartPoint.y;
}


}
//---------------------------------------
void __fastcall TLineBase::PointToPoint(int x,int y)
{
// AddCorner(x-1,y);
//AddCorner(x+1,y);

}
//---------------------------------------
void __fastcall TLineBase::SetEndPoint(TPoint point)


{
EndPoint.x=point.x;
EndPoint.y=point.y;

}

void __fastcall TLineBase::SetStartPoint(TPoint point)
{
StartPoint.x=point.x;
StartPoint.y=point.y;
}


问题,当创件一个对像
TLineBase *line
line= new TLineBase(this);
line-> Name= "a "+IntToStr(cc);
line-> Parent=this;
line-> Color=clBlack;
line-> PopupMenu=PopupMenu1;
line-> OnClick=aaClinck;


在该对像操作完,进行释放时
line-> free()
提示内存错误,请指点谢谢,分不够可以再给。只要解决问题就行。


[解决办法]
删除自己肯定是可以的,vcl都可以直接这样做
内存出出错详细信息是什么?
开个cg查看一下,看看有没有用,并单步跟踪看到了那里出错.
这样问题应该比较容易找出来的.
或者fastmm也可以,有的会直接说明那一行,而且有调用过程的跟踪.
[解决办法]
mark
[解决办法]
mark
[解决办法]
另外建议楼主改下代码.如下:
void __fastcall TForm1::aaClinck(TObject *Sender)
{
ShowMessage( "delete '); //看看aaClinck都是什么时候被调用了
if (draw== "del ")
{
if Assigned(Sender)
// 或者 if(Sender!=NULL)
delete ((TLineBase *)Sender);

}
}

读书人网 >C++ Builder

热点推荐