ok,不过是用 BCB 写的,代码比较长,没有删减,辛苦各位,不好意思:
void __fastcall TReportForm:
oPreparePrint(TCanvas* ACanvas, PRINT_INFO&
lpPrintInfo)
{
//准备打印字体
FTitleFont->Name = FPageDesc->TitleFontName;
FTitleFont->Size = FPageDesc->TitleFontSize ;
if (FPageDesc->TitleBold) FTitleFont->Style = TFontStyles() << fsBold;
if (FPageDesc->TitleItalic) FTitleFont->Style = TFontStyles() << fsItalic;
FTextFont->Name = FPageDesc->TextFontName ;
FTextFont->Size = FPageDesc->TextFontSize ;
if (FPageDesc->TextBold) FTextFont->Style = TFontStyles() << fsBold;
if (FPageDesc->TextItalic) FTextFont->Style = TFontStyles() << fsItalic;
//默认纸张为 A3 纸(297*420mm)
lpPrintInfo.dwPageWidth = FPageDesc->PageWidth ;
lpPrintInfo.dwPageHeight = FPageDesc->PageHeight ;
//默认页边矩为20mm
lpPrintInfo.dwLeftMargin = FPageDesc->LeftMargin ;
lpPrintInfo.dwTopMargin = FPageDesc->TopMargin ;
lpPrintInfo.dwRightMargin = FPageDesc->RightMargin ;
lpPrintInfo.dwBottomMargin = FPageDesc->BottomMargin ;
//默认字体:文本 9 号宋体,标题 16号黑体
lpPrintInfo.hTitleFont = FTitleFont->Handle ;
lpPrintInfo.hTextFont = FTextFont->Handle ;
//横打
lpPrintInfo.uPrintOrient = FPageDesc->PrintOrient;
//页面
lpPrintInfo.dwMaxPageLines = FPageDesc->MaxPageLines ;
lpPrintInfo.dwMinPageIndex = 1;
lpPrintInfo.dwMaxPageIndex = int(RepGrid->RowCount / (FPageDesc->MaxPageLines - 2))
+ ((RepGrid->RowCount % (FPageDesc->MaxPageLines - 2)) == 0 ? 0 : 1);
}
//---------------------------------------------------------------------------
void __fastcall TReportForm:
oPagePaint(TCanvas* ACanvas, const TRect&
ARect, const int APageIndex)
{
//获取设备缩放比
HDC hDC = ACanvas->Handle;
int ScaleX = GetDeviceCaps(hDC, LOGPIXELSX);
int ScaleY = GetDeviceCaps(hDC, LOGPIXELSY);
//准备网格
GRIDCLASS* grdcls = new GRIDCLASS;
grdcls->Cols = RepGrid->ColCount ;
grdcls->Rows = RepGrid->RowCount ;
grdcls->Color = clWhite;
grdcls->DefaultColWidth = 60 * ScaleX / 96;
grdcls->DefaultRowHeight = 21 * ScaleY / 96;
grdcls->LineStyle = GLS_BOTH;
grdcls->LineWidth = 1;
grdcls->LineColor = clBlack;
grdcls->ExtColWidths = new GRIDEXTENTS;
grdcls->ExtColWidths->Count = RepGrid->ColCount;
grdcls->ExtColWidths->Extents = new GRIDEXTENT[RepGrid->ColCount];
GRIDEXTENT* pExtent = grdcls->ExtColWidths->Extents;
for (int i = 0;
i < RepGrid->ColCount;
i++)
{
pExtent->Index = i;
pExtent->Extent = RepGrid->ColWidths
* ScaleX / 96;
pExtent++;
}
grdcls->ExtRowHeights = new GRIDEXTENTS;
grdcls->ExtRowHeights->Count = 2;
grdcls->ExtRowHeights->Extents = new GRIDEXTENT[2];
pExtent = grdcls->ExtRowHeights->Extents;
for (int i = 0;
i < 2;
i++)
{
pExtent->Index = i;
if (i == 0)
pExtent->Extent = 16 * ScaleX / 96;
else
pExtent->Extent = 28 * ScaleX / 96;
pExtent++;
}
const int MERGE_COUNT = 11;
grdcls->GridMerges = new GRID_MERGES;
grdcls->GridMerges->Count = MERGE_COUNT;
grdcls->GridMerges->Merges = new GRID_MERGE_INFO[MERGE_COUNT];
GRID_MERGE_INFO* pMergeInfo = grdcls->GridMerges->Merges;
for (int i = 0;
i < MERGE_COUNT;
i++)
{
switch (i) {
case 0:
pMergeInfo->Col = 0;
pMergeInfo->Row = 0;
pMergeInfo->MergeCols = 0;
pMergeInfo->MergeRows = 1;
break;
case 1:
pMergeInfo->Col = 1;
pMergeInfo->Row = 0;
pMergeInfo->MergeCols = 0;
pMergeInfo->MergeRows = 1;
break;
case 2:
pMergeInfo->Col = 2;
pMergeInfo->Row = 0;
pMergeInfo->MergeCols = 0;
pMergeInfo->MergeRows = 1;
break;
case 3:
pMergeInfo->Col = 3;
pMergeInfo->Row = 0;
pMergeInfo->MergeCols = 0;
pMergeInfo->MergeRows = 1;
break;
case 4:
pMergeInfo->Col = 4;
pMergeInfo->Row = 0;
pMergeInfo->MergeCols = 1;
pMergeInfo->MergeRows = 0;
break;
case 5:
pMergeInfo->Col = 6;
pMergeInfo->Row = 0;
pMergeInfo->MergeCols = 5;
pMergeInfo->MergeRows = 0;
break;
case 6:
pMergeInfo->Col = 12;
pMergeInfo->Row = 0;
pMergeInfo->MergeCols = 4;
pMergeInfo->MergeRows = 0;
break;
case 7:
pMergeInfo->Col = 17;
pMergeInfo->Row = 0;
pMergeInfo->MergeCols = 3;
pMergeInfo->MergeRows = 0;
break;
case 8:
pMergeInfo->Col = 21;
pMergeInfo->Row = 0;
pMergeInfo->MergeCols = 0;
pMergeInfo->MergeRows = 1;
break;
case 9:
pMergeInfo->Col = 22;
pMergeInfo->Row = 0;
pMergeInfo->MergeCols = 1;
pMergeInfo->MergeRows = 0;
break;
case 10:
pMergeInfo->Col = 24;
pMergeInfo->Row = 0;
pMergeInfo->MergeCols = 0;
pMergeInfo->MergeRows = 1;
break;
}
pMergeInfo++;
}
//绘制标题
RECT DrawRect = ARect;
RECT TextRect = DrawRect;
int dwTextHeight;
//绘制主标题
const char* szTitle = "________________镇拆迁安置情况一览表";
ACanvas->Font = FTitleFont;
dwTextHeight = ACanvas->TextHeight("H") * 2;
TextRect.bottom = TextRect.top + dwTextHeight;
DrawText(hDC, szTitle, strlen(szTitle), &TextRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
DrawRect.top += dwTextHeight;
//绘制副标题
const char* szSubTitle[3] = {"_____________________ 村", "________ 年 ____ 月 ____ 日", "单位:元"};
ACanvas->Font = FTextFont;
hDC = ACanvas->Handle;
dwTextHeight = ACanvas->TextHeight("H") * 2;
TextRect = DrawRect;
TextRect.bottom = TextRect.top + dwTextHeight;
DrawText(hDC, szSubTitle[0], strlen(szSubTitle[0]), &TextRect, DT_SINGLELINE | DT_LEFT | DT_VCENTER);
DrawText(hDC, szSubTitle[1], strlen(szSubTitle[1]), &TextRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
DrawText(hDC, szSubTitle[2], strlen(szSubTitle[2]), &TextRect, DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
DrawRect.top += dwTextHeight;
//绘制网格
:rawGrid(hDC, &DrawRect, grdcls);
//绘制网格标题
GRIDCOORD ACoord;
int uFormat;
String szCellText;
TRect CellRect;
ACanvas->Font = FTextFont;
hDC = ACanvas->Handle;
ACoord.Row = 0;
uFormat = DT_WORDBREAK | DT_VCENTER | DT_CENTER;
for (int i = 0;
i < RepGrid->ColCount;
i++)
{
ACoord.Col = i;
szCellText = RepGrid->CellText[ACoord.Col][ACoord.Row];
:rawCell(hDC, ACoord, szCellText.c_str(), uFormat, &DrawRect, grdcls);
}
ACoord.Row = 1;
uFormat = DT_WORDBREAK | DT_VCENTER | DT_CENTER;
for (int i = 0;
i < RepGrid->ColCount;
i++)
{
ACoord.Col = i;
szCellText = RepGrid->CellText[ACoord.Col][ACoord.Row];
:rawCell(hDC, ACoord, szCellText.c_str(), uFormat, &DrawRect, grdcls);
}
//绘制单元内容
int iPageRowFrom;
int iPageRowend;
//计算页起始行及终止行
if (APageIndex <= 1) //第一页
{
iPageRowFrom = RepGrid->FixedRows;
iPageRowEnd = FPageDesc->MaxPageLines < RepGrid->RowCount ? FPageDesc->MaxPageLines : RepGrid->RowCount;
}
else
{
iPageRowFrom = (FPageDesc->MaxPageLines - 2) * APageIndex + 2;
iPageRowEnd = iPageRowFrom + (FPageDesc->MaxPageLines < RepGrid->RowCount ? FPageDesc->MaxPageLines : RepGrid->RowCount);
}
if (iPageRowFrom < RepGrid->FixedRows) iPageRowFrom = RepGrid->FixedRows;
if (iPageRowEnd > RepGrid->RowCount) iPageRowEnd = RepGrid->RowCount;
//打印数据
for (int iRow = iPageRowFrom;
iRow < iPageRowend;
iRow++)
{
ACoord.Row = iRow;
for (int iCol = 0;
iCol < RepGrid->ColCount;
iCol++)
{
ACoord.Col = iCol;
szCellText = RepGrid->CellText[ACoord.Col][ACoord.Row];
uFormat = RepGrid->TextDrawFormat(ACoord.Col, ACoord.Row);
:rawCell(hDC, ACoord, szCellText.c_str(), uFormat, &DrawRect, grdcls);
}
}
//释放内存
delete grdcls;
grdcls = NULL;
}
//---------------------------------------------------------------------------