本文以實例講述了Delphi實作影像文字旋轉特效的解決方法,在本程式中所利用的控制項主要是Panel 控制項、Image 控制項、Edit 控制項、Label 控制項和Button 控制項。本程式的關鍵是利用Delphi 的bmp_rotate()函數來實現旋轉影像的功能。並巧妙地呼叫相關Windows API 函數來實現對文字的旋轉特效。
完整的實例程式碼如下:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,math,StdCtrls, ExtCtrls;typeTForm1 = class(TForm)Edit1: TEdit;Image1: TImage; ;Image2: TImage;Panel2: TPanel;Image3: TImage;Edit2: TEdit;RotatedText: TButton;Label2: TLabel;procedure bmprotateClick(Sender: TObject);procedure RotatedTextClick(Sender: TObject);procated Formbate(Swate); }public{ Public declarations }procedure bmp_rotate(src,dst:Tbitmap;angle:extended);Procedure DrawRotatedText(TheCanvas : TCanvas; TheAngle : Integer;TheText : String);end;varForm1: TForm1;implementation{$R *M}proc TForm1.bmp_rotate(src,dst:Tbitmap;angle:extended);//圖像旋轉varc1x,c1y,c2x,c2y:integer;p1x,p1y,p2x,p2y:integer;radius,n:integer;alpha:extended; ,c1,c2,c3:tcolor;beginangle := (angle / 180) * pi; //將文字方塊內的內容轉換角度c1x := src.Width div 2;c1y := src.Height div 2;c2x := dst.Width div 2;c2y := dst.Height div 2;if c2x < c2y thenn := c2yelsen := c2x;dec (n,1); //使n值減少1,n:=n-1for p2x := 0 to n do beginfor p2y := 0 to n do beginif p2x = 0 thenalpha:= pi/2elsealpha := arctan2(p2y,p2x);radius := round(sqrt((p2x*p2x)+(p2y*p2y)));//設定旋轉時的半徑大小p1x := round(radius * cos(angle+alpha)); //設定旋轉時的圓心橫座標p1y := round(radius * sin(angle+alpha)); //設定旋轉時的圓心縱座標c0 := src.Canvas.pixels[c1x+p1x,c1y+p1y];//替換影像相關座標處的像素c1 := src.Canvas.pixels[c1x-p1x,c1y-p1y];c2 := src.Canvas.pixels [c1x+p1y,c1y-p1x];c3 := src.Canvas.pixels[c1x-p1y,c1y+p1x];dst.Canvas.pixels[c2x+p2x,c2y+p2y]:=c0; //置換不同位置的像素點dst.Canvas.pixels[c2x-p2x,c2y-p2y]:=c1;dst.Canvas.pixels[c2x+p2y,c2y-p2x]:=c2;dst.Canvas.pixels[ c2x-p2y,c2y+p2x]:=c3;end;application.processmessages //回應其他的訊息請求end;end;procedure TForm1.bmprotateClick(Sender: TObject);VarRAngle : Extended;beginRAngle := StrToFloat(Edit1.Text); //設定旋轉的角度bmp_rotate(Image1.Picture.bitmap,Image); //設定旋轉的角度bmp_rotate(Image1.Picture.bitmap,Imageage. .Picture.bitmap, RAngle);//運行旋轉函數,旋轉圖形end;Procedure TForm1.DrawRotatedText(TheCanvas : TCanvas; TheAngle : Integer;TheText : String);varlf : TLogFont;tf : TFont;beginImage3.Canvas.refresh;with TheCanvas do beginFont.Name:='Arial';Font.Size:=18;Brush.Style:= bsClear;tf:= TFont.Create;trytf.Assign(Font);GetObject(tf.Handle, Sizeof(lf), @lf ); //將目前實例句柄賦給tf 這個Font 物件lf.lfEscapement:=TheAngle*10;lf.lfOrientation := TheAngle * 10; //讓輸入時的橫座標線與水平線呈所指定的角度tf.Handle := CreateFontIndirect(lf); //建立一個Font 具體物件Font.Assign(tf); //使這個物件運用到程式中finallytf.Free; //釋放物件end;TextOut(Image3.left, Image3.top+20 , TheText); //繪製文字到指定區域end;end;procedure TForm1.RotatedTextClick(Sender: TObject);VarTheAngle : Integer;beginTheAngle := StrToInt(Edit2.Text); //將文字方塊中的文字轉換角度DrawRotatedText(Image3.Canvas, TheAngle, 'Delphidle'); /圖形工作室'); /呼叫旋轉文字函數end;procedure TForm1.FormCreate(Sender: TObject);vartf : TFont;theText:string;begintheText:='Delphi圖形工作室'; //設定文字with Image3.Canvas dobeginFont.Name := 'Arial'; //設定字體Font.Size := 18; //設定字體大小Brush .Style := bsClear; //設定筆刷樣式tf:= TFont.Create; //建立一個Font 具體物件TextOut(Image3.left, Image3.top+20 , TheText); //繪製文字到指定區域end;end;end.