Unit1.cpp
上传用户:lhxd_sz
上传日期:2014-10-02
资源大小:38814k
文件大小:2k
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include <printers.hpp>
- #pragma hdrstop
- #include "Unit1.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- tagRECT drect;
- AnsiString ass;
- int left,top,right,bottom,dpix,dpiy,asslen;
- TPrinter *prn;
- prn=Printer();
- prn->Title="打印文本示例";
- prn->BeginDoc();
- dpix=GetDeviceCaps(prn->Handle,LOGPIXELSX);
- dpiy=GetDeviceCaps(prn->Handle,LOGPIXELSY);
- left=2*dpix/2.54;
- top=2*dpiy/2.54;
- right=left+5*dpix/2.54;
- bottom=top+5*dpix/2.54;
- prn->Canvas->Font->Name="黑体";
- prn->Canvas->Font->Size=16;
- drect.left=left;drect.top=top;
- drect.right=right;
- drect.bottom=bottom;
- ass="限制文本在矩形框内打印";
- asslen=ass.Length();
- DrawText(prn->Canvas->Handle,ass.c_str(),
- asslen,&drect,DT_CENTER|DT_WORDBREAK);
- prn->Canvas->TextOut(left,bottom,"文本打印示例");
- prn->EndDoc();
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button2Click(TObject *Sender)
- {
- Graphics::TBitmap *bmp;
- TRect rc;
- int dpix,dpiy;
- TPrinter *prn;
- if(!OpenPictureDialog1->Execute())return;
- bmp = new Graphics::TBitmap();
- prn=Printer();
- prn->Title="打印位图示例";
- try
- {
- bmp->LoadFromFile(OpenPictureDialog1->FileName);
- prn->BeginDoc();
- dpix=GetDeviceCaps(prn->Handle,LOGPIXELSX);
- dpiy=GetDeviceCaps(prn->Handle,LOGPIXELSY);
- rc.left=2*dpix/2.54;
- rc.top=2*dpiy/2.54;
- rc.Right=rc.left+bmp->Width*dpix/96;
- rc.Bottom=rc.left+bmp->Height*dpiy/96;
- prn->Canvas->StretchDraw(rc,bmp);
- prn->EndDoc();
- }
- catch (...)
- {
- ShowMessage("读取位图不正确!");
- }
- delete bmp;
- }
- //---------------------------------------------------------------------------