选择显示字体大小

用delphi实现打印功能

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

  程序要求:

  单击[打印]按钮,把memo的内容最多分三行打印出来,每行最多能容纳22个三号字,限定汉字上限为50个汉字。

  编程思路:

  用l.neto和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);
   l.neto(left+wordwidth*22,top);
   l.neto(left+wordwidth*22,
  top+wordheight*7);
   l.neto(left,top+wordheight*7);
   l.neto(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调试通过,希望能对初次编写打印功能程序的读者有所帮助。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons