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

VC书籍

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <printers.hpp>
  6. #pragma hdrstop
  7. #include "Unit1.h"
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. #pragma resource "*.dfm"
  11. TForm1 *Form1;
  12. //---------------------------------------------------------------------------
  13. __fastcall TForm1::TForm1(TComponent* Owner)
  14.         : TForm(Owner)
  15. {
  16.   PrnDpi=180;
  17.   TabLeft=100;
  18.   TabTop=100;
  19. }
  20. //---------------------------------------------------------------------------
  21. int __fastcall TForm1::ToSz(int nn)
  22. {
  23.   int ii;
  24.   double aa;
  25.   aa=nn;
  26.   aa/=96;
  27.   aa*=PrnDpi;
  28.   ii=ceill(aa);
  29.   return ii;
  30. }
  31. //---------------------------------------------------------------------------
  32. void __fastcall TForm1::PaintText(TCanvas *cv,int xx,int yy,int ww,int hh,int indent,TAlignment al,AnsiString text)
  33. {
  34.   TRect rc;
  35.   tagRECT drect;
  36.   int cw,ch,fsz,ll;
  37.   bool chgfsz=False;
  38.   cw=cv->TextWidth(text);
  39.   if((cw+2*indent)>ww)
  40.   {
  41.     fsz=cv->Font->Size;//保存字号,准备改变字号
  42.     chgfsz=True;
  43.     for(;;)
  44.     {
  45.       if(cv->Font->Size<=8)break;
  46.       cv->Font->Size--;
  47.       cw=cv->TextWidth(text);
  48.       if((cw+2*indent)<=ww)break;
  49.     }
  50.   }
  51. //
  52.   cw=cv->TextWidth(text);
  53. //超宽处理
  54.   if((cw+2*indent)>ww)
  55.   {
  56.     drect.left=xx+indent;
  57.     drect.top=yy+2;
  58.     drect.right=xx+ww-indent;
  59.     drect.bottom=yy+hh-2;
  60.     ll=text.Length();
  61.     DrawText(cv->Handle,text.c_str(),ll,&drect,DT_WORDBREAK);
  62.   }
  63.   else
  64.   {
  65. //按对齐方式输出文本
  66.     ch=cv->TextHeight(text);
  67.     ll=hh-ch;ll/=2;if(ll<1)ll=1;
  68.     if(al==taLeftJustify)
  69.     {
  70.       cv->TextOut(xx+indent,yy+ll,text);
  71.     }
  72.     else if(al==taRightJustify)
  73.     {
  74.       xx=xx+ww-cw-indent;
  75.       cv->TextOut(xx,yy+ll,text);
  76.     }
  77.     else
  78.     {
  79.       ch=ww-cw;ch/=2;
  80.       cv->TextOut(xx+ch,yy+ll,text);
  81.     }
  82.   }
  83.   if(chgfsz)cv->Font->Size=fsz;
  84. }
  85. //---------------------------------------------------------------------------
  86. AnsiString __fastcall TForm1::GetFieldValue(TDataSet *Dset,AnsiString name)
  87. {
  88.   double aa;
  89.   TDate dd;
  90.   TField *field;
  91.   AnsiString ass="";
  92.   field=Dset->FindField(name);
  93.   if(field==NULL)return ass;
  94.   switch(field->DataType)
  95.   {
  96.     case ftString:
  97.       ass=Dset->FieldByName(name)->AsString;
  98.       break;
  99.     case ftDate:
  100.     case ftDateTime:
  101.       dd=Dset->FieldByName(name)->AsDateTime;
  102.       ass=DateToStr(dd);
  103.       break;
  104.     case ftInteger:
  105.     case ftFloat:
  106.       aa=Dset->FieldByName(name)->AsFloat;
  107.       ass=FloatToStr(aa);
  108.       break;
  109.     default:
  110.       break;
  111.   }
  112.   return ass;
  113. }
  114. //---------------------------------------------------------------------------
  115. void __fastcall TForm1::Button1Click(TObject *Sender)
  116. {
  117.   if(OpenDialog1->Execute())
  118.   {
  119.     Query1->Active=False;
  120.     Query1->SQL->Clear();
  121.     Query1->SQL->Add("SELECT *");
  122.     Query1->SQL->Add("From ""+OpenDialog1->FileName+""");
  123.     Query1->Active=True;
  124.   }
  125. }
  126. //---------------------------------------------------------------------------
  127. int __fastcall TForm1::PrintTableTitle(TDBGrid *DGrid,TCanvas *cv,int x1,int y1)
  128. {
  129.   AnsiString ass;
  130.   int TitleHeight,ii,nn,left,ww,top;
  131.   left=x1;
  132.   top=y1;
  133.   cv->Font=DGrid->TitleFont;
  134.   TitleHeight=Canvas->TextHeight("高度")*2;
  135.   nn=DGrid->Columns->Count;
  136.   for(ii=0;ii<nn;ii++)
  137.   {
  138.     ass=DGrid->Columns->Items[ii]->FieldName;
  139.     ww=DGrid->Columns->Items[ii]->Width;
  140.     if(ww>=6)
  141.     {
  142.       PaintText(cv,ToSz(left),ToSz(top),ToSz(ww),ToSz(TitleHeight),
  143.         ToSz(1),DGrid->Columns->Items[ii]->Title->Alignment,ass);
  144.       cv->MoveTo(ToSz(left),ToSz(top));
  145.       cv->LineTo(ToSz(left),ToSz(top+TitleHeight));
  146.       cv->LineTo(ToSz(left+ww),ToSz(top+TitleHeight));
  147.       cv->LineTo(ToSz(left+ww),ToSz(top));
  148.       cv->LineTo(ToSz(left),ToSz(top));
  149.     }
  150.     left=left+ww;
  151.   }
  152.   return TitleHeight;
  153. }
  154. //---------------------------------------------------------------------------
  155. int __fastcall TForm1::PrintTableItem(TDBGrid *DGrid,TCanvas *cv,int x1,int y1)
  156. {
  157.   AnsiString ass;
  158.   int ItemHeight,ii,nn,left,ww,top;
  159.   left=x1;
  160.   top=y1;
  161.   cv->Font=DGrid->Font;
  162.   ItemHeight=Canvas->TextHeight("高度")*2;
  163.   nn=DGrid->Columns->Count;
  164.   for(ii=0;ii<nn;ii++)
  165.   {
  166.     ass=GetFieldValue(DGrid->DataSource->DataSet,DGrid->Columns->Items[ii]->FieldName);
  167.     ww=DGrid->Columns->Items[ii]->Width;
  168.     if(ww>=6)
  169.     {
  170.       PaintText(cv,ToSz(left),ToSz(top),ToSz(ww),ToSz(ItemHeight),
  171.         ToSz(1),DGrid->Columns->Items[ii]->Title->Alignment,ass);
  172.       cv->MoveTo(ToSz(left),ToSz(top));
  173.       cv->LineTo(ToSz(left),ToSz(top+ItemHeight));
  174.       cv->LineTo(ToSz(left+ww),ToSz(top+ItemHeight));
  175.       cv->LineTo(ToSz(left+ww),ToSz(top));
  176.       cv->LineTo(ToSz(left),ToSz(top));
  177.     }
  178.     left=left+ww;
  179.   }
  180.   return ItemHeight;
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TForm1::Button2Click(TObject *Sender)
  184. {
  185.   int top,lines,cot;
  186.   TCanvas *cv;
  187.   PrnDpi=GetDeviceCaps(Printer()->Handle,LOGPIXELSX);
  188.   cv=Printer()->Canvas;
  189.   Printer()->BeginDoc();
  190.   SetBkMode(Printer()->Canvas->Handle,TRANSPARENT);
  191.   Query1->First();
  192.   top=TabTop;
  193.   top+=PrintTableTitle(DBGrid1,cv,TabLeft,top);
  194.   lines=atoi(Edit1->Text.c_str());
  195.   if(lines<2)lines=2;
  196.   cot=0;
  197.   while(!Query1->Eof)
  198.   {
  199.     top+=PrintTableItem(DBGrid1,cv,TabLeft,top);
  200.     Query1->Next();
  201.     cot++;
  202.     if(cot>lines)
  203.     {
  204.       cot=0;
  205.       Printer()->NewPage();
  206.       top=TabTop;
  207.       top+=PrintTableTitle(DBGrid1,cv,TabLeft,top);
  208.     }
  209.   }
  210.   Printer()->EndDoc();
  211. }
  212. //---------------------------------------------------------------------------