發軟件時經常需要加入各種圖形的特效顯示效果,這樣可以使畫面變得更為生動活潑,增加軟件的趣味性,使軟件更加受歡迎。本文將探討如何在Delphi編程中實現移動、交錯、瀑布狀、百葉窗和積木堆疊等各種圖形特效顯示效果。
基本原理 在Delphi中,實現圖像的顯示是非常簡單的,我們只要在Form中定義一個TImage組件,設置其picture屬性,然后選擇任何有效的.ICO、.BMP、.EMF或.WMF文件,進行載入,所選文件就會顯示在TImage組件中。但這只是直接將圖形顯示在窗體中,毫無技巧可言。為了使圖形顯示具有特殊效果,我們可以按下列步驟實現:
1.定義一個TImage組件,把要顯示的圖形先裝入到TImage組件中,作為圖形緩存;
2.創建一新的位圖對象,其尺寸跟TImage組件中的圖形一樣;
3.利用畫布(Canvas)的CopyRect功能(將一個畫布的矩形區域拷貝到另一個畫布的矩形區域),使用技巧,動態形成位圖文件內容,然后在窗體中顯示位圖。
實現方法 首先在窗體上定義一個Image控件Image1,載入一幅圖像(注意將其AutoSize設為True,Visible設為False),再定義6個按鈕控件,分別設置Caption為“推拉”、“垂直交錯”、“水平交錯”、“瀑布”、“百葉窗”、“積木”,圖形特效的編程原理和按鈕的Click程序分別如下。
1.推拉效果
將要顯示的圖形由上、下、左、右方向拉進屏幕內顯示,同時將屏幕上原來的舊圖覆蓋掉,此種效果可分為四種:上拉、下拉、左拉和右拉,但原理都差不多,筆者程序以上拉效果為例。
>原理:
首先將放在緩存中圖形的第一條水平線,搬移至要顯示的位圖的最后一條,接著再將緩存中圖形的前兩條水平線,按順序搬移至要顯示位圖的最后兩條水平線,然后搬移前三條、前四條……直到全部圖形數據搬完為止。在搬移的過程中即可看到顯示的位圖由下而上浮起,而達到上拉的效果。
>程序算法如下:
procedure TForm1.Button1Click(Sender: TObject); var newbmp: TBitmap; i,bmpheight,bmpwidth:integer; begin newbmp:= TBitmap.Create; newbmp.Width:=image1.Width; newbmp.Height:=image1.Height; bmpheight:=image1.Height; bmpwidth:=image1.Width; for i:=0 to bmpheight do begin newbmp.Canvas.CopyRect(Rect(0,bmpheight-i,bmpwidth,bmpheight),image1.Canvas,Rect(0,0,bmpwidth,i)); form1.Canvas.Draw(120,100,newbmp); end; newbmp.free; end;
2.垂直交錯效果
>原理:
將要顯示的圖形分成兩部分,奇數條掃描線由上往下搬移,偶數條掃描線的部分則由下往上搬移,而且兩者同時進行。從屏幕上便可看到分別由上下兩端出現的較淡圖形向屏幕中央移動,直到完全清楚為止。
>程序算法如下:
procedure TForm1.Button4Click(Sender: TObject); var newbmp:TBitmap; i,j,bmpheight,bmpwidth:integer; begin newbmp:= TBitmap.Create; newbmp.Width:=image1.Width; newbmp.Height:=image1.Height; bmpheight:=image1.Height; bmpwidth:=image1.Width; i:=0; while i<=bmpheight do begin j:=i; while j >0 do begin newbmp.Canvas.CopyRect(Rect(0,j-1,bmpwidth,j),image1.Canvas,Rect(0,bmpheight-i+j-1,bmpwidth,bmpheight-i+j)); newbmp.Canvas.CopyRect(Rect(0,bmpheight-j,bmpwidth,bmpheight-j+1), image1.Canvas,Rect(0,i-j,bmpwidth,i-j+1)); j:=j-1; end; form1.Canvas.Draw(120,100,newbmp); i:=i+1; end; newbmp.free; end;
3.水平交錯效果
>原理:
同垂直交錯效果原理一樣,只是將分成兩組后的圖形分別由左右兩端移進屏幕。
>程序算法如下:
procedure TForm1.Button5Click(Sender: TObject); var newbmp:TBitmap; i,j,bmpheight,bmpwidth:integer; begin newbmp:= TBitmap.Create; newbmp.Width:=image1.Width; newbmp.Height:=image1.Height; bmpheight:=image1.Height; bmpwidth:=image1.Width; i:=0; while i<=bmpwidth do begin j:=i; while j>0 do begin newbmp.Canvas.CopyRect(Rect(j-1,0,j,bmpheight), image1.Canvas, Rect(bmpwidth-i+j-1,0,bmpwidth-i+j,bmpheight)); newbmp.Canvas.CopyRect(Rect(bmpwidth-j,0,bmpwidth-j+1,bmpheight), image1.Canvas, Rect(i-j,0,i-j+1,bmpheight)); j:=j-2; end; form1.Canvas.Draw(120,100,newbmp); i:=i+2; end; newbmp.free; end;
4.瀑布效果
>原理:
將緩存中圖形的最后一條掃描線,按順序搬移到可視位圖的第一條到最后一條掃描線,讓此條掃描線在屏幕上留下它的軌跡。接著再把緩存圖形的倒數第二條掃描線,依序搬移到可視位圖的第一條到倒數第二條掃描線。其余的掃描線依此類推。
>程序算法如下:
procedure TForm1.Button3Click(Sender: TObject); var newbmp:TBitmap; i,j,bmpheight,bmpwidth:integer; begin newbmp:= TBitmap.Create; newbmp.Width:=image1.Width; newbmp.Height:=image1.Height; bmpheight:=image1.Height; bmpwidth:=image1.Width; for i:=bmpheight downto 1 do for j:=1 to i do begin newbmp.Canvas.CopyRect(Rect(0,j-1,bmpwidth,j),image1.Canvas, Rect(0,i-1,bmpwidth,i)); form1.Canvas.Draw(120,100,newbmp); end; newbmp.free; end;
5.百葉窗效果
>原理:
將放在緩存中圖形的數據分成若干組,然后依次從第一組到最后一組搬移,第一次每組各搬移第一條掃描線到可視位圖的相應位置,第二次搬移第二條掃描線,接著搬移第三條、第四條掃描線。
>程序算法如下:
procedure TForm1.Button6Click(Sender: TObject); var newbmp:TBitmap; i,j,bmpheight,bmpwidth:integer; xgroup,xcount:integer; begin newbmp:= TBitmap.Create; newbmp.Width:=image1.Width; newbmp.Height:=image1.Height; bmpheight:=image1.Height; bmpwidth:=image1.Width; xgroup:=16; xcount:=bmpheight div xgroup; for i:=0 to xcount do for j:=0 to xgroup do begin newbmp.Canvas.CopyRect(Rect(0,xcountj+i-1,bmpwidth,xcountj+i), image1.Canvas, Rect(0,xcountj+i-1,bmpwidth,xcountj+i)); form1.Canvas.Draw(120,100,newbmp); end; newbmp.Free; end;
6.積木效果
>原理:
是瀑布效果的一種變化,不同之處在于,積木效果每次搬移的是一塊圖形(組),而不只是一根掃描線。
>程序算法如下:
procedure TForm1.Button7Click(Sender: TObject); var newbmp:TBitmap; i,j,bmpheight,bmpwidth:integer; begin newbmp:= TBitmap.Create; newbmp.Width:=image1.Width; newbmp.Height:=image1.Height; bmpheight:=image1.Height; bmpwidth:=image1.Width; i:=bmpheight; while i>0 do begin for j:=10 to i do begin newbmp.Canvas.CopyRect(Rect(0,j-10,bmpwidth,j), image1.Canvas, Rect(0,i-10,bmpwidth,i)); form1.Canvas.Draw(120,100,newbmp); end; i:=i-10; end; newbmp.free; end;
上述圖形特效顯示效果在Windows 98、Delphi 4.0下運行通過。當然圖形效果還有許多,讀者只要明白其中原理,就可以很容易設計并演示其他效果。
|