Unit1.cpp
上传用户:lhxd_sz
上传日期:2014-10-02
资源大小:38814k
文件大小:6k
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include <math.h>
- #include <stdlib.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)
- {
- PrnDpi=180;
- TabLeft=100;
- TabTop=100;
- }
- //---------------------------------------------------------------------------
- int __fastcall TForm1::ToSz(int nn)
- {
- int ii;
- double aa;
- aa=nn;
- aa/=96;
- aa*=PrnDpi;
- ii=ceill(aa);
- return ii;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::PaintText(TCanvas *cv,int xx,int yy,int ww,int hh,int indent,TAlignment al,AnsiString text)
- {
- TRect rc;
- tagRECT drect;
- int cw,ch,fsz,ll;
- bool chgfsz=False;
- cw=cv->TextWidth(text);
- if((cw+2*indent)>ww)
- {
- fsz=cv->Font->Size;//保存字号,准备改变字号
- chgfsz=True;
- for(;;)
- {
- if(cv->Font->Size<=8)break;
- cv->Font->Size--;
- cw=cv->TextWidth(text);
- if((cw+2*indent)<=ww)break;
- }
- }
- //
- cw=cv->TextWidth(text);
- //超宽处理
- if((cw+2*indent)>ww)
- {
- drect.left=xx+indent;
- drect.top=yy+2;
- drect.right=xx+ww-indent;
- drect.bottom=yy+hh-2;
- ll=text.Length();
- DrawText(cv->Handle,text.c_str(),ll,&drect,DT_WORDBREAK);
- }
- else
- {
- //按对齐方式输出文本
- ch=cv->TextHeight(text);
- ll=hh-ch;ll/=2;if(ll<1)ll=1;
- if(al==taLeftJustify)
- {
- cv->TextOut(xx+indent,yy+ll,text);
- }
- else if(al==taRightJustify)
- {
- xx=xx+ww-cw-indent;
- cv->TextOut(xx,yy+ll,text);
- }
- else
- {
- ch=ww-cw;ch/=2;
- cv->TextOut(xx+ch,yy+ll,text);
- }
- }
- if(chgfsz)cv->Font->Size=fsz;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TForm1::GetFieldValue(TDataSet *Dset,AnsiString name)
- {
- double aa;
- TDate dd;
- TField *field;
- AnsiString ass="";
- field=Dset->FindField(name);
- if(field==NULL)return ass;
- switch(field->DataType)
- {
- case ftString:
- ass=Dset->FieldByName(name)->AsString;
- break;
- case ftDate:
- case ftDateTime:
- dd=Dset->FieldByName(name)->AsDateTime;
- ass=DateToStr(dd);
- break;
- case ftInteger:
- case ftFloat:
- aa=Dset->FieldByName(name)->AsFloat;
- ass=FloatToStr(aa);
- break;
- default:
- break;
- }
- return ass;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- if(OpenDialog1->Execute())
- {
- Query1->Active=False;
- Query1->SQL->Clear();
- Query1->SQL->Add("SELECT *");
- Query1->SQL->Add("From ""+OpenDialog1->FileName+""");
- Query1->Active=True;
- }
- }
- //---------------------------------------------------------------------------
- int __fastcall TForm1::PrintTableTitle(TDBGrid *DGrid,TCanvas *cv,int x1,int y1)
- {
- AnsiString ass;
- int TitleHeight,ii,nn,left,ww,top;
- left=x1;
- top=y1;
- cv->Font=DGrid->TitleFont;
- TitleHeight=Canvas->TextHeight("高度")*2;
- nn=DGrid->Columns->Count;
- for(ii=0;ii<nn;ii++)
- {
- ass=DGrid->Columns->Items[ii]->FieldName;
- ww=DGrid->Columns->Items[ii]->Width;
- if(ww>=6)
- {
- PaintText(cv,ToSz(left),ToSz(top),ToSz(ww),ToSz(TitleHeight),
- ToSz(1),DGrid->Columns->Items[ii]->Title->Alignment,ass);
- cv->MoveTo(ToSz(left),ToSz(top));
- cv->LineTo(ToSz(left),ToSz(top+TitleHeight));
- cv->LineTo(ToSz(left+ww),ToSz(top+TitleHeight));
- cv->LineTo(ToSz(left+ww),ToSz(top));
- cv->LineTo(ToSz(left),ToSz(top));
- }
- left=left+ww;
- }
- return TitleHeight;
- }
- //---------------------------------------------------------------------------
- int __fastcall TForm1::PrintTableItem(TDBGrid *DGrid,TCanvas *cv,int x1,int y1)
- {
- AnsiString ass;
- int ItemHeight,ii,nn,left,ww,top;
- left=x1;
- top=y1;
- cv->Font=DGrid->Font;
- ItemHeight=Canvas->TextHeight("高度")*2;
- nn=DGrid->Columns->Count;
- for(ii=0;ii<nn;ii++)
- {
- ass=GetFieldValue(DGrid->DataSource->DataSet,DGrid->Columns->Items[ii]->FieldName);
- ww=DGrid->Columns->Items[ii]->Width;
- if(ww>=6)
- {
- PaintText(cv,ToSz(left),ToSz(top),ToSz(ww),ToSz(ItemHeight),
- ToSz(1),DGrid->Columns->Items[ii]->Title->Alignment,ass);
- cv->MoveTo(ToSz(left),ToSz(top));
- cv->LineTo(ToSz(left),ToSz(top+ItemHeight));
- cv->LineTo(ToSz(left+ww),ToSz(top+ItemHeight));
- cv->LineTo(ToSz(left+ww),ToSz(top));
- cv->LineTo(ToSz(left),ToSz(top));
- }
- left=left+ww;
- }
- return ItemHeight;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button2Click(TObject *Sender)
- {
- int top,lines,cot;
- TCanvas *cv;
- PrnDpi=GetDeviceCaps(Printer()->Handle,LOGPIXELSX);
- cv=Printer()->Canvas;
- Printer()->BeginDoc();
- SetBkMode(Printer()->Canvas->Handle,TRANSPARENT);
- Query1->First();
- top=TabTop;
- top+=PrintTableTitle(DBGrid1,cv,TabLeft,top);
- lines=atoi(Edit1->Text.c_str());
- if(lines<2)lines=2;
- cot=0;
- while(!Query1->Eof)
- {
- top+=PrintTableItem(DBGrid1,cv,TabLeft,top);
- Query1->Next();
- cot++;
- if(cot>lines)
- {
- cot=0;
- Printer()->NewPage();
- top=TabTop;
- top+=PrintTableTitle(DBGrid1,cv,TabLeft,top);
- }
- }
- Printer()->EndDoc();
- }
- //---------------------------------------------------------------------------