Unit1.cpp
上传用户:lhxd_sz
上传日期:2014-10-02
资源大小:38814k
文件大小:2k
源码类别:

VC书籍

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <printers.hpp>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma resource "*.dfm"
  9. TForm1 *Form1;
  10. //---------------------------------------------------------------------------
  11. __fastcall TForm1::TForm1(TComponent* Owner)
  12.         : TForm(Owner)
  13. {
  14. }
  15. //---------------------------------------------------------------------------
  16. void __fastcall TForm1::Button1Click(TObject *Sender)
  17. {
  18.   tagRECT drect;
  19.   AnsiString ass;
  20.   int left,top,right,bottom,dpix,dpiy,asslen;
  21.   TPrinter *prn;
  22.   prn=Printer();
  23.   prn->Title="打印文本示例";
  24.   prn->BeginDoc();
  25.   dpix=GetDeviceCaps(prn->Handle,LOGPIXELSX);
  26.   dpiy=GetDeviceCaps(prn->Handle,LOGPIXELSY);
  27.   left=2*dpix/2.54;
  28.   top=2*dpiy/2.54;
  29.   right=left+5*dpix/2.54;
  30.   bottom=top+5*dpix/2.54;
  31.   prn->Canvas->Font->Name="黑体";
  32.   prn->Canvas->Font->Size=16;
  33.   drect.left=left;drect.top=top;
  34.   drect.right=right;
  35.   drect.bottom=bottom;
  36.   ass="限制文本在矩形框内打印";
  37.   asslen=ass.Length();
  38.   DrawText(prn->Canvas->Handle,ass.c_str(),
  39.       asslen,&drect,DT_CENTER|DT_WORDBREAK);
  40.   prn->Canvas->TextOut(left,bottom,"文本打印示例");
  41.   prn->EndDoc();
  42. }
  43. //---------------------------------------------------------------------------
  44. void __fastcall TForm1::Button2Click(TObject *Sender)
  45. {
  46.   Graphics::TBitmap *bmp;
  47.   TRect rc;
  48.   int dpix,dpiy;
  49.   TPrinter *prn;
  50.   if(!OpenPictureDialog1->Execute())return;
  51.   bmp = new Graphics::TBitmap();
  52.   prn=Printer();
  53.   prn->Title="打印位图示例";
  54.   try
  55.   {
  56.     bmp->LoadFromFile(OpenPictureDialog1->FileName);
  57.     prn->BeginDoc();
  58.     dpix=GetDeviceCaps(prn->Handle,LOGPIXELSX);
  59.     dpiy=GetDeviceCaps(prn->Handle,LOGPIXELSY);
  60.     rc.left=2*dpix/2.54;
  61.     rc.top=2*dpiy/2.54;
  62.     rc.Right=rc.left+bmp->Width*dpix/96;
  63.     rc.Bottom=rc.left+bmp->Height*dpiy/96;
  64.     prn->Canvas->StretchDraw(rc,bmp);
  65.     prn->EndDoc();
  66.   }
  67.   catch (...)
  68.   {
  69.     ShowMessage("读取位图不正确!");
  70.   }
  71.   delete bmp;
  72. }
  73. //---------------------------------------------------------------------------