类型:转载 责任编辑:asp 日期:2007/03/01
我想制定一个显示图像的组件,有边框的显示缩略图的组件,还要能够将这个组件数组创建,像timage *image[100]那样,我使用了tpanel.timage,tbevel组件,继承的是tcustomcontrol类作为父类,代码我写好了,但是后续工作不知道怎么做,请教各位高手,帮帮忙,在看看这样写对不对,能不能达到效果
附代码
displayimage.h:
#ifndef displayimageh
#define displayimageh
//---------------------------------------------------------------------------
#include <sysutils.hpp>
#include <classes.hpp>
#include <controls.hpp>
#include "jpeg.hpp"
//---------------------------------------------------------------------------
class package tdisplayimage : public tcustomcontrol
{
private:
timage *image;
tpanel *panel;
tbevel *bevel;
tjpegimage *jpegimage;
int simagewidth;
int simageheight;
int spanelwidth;
int spanelheight;
ansistring sfilename;
protected:
virtual void __fastcall createwnd();
void __fastcall setimagewidth(int imagewidth);
void __fastcall setimageheight(int imageheight);
void __fastcall setpanelwidth(int panelwidth);
void __fastcall setpanelheight(int panelheight);
void __fastcall setfilename(ansistring filename);
trect set_size(int ownerwidth,int ownerheight,int jpegimagewidth,int jpegimageheight);
public:
__fastcall tdisplayimage(tcomponent* owner);
__fastcall ~tdisplayimage() ;
virtual void __fastcall paint(void);
__published:
__property int panelwidth={read=spanelwidth,write=setpanelwidth,default=250};
__property int panelheight={read=spanelheight,write=setpanelheight,default=200};
__property ansistring filename={read=sfilename,write=setfilename,default=null};
__property int imagewidth={read=simagewidth,write=setimagewidth,default=120};
__property int imageheight={read=simageheight,write=setimageheight,default=170};
__property align;
//__property alignment;
__property anchors;
__property bidimode;
__property caption;
__property color;
__property constraints;
__property dragcursor;
__property dragkind;
__property dragmode;
__property enabled;
// __property focuscontrol;
__property font;
__property parentbidimode;
__property parentfont;
__property parentcolor;
__property parentshowhint;
__property popupmenu;
__property showhint;
// __property boundsrect;
__property visible;
// __property style;
__property onclick;
__property ondblclick;
__property ondragdrop;
__property ondragover;
__property onenddock;
__property onenddrag;
__property onmousedown;
__property onmousemove;
__property onmouseup;
__property onstartdock;
__property onstartdrag;
};
//---------------------------------------------------------------------------
#endif
displayimage.cpp:
#include <vcl.h>
#pragma hdrstop
#include "displayimage.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// validctrcheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void validctrcheck(tdisplayimage *)
{
new tdisplayimage(null);
}
//---------------------------------------------------------------------------
__fastcall tdisplayimage::tdisplayimage(tcomponent* owner)
: tcustomcontrol(owner)
{
image=new timage(this);
panel=new tpanel(this);
bevel=new tbevel(this);
jpegimage=new tjpegimage();
image->parent=panel;
bevel->parent=panel;
spanelwidth=250;
spanelheight=200;
simagewidth=170;
simageheight=120;
sfilename=null;
jpegimage=null;
panel->bevelinner=bvlowered;
panel->bevelouter=bvraised;
panel->bevelwidth=2;
}
trect set_size(int ownerwidth,int ownerheight,int jpegimagewidth,int jpegimageheight);
{
trect result;
int high,width;
int x1,y1,x2,y2;
if (((float)ownerwidth/ownerheight)>((float)jpegwidth/jpegheight)){
high = ownerheight;
width = (double)high*(double)jpegwidth/(double)jpegheight;
}
else{
width = ownerwidth;
high = (double)width*(double)jpegheight/(double)jpegwidth;
}
x1 = (ownerwidth-width)/2;
y1 = (ownerheight-high)/2;
x2 = x1 + width;
y2 = y1 +high;
result.left = x1;
result. = y1;
result.right = x2;
result.bottom = y2;
return result;
}
void __fastcall tdisplayimage::setimagewidth(int imagewidth)
{
image->width=simagewidth=imagewidth;
}
void __fastcall tdisplayimage::setimageheight(int imageheight)
{
image->height=simageheight=imageheight;
}
void __fastcall tdisplayimage::setpanelwidth(int panelwidth)
{
panel->width=spanelwidth=panelwidth;
}
void __fastcall tdisplayimage::setpanelheight(int panelheight)
{
panel->height=spanelheight=panelheight;
}
void __fastcall tdisplayimage::setfilename(ansistring filename)
{
sfilename=filename;
jpegimage->loadfromfile(sfilename);
}
void __fastcall tdisplayimage::paint()
{
if(jpegimage->width<image->width &jpegimage->height<image->height)
{
image->width=jpegimage->width;
bevel->width=image->width+2;
image->height=jpegimage->height;
bevel->height=image->height+2;
bevel1->=image->-1;
bevel1->left=image->left-1;
image->canvas->draw(0,0,jpegimage) ;
}
else
{
trect rect=set_size(image->width,image->height,jpegimage->width,jpegimage->height);
image->width=rect.right-rect.left;
image->height=rect.bottom-rect.;
bevel1->width=image->width+2;
bevel1->height=image->height+2;
image->left=(bevel->width-image->width)/2;
image->=(bevel->height-image->height)/2;
bevel1->=image->-1;
bevel1->left=image->left-1;
image->canvas->stretchdraw(trect(0,0,image->width,image->height),jpegimage);
}
}
void __fastcall ~tdiaplayimage();
{
delete image;
delete panel;
delete bevel;
delete jpegimage;
}
}
//---------------------------------------------------------------------------
namespace displayimage
{
void __fastcall package register()
{
tcomponentclass classes[1] = {__classid(tdisplayimage)};
registercomponents("common controls", classes, 0);
}
}
//---------------------------------------------------------------------------
推荐阅读
写好了就可以编译了啊。
然后install
这样就可以在控件面板上看到你自己写的控件了。
先不要安装,动态的在form上生成控件并进行测试,确信测试基本通过在安装到控件板上。
给你个建议了,不要用timage image[100]这样的固定数组,用tlist容器了,用多少创建多少,然后用timage * 来转换,这样比较好,还可以用tlist的函数来管理
tlist用法实例:
void __fastcall tform_infomodify::formcreate(tobject *sender)
{
lslabellist = new tlist();
lsquerylist = new tlist();
lsdatafieldlist = new tlist();
lsrunneredit = new tlist();
slfieldname = new tstringlist();
slcodefield = new tstringlist();
slcodefieldcode = new tstringlist();
sldatatype = new tstringlist();
slfieldlabel = new tstringlist() ;
if (qb_list)
{
ds_list->dataset = qb_list;
qb_list->afterscroll = qb_listafterscroll;
// qb_list->filtered = false;
// qb_list->filtered = true;
}
tb_tabdef2->active = true;
tb_flddef2->active = true;
tb_tabdef2->filtered = false;
tb_tabdef2->filtered = true;
string sss = "select count(*) from flddef2,tabdef2 where flddef2.fld_type = 相片型 and tabdef2.tab_type = " + sinfotype + " and tabdef2.tab_id = flddef2.tab_id ";
qb_image->active = false;
qb_image->sql->clear();
qb_image->sql->add(sss);
qb_image->active = true;
int i = qb_image->fields->fields[0]->asinteger ;
sss = "select * from flddef2,tabdef2 where flddef2.fld_type = 相片型 and tabdef2.tab_type = " + sinfotype + " and tabdef2.tab_id = flddef2.tab_id ";
qb_image->active = false;
qb_image->sql->clear();
qb_image->sql->add(sss);
qb_image->active = true;
string simagetable;
string simagefield;
if ( i > 0 && qb_list) // > 0 &&
{
simagetable = qb_image->fieldbyname("tab_id")->asstring;
simagefield = qb_image->fieldbyname("fld_id")->asstring;
tb_image->tablename = simagetable;
tb_image->active = true;
dxdbimage1->datafield = simagefield;
tb_image->mastersource = ds_list;
tb_image->masterfields = "pid";
}
sss = "select count(*) from flddef2,tabdef2 where flddef2.fld_type = 多媒体型 and tabdef2.tab_type = " + sinfotype + " and tabdef2.tab_id = flddef2.tab_id ";
qb_mpeg->active = false;
qb_mpeg->sql->clear();
qb_mpeg->sql->add(sss);
qb_mpeg->active = true;
i = qb_mpeg->fields->fields[0]->asinteger ;
sss = "select * from flddef2,tabdef2 where flddef2.fld_type = 多媒体型 and tabdef2.tab_type = " + sinfotype + " and tabdef2.tab_id = flddef2.tab_id ";
qb_mpeg->active = false;
qb_mpeg->sql->clear();
qb_mpeg->sql->add(sss);
qb_mpeg->active = true;
string smpegtable;
string smpegfield;
if ( i > 0 && qb_list) // > 0 &&
{
smpegtable = qb_mpeg->fieldbyname("tab_id")->asstring;
smpegfield = qb_mpeg->fieldbyname("fld_id")->asstring;
tb_mpeg->tablename = smpegtable;
smpegfield = smpegfield;
tb_mpeg->active = true;
tb_mpeg->mastersource = ds_list;
tb_mpeg->masterfields = "pid";
}
}
在菜单components->install components,选install to new package,在unit file里选定你的.cpp文件,在package file里输入包的路径和文件名,点[ok],会弹出提示,自动编译、安装你的控件;完成后,仍会有相应提示。若已经成功安装,在组建面板上会多一个“common controls”页,你做的控件就在里面,并显示为默认图标。
然后你就可以新建一个工程,把组件拖放到form上以测试效果了