文/黃建志
在C++ Builder中,實現一幅圖像的顯示非常簡單,只要在Form窗體中定義一個TImage組件,設置其Picture屬性,然后選擇任何有效的.ico、.bmp、.emf或.wwf文件,加載進來,所選文件就顯示在TImage組件中。但這只是直接將圖形顯示在窗體中,毫無技巧可言,給人感覺是一種枯燥乏味。為了使圖形顯示有別具一格的效果。按下列步驟實現:
1、 定義一個TImage組件,把要顯示的圖形先加載到TImage組件中,也就是說,把圖 形內容從磁盤載入內存中,作為圖形緩存。
2、 創建一新的位圖對象,其尺寸跟TImage組件中的圖形一樣。
3、 利用畫布(Canvas)的CopyRect功能(將一個畫布的矩形區域拷貝到另一個畫布的矩形區域),以達到動態顯示位圖。
下面介紹各種圖形顯示技巧的具體實現方法。
上拉效果
圖1
實現原理:首先將暫存圖形的第一條水平線,搬移至要顯示位圖的最后一條,接著再將暫存圖形的前兩條水平線,依次搬移至要顯示位圖的最后兩條水平線,然后搬移前三條、前四條直到全部圖形數據搬完為止。在搬移的過程中即可看到顯示的位圖由下而上浮起,而達到上拉的效果(如圖1)。
程序算法:
void _fastcall TFor-
m1::Button1Click(TObject *Sender)
{int i,width,height;
Graphics::TBitmap *newbmp;
newbmp=new Graphics::TBitmap;
newbmp-〉Width=Image1-〉Width;
newbmp-〉Height=Image1-〉Height;
width=Image1-〉Width;
height=Image1-〉Height;
for(i=0;i
{newbmp-〉Canvas-〉CopyRect(Rect(0,height-i,width,height),
Image1-〉Canvas,Rect(0,0,width,i));
Form1-〉Canvas-〉Draw(10,10,newbmp);}
delete newbmp;}
從左向右平鋪顯示效果
圖2
實現原理:首先將暫存圖形的最后一條豎線,搬移至要顯示位圖的第一條豎線,接著再將暫存圖形的最后兩條豎線,依序搬移至要顯示位圖的前兩條豎線,然后搬移最后三條、四條豎線直到全部圖形數據搬完為止。在搬移的過程中即可看到顯示的位圖由左向右浮起,而達到從左向右平鋪顯示的效果(如圖2)。
程序算法:
void _fastcall TForm1::Button2Click(TObject *Sender)
{int i,width,height;
Graphics::TBitmap *newbmp;
newbmp=new Graphics::TBitmap;
newbmp-〉Width=Image1-〉Width;
newbmp-〉Height=Image1-〉Height;
width=Image1-〉Width;
height=Image1-〉Height;
for(i=0;i<=width;i++)
{ newbmp-〉Canvas-〉
CopyRect(Rect(0,0,i,height),
Image1-〉Canvas,Rect(width-i,0,width,height));
Form1-〉Canvas-〉Draw(10,10,newbmp); }
delete newbmp;}
垂直交錯效果
圖3
實現原理:將要顯示的圖形拆成兩部分,奇數條掃描線由上往下搬移,偶數條掃描線則由下往上搬移,而且兩者同時進行。便可看到分別由上下兩端出現的較淡圖形向屏幕中央移動,直到完全清楚為止(如圖3)。
程序算法:
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{Graphics::TBitmap *newbmp;
int i,j,height,width;
newbmp=new Graphics::TBitmap;
newbmp-〉Width=Image1-〉Width;
newbmp-〉Height=Image1-〉Height;
height=Image1-〉Height;
width=Image1-〉Width;
i=0;
while(i<=height)
{for(j=0;j
{newbmp-〉Canvas-〉CopyRect(Rect(j*2,0,j*2+1,i),Image1-〉Canvas,
Rect(j*2,0,j*2+1,i));
newbmp-〉Canvas-〉CopyRect(Rect(j*2+1,height,j*2+2,height-i),
Image1-〉Canvas, Rect(j*2+1,height,j*2+2,height-i)); }
Form1-〉Canvas-〉Draw(10,10,newbmp);
i+=2; }
delete newbmp;}
水平交錯效果
圖4
實現原理:同垂直交錯效果原理一樣,將要顯示的圖形拆成兩部分,奇數條掃描線由左往右搬移,偶數條掃描線則由右往左搬移,兩者同時進行。從屏幕上便可看到分別由左右兩端出現的較淡圖形向屏幕中央移動,直到完全清楚為止(如圖4)。
程序算法:
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{int i,j,height,width;
Graphics::TBitmap *newbmp;
newbmp=new Graphics::TBitmap;
newbmp-〉Width=Image1-〉Width;
newbmp-〉Height=Image1-〉Height;
height=Image1-〉Height;
width=Image1-〉Width;
i=0;
while(i<=height)
{for(j=0;j
{newbmp-〉Canvas-〉CopyRect(Rect(0,j*10,i,j*10+5),Image1-〉Canvas,
Rect(0,j*10,i,j*10+5));
newbmp-〉Canvas-〉CopyRect(Rect(width-i,j*10+5,width,j*10+10),
Image1-〉Canvas, Rect(width-i,j*10+5,width,j*10+10)); }
Form1-〉Canvas-〉Draw(10,10,newbmp);
i+=2; }
delete newbmp;}
從左到右圓筒滾動效果
圖5
實現原理:圖形復制過程中,把目標圖形的坐標按照曲線方式移動,以達到圓筒滾動效果(如圖5)。
程序算法:
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{int i,j,height,width;
Graphics::TBitmap *newbmp;
newbmp=new Graphics::TBitmap;
newbmp-〉Width=Image1-〉Width;
newbmp-〉Height=Image1-〉Height;
height=Image1-〉Height;
width=Image1-〉Width;
i=0;
int intr=50;
for(i=0;i<=width;i+=5)
{for(j=1;j<=2*intr;j++)
{newbmp-〉Canvas-〉CopyRect(Rect(i+j,-sqrt(2*intr*j-j*j),i+j+1,-sqrt(2*intr*j-j*j)+height),Image1-〉Canvas,Rect(i+j,0,i+j+1,height));}
newbmp-〉Canvas-〉CopyRect(Rect(i,0,i+5,height),Image1-〉Canvas,Rect(i,0,i+5,height));
Form1-〉Canvas-〉Draw(10,10,newbmp);
Sleep(10); }}
所有程序算法都在C++ Builder 4.0和5.0調試通過。
|