首页 天气预报 新闻 邮箱 搜索 短信 聊天
上移动梦网
赢手机大奖

新浪首页 > 科技时代 > 网上学园 > 开 发 者 > 正文
用好Delphi的打印技能的编程技巧

http://www.sina.com.cn 2002/09/03 18:05 赛迪网--中国电脑教育报

  文/刘福旺

  给单位开发软件,涉及一打印模块,我感到颇有兴趣,就拿来其中的一个小功能模块与读者共享。下面以打印在纸张的矩形框内为例简单介绍:

  程序要求:

  单击[打印]按钮,把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调试通过,希望能对初次编写打印功能程序的读者有所帮助。


发表评论】【初学者园地】【科技聊天】【关闭窗口

新 闻 查 询

 相关链接
Delphi编程实例:桌面小精灵 (2002/05/15 13:55)
用Delphi开发简单的WebMail程序 (2002/04/28 15:06)
Borland Delphi专业培训认证体系登陆中国 (2002/02/26 11:00)
用Delphi 5.0编写在线播放器 (2002/02/04 12:38)
Delphi编程使用经验四则 (2001/12/18 17:02)
Delphi程序的时限和加密方法 (2001/12/17 14:31)
通用微型面包车装备Delphi娱乐系统 (2001/11/09 14:52)
Delphi应用编程答疑解惑 (2001/11/02 16:14)
用Delphi再现Windows纸牌游戏 (2001/10/30 10:56)
Delphi发布Q4预期 并预期裁员1,500 (2001/10/17 04:40)


科技时代意见反馈留言板 电话:010-82612286 或 010-82628888-5488   欢迎批评指正

新浪简介 | 用户注册 | 广告服务 | 招聘信息 | 中文阅读 | Richwin | 联系方式 | 产品答疑

Copyright © 1996 - 2002 SINA.com, Stone Rich Sight. All Rights Reserved

版权所有 四通利方 新浪网