快速动画是指每隔一段很小的时间间隔就快速擦去原有画面,并重新画上新的画面的动画技术。快速动画成功的关键就在于擦去和重画的速度必须很快,否则画面就会有闪烁现象。
在vb中制作快速动画比较简单,只要把窗体的autoredraw属性设置为true,再直接调用api函数bitblt,画完一帧refresh一次。但c++builder的窗体没有autoredraw属性,只要一使用bitblt,窗体就会自动刷新,由于一帧画面往往要几次用到bitblt,画面就闪烁起来了。解决的办法就是自己定义一个不可见的缓冲区,其大小应与目标窗体相同,先在缓冲区上把一帧的画面画完,再用一次bitblt函数把缓冲区的图案贴到窗体上。请看例子:
//在tform1 *form1;的后面添上这三句 graphics::tbitmap *p; graphics::tbitmap *q; int xx=0; //--------------------- void __fastcall tform1::formcreate(tobject *sender) { p=new graphics::tbitmap;//这存放的就是要贴到窗体上的小图案 p->handle=loadbitmap(hinstance,″aaa″);//从资源文件中载入小图案 q=new graphics::tbitmap;//定义缓冲区q q->width=width;//使缓冲区的大小与窗体相同 q->height=height; patblt(q->canvas->handle,0,0,q->width,q->height,0);//把缓冲区的背景变为黑色 } //-------------------- void __fastcall tform1::formdestroy(tobject *sender) { //程序结束时释放内存 delete p; delete q; } //-------------------- void __fastcall tform1::timer1timer(tobject *sender) { //窗体上要加上一个timer控件 xx+=2; patblt(q->canvas->handle,0,0,width,height,0);//把缓冲区的背景变为黑色,同时擦去了旧的画面 bitblt(q->canvas->handle,xx,0,p->width,p->height,p->canvas->handle,0,0,srccopy); //正在缓冲区上制作一帧的画面,这几句是不可见的 bitblt(q->canvas->handle,xx,50,p->width,p->height,p->canvas->handle,0,0,srccopy); bitblt(q->canvas->handle,xx,100,p->width,p->height,p->canvas->handle,0,0,srccopy); bitblt(q->canvas->handle,xx,150,p->width,p->height,p->canvas->handle,0,0,srccopy); bitblt(q->canvas->handle,xx,200,p->width,p->height,p->canvas->handle,0,0,srccopy); bitblt(q->canvas->handle,xx,250,p->width,p->height,p->canvas->handle,0,0,srccopy); bitblt(q->canvas->handle,xx,300,p->width,p->height,p->canvas->handle,0,0,srccopy); bitblt(canvas->handle,0,0,width,height,q->canvas->handle,0,0,srccopy); //把缓冲区的画面贴到窗体上 } |