用好Delphi的打印技能的编程技巧
点击次数:63 次 发布日期:2008-11-09 08:42:00 作者:源代码网
|
源代码网推荐 源代码网推荐 给单位开发软件,涉及一打印模块,我感到颇有兴趣,就拿来其中的一个小功能模块与读者共享。下面以打印在纸张的矩形框内为例简单介绍: 源代码网推荐 源代码网推荐 程序要求: 源代码网推荐 源代码网推荐 单击[打印]按钮,把Memo的内容最多分三行打印出来,每行最多能容纳22个三号字, 源代码网推荐 限定汉字上限为50个汉字。 源代码网推荐 源代码网推荐 编程思路: 源代码网推荐 源代码网推荐 用LineTo和MoveTo函数画一矩形框,根据Memo组件的内容长度用Copy函数把它分割为1到3个子串。在矩形框内美观地输出文字时技术处理为:当输出一行时最多可打印18个汉字,当输出多行时第一、二行分别打印16、18个汉字。 源代码网推荐 源代码网推荐 编程步骤: 源代码网推荐 源代码网推荐 1、首先新建一工程,在窗体上加一个Memo组件Button组件。 源代码网推荐 源代码网推荐 2、Memo组件的Lines值为空,MaxLength值为“100”(即50个汉字),字体为“三号字”;Button的Caption值为“打印”。 源代码网推荐 源代码网推荐 3、添加[打印]按钮的事件处理过程代码Button1.Click,首先在Interface的Uses部分添加Printers,其完整代码如下: 源代码网推荐 源代码网推荐 procedure TForm1.Button1Click(Sender: TObject); 源代码网推荐 源代码网推荐 var StrLen , Left,Top , WordHeight , wordWidth : Integer; 源代码网推荐 源代码网推荐 ContentStr : String[100]; 源代码网推荐 源代码网推荐 Str1, Str2, Str3 : String[36]; 源代码网推荐 源代码网推荐 begin 源代码网推荐 源代码网推荐 with Printer do 源代码网推荐 源代码网推荐 begin 源代码网推荐 源代码网推荐 Canvas.Font.Size:=16; 源代码网推荐 源代码网推荐 wordHeight:=Canvas.TextHeight 源代码网推荐 源代码网推荐 ("字"); 源代码网推荐 源代码网推荐 wordWidth:=Canvas.TextWidth 源代码网推荐 源代码网推荐 ("字"); 源代码网推荐 源代码网推荐 Left:=(Printer.PageWidth-wordWidth*22) div 2; 源代码网推荐 源代码网推荐 Top:=(Printer.PageHeight-wordHeight*7) div 2; 源代码网推荐 源代码网推荐 BeginDOC; 源代码网推荐 源代码网推荐 With Canvas do 源代码网推荐 源代码网推荐 begin 源代码网推荐 源代码网推荐 Pen.Width:=3; 源代码网推荐 源代码网推荐 {画一个22字宽,7个字高的矩形框} 源代码网推荐 源代码网推荐 MoveTo(Left,Top); 源代码网推荐 源代码网推荐 LineTo(Left wordWidth*22,Top); 源代码网推荐 源代码网推荐 LineTo(Left wordWidth*22, 源代码网推荐 源代码网推荐 Top wordHeight*7); 源代码网推荐 源代码网推荐 LineTo(Left,Top wordHeight*7); 源代码网推荐 源代码网推荐 LineTo(Left,Top); 源代码网推荐 源代码网推荐 ContentStr:=Memo1.Lines.Text; 源代码网推荐 源代码网推荐 StrLen:=Length(ContentStr); 源代码网推荐 源代码网推荐 if StrLen< 37 then 源代码网推荐 源代码网推荐 {分一行打印} 源代码网推荐 源代码网推荐 begin 源代码网推荐 源代码网推荐 TextOut(Left WordWidth*2, Top Wordheight*3, ContentStr) 源代码网推荐 源代码网推荐 end 源代码网推荐 源代码网推荐 else if StrLen< 66 then 源代码网推荐 源代码网推荐 {在垂直方向中间分两行打印} 源代码网推荐 源代码网推荐 begin 源代码网推荐 源代码网推荐 Str1:=Copy(ContentStr, 0, 32); 源代码网推荐 源代码网推荐 Str2:=Copy(ContentStr, 33, StrLen-32); 源代码网推荐 源代码网推荐 TextOut(Left WordWidth*4, Top WordHeight*(7-2) div 2 , Str1); 源代码网推荐 源代码网推荐 TextOut(Left WordWidth*2, Top WordHeight*(7-2) div 2 wordHeight, Str2); 源代码网推荐 源代码网推荐 end 源代码网推荐 源代码网推荐 else 源代码网推荐 源代码网推荐 {分三行打印} 源代码网推荐 源代码网推荐 begin 源代码网推荐 源代码网推荐 Str1:=Copy(ContentStr,0,32); 源代码网推荐 源代码网推荐 Str2:=Copy(ContentStr,33,36); 源代码网推荐 源代码网推荐 Str3:=Copy(ContentStr, 69, StrLen-68); 源代码网推荐 源代码网推荐 TextOut(Left WordWidth*4, Top WordHeight*2, Str1); 源代码网推荐 源代码网推荐 {左缩进两个汉字} 源代码网推荐 源代码网推荐 TextOut(Left WordWidth*2, Top WordHeight*3, Str2); 源代码网推荐 源代码网推荐 TextOut(Left WordWidth*2, Top WordHeight*4, Str3); 源代码网推荐 源代码网推荐 end 源代码网推荐 源代码网推荐 end; 源代码网推荐 源代码网推荐 EndDoc; 源代码网推荐 源代码网推荐 end; 源代码网推荐 源代码网推荐 end; 源代码网推荐 源代码网推荐 以上程序在Windows 98/Me Delphi 6.0调试通过,希望能对初次编写打印功能程序的读者有所帮助。 源代码网推荐 源代码网供稿. |
