Delphi实现图像文本旋转特效完整实例代码
本文以实例讲述了Delphi实现图像文本旋转特效的解决方法,在本程序中利用的控件主要是Panel控件、Image控件、Edit控件、Label控件和Button控件。本程序的关键是利用Delphi的bmp_rotate()函数来实现旋转图像的功能。并巧妙地调用相关WindowsAPI函数来实现对文本的旋转特效。
完整的实例代码如下:
unitUnit1; interface uses Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,math, StdCtrls,ExtCtrls; type TForm1=class(TForm) Edit1:TEdit; Image1:TImage; bmprotate:TButton; Label1:TLabel; Panel1:TPanel; Image2:TImage; Panel2:TPanel; Image3:TImage; Edit2:TEdit; RotatedText:TButton; Label2:TLabel; procedurebmprotateClick(Sender:TObject); procedureRotatedTextClick(Sender:TObject); procedureFormCreate(Sender:TObject); private {Privatedeclarations} public {Publicdeclarations} procedurebmp_rotate(src,dst:Tbitmap;angle:extended); ProcedureDrawRotatedText(TheCanvas:TCanvas;TheAngle:Integer; TheText:String); end; var Form1:TForm1; implementation {$R*.DFM} procedureTForm1.bmp_rotate(src,dst:Tbitmap;angle:extended);//图像旋转 var c1x,c1y,c2x,c2y:integer; p1x,p1y,p2x,p2y:integer; radius,n:integer; alpha:extended; c0,c1,c2,c3:tcolor; begin angle:=(angle/180)*pi;//将文本框内的内容转换角度 c1x:=src.Widthdiv2; c1y:=src.Heightdiv2; c2x:=dst.Widthdiv2; c2y:=dst.Heightdiv2; ifc2x<c2ythen n:=c2y else n:=c2x; dec(n,1);//使n值减少1,n:=n-1 forp2x:=0tondobegin forp2y:=0tondobegin ifp2x=0then alpha:=pi/2 else alpha:=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; procedureTForm1.bmprotateClick(Sender:TObject); Var RAngle:Extended; begin RAngle:=StrToFloat(Edit1.Text);//设置旋转的角度 bmp_rotate(Image1.Picture.bitmap,Image2.Picture.bitmap,RAngle);//运行旋转函数,旋转图形 end; ProcedureTForm1.DrawRotatedText(TheCanvas:TCanvas;TheAngle:Integer; TheText:String); var lf:TLogFont; tf:TFont; begin Image3.Canvas.refresh; withTheCanvasdobegin Font.Name:='Arial'; Font.Size:=18; Brush.Style:=bsClear; tf:=TFont.Create; try tf.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);//使这个对象运用到程序中 finally tf.Free;//释放对象 end; TextOut(Image3.left,Image3.top+20,TheText);//绘制文本到指定区域 end; end; procedureTForm1.RotatedTextClick(Sender:TObject); Var TheAngle:Integer; begin TheAngle:=StrToInt(Edit2.Text);//将文本框中的文本转换角度 DrawRotatedText(Image3.Canvas,TheAngle,'Delphi图形工作室');//调用旋转文本函数 end; procedureTForm1.FormCreate(Sender:TObject); var tf:TFont; theText:string; begin theText:='Delphi图形工作室';//设置文本 withImage3.Canvasdo begin Font.Name:='Arial';//设置字体 Font.Size:=18;//设置字体大小 Brush.Style:=bsClear;//设置笔刷样式 tf:=TFont.Create;//创建一个Font具体对象 TextOut(Image3.left,Image3.top+20,TheText);//绘制文本到指定区域 end; end; end.