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.   TPrinter *prn;
  19.   Graphics::TBitmap *bmp;
  20.   int dpi,PageWidth,PageHeight;
  21.   prn=Printer();
  22.   dpi=GetDeviceCaps(Printer()->Handle,LOGPIXELSX);
  23.   PageWidth=5;//预定义5英寸
  24.   PageHeight=5;
  25.   bmp = new Graphics::TBitmap();
  26.   bmp->Width=PageWidth*dpi;
  27.   bmp->Height=PageHeight*dpi;
  28.   bmp->Canvas->Font->PixelsPerInch=dpi;
  29.   bmp->Canvas->Rectangle(0.2*dpi,0.2*dpi,(5-0.2)*dpi,(5-0.2)*dpi);
  30.   bmp->Canvas->Font->Size=12;
  31.   bmp->Canvas->TextOut(0.3*dpi,0.3*dpi,"中华人民共和国");
  32.   bmp->Canvas->TextOut(0.3*dpi,0.5*dpi,"全国人民代表大会");
  33.   PaintBox1->Canvas->CopyRect(Rect(0,0,PaintBox1->Width,PaintBox1->Height),
  34.     bmp->Canvas,Rect(0,0,bmp->Width,bmp->Height));
  35.   if(Application->MessageBox("打印吗","提示",MB_YESNO)==IDYES)
  36.   {
  37.     prn->BeginDoc();
  38.     prn->Canvas->StretchDraw(Rect(50,50,PageWidth*dpi,PageHeight*dpi),bmp);
  39. //    Printer()->Canvas->TextOut(0.3*dpi,0.3*dpi,"中华人民共和国111");
  40. //    Printer()->Canvas->CopyRect(Rect(50,50,PageWidth*dpi+50,PageHeight*dpi+50),
  41. //      bmp->Canvas,Rect(0,0,bmp->Width,bmp->Height));
  42.     prn->EndDoc();
  43.   }
  44.   delete bmp;
  45. }
  46. //---------------------------------------------------------------------------