<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      用好Delphi的打印技能的編程技巧

      [摘要]文/劉福旺  給單位開發軟件,涉及一打印模塊,我感到頗有興趣,就拿來其中的一個小功能模塊與讀者共享。下面以打印在紙張的矩形框內為例簡單介紹:  程序要求:  單擊[打印]按鈕,把Memo的內容最多分...
      文/劉福旺

        給單位開發軟件,涉及一打印模塊,我感到頗有興趣,就拿來其中的一個小功能模塊與讀者共享。下面以打印在紙張的矩形框內為例簡單介紹:

        程序要求:

        單擊[打印]按鈕,把Memo的內容最多分三行打印出來,每行最多能容納22個三號字,
      限定漢字上限為50個漢字。

        編程思路:

        用LineTo和MoveTo函數畫一矩形框,根據Memo組件的內容長度用Copy函數把它分割為1到3個子串。在矩形框內美觀地輸出文字時技術處理為:當輸出一行時最多可打印18個漢字,當輸出多行時第一、二行分別打印16、18個漢字。

        編程步驟:

        1、首先新建一工程,在窗體上加一個Memo組件Button組件。

        2、Memo組件的Lines值為空,MaxLength值為“100”(即50個漢字),字體為“三號字”;Button的Caption值為“打印”。

        3、添加[打。莅粹o的事件處理過程代碼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調試通過,希望能對初次編寫打印功能程序的讀者有所幫助。


      日韩精品一区二区三区高清