求一编写打印程序的思路!高手指点!(100分)

  • 主题发起人 主题发起人 lizhen_1020
  • 开始时间 开始时间
L

lizhen_1020

Unregistered / Unconfirmed
GUEST, unregistred user!
求一编写打印程序的思路!高手指点!
我是一个初学者,但是看了几个别人写的程序后被搞的头晕死了,
请高手指点,Escape(printer.Handle,GETPHYSPAGESIZE,0,nil,@p),
GetDeviceCaps(Printer.Handle, PHYSICALWIDTH)和Printer.PageHeight(PageWidth)有什么不同?
还有Screen.PixelsPerInch与GetDeviceCaps(Printer.Handle, LOGPIXELSX)有什么不同?
还有打印机实际页面与页面设置中的A4,A3有什么关系?
请高手帮忙解释一下,再给一个打印思路?
 
Escape(printer.Handle,GETPHYSPAGESIZE,0,nil,@p) //不通过GDI
GetDeviceCaps(Printer.Handle, PHYSICALWIDTH) //分辩率
Printer.PageHeight(PageWidth) //纸张高度
Screen.PixelsPerInch //仅用于屏幕
与GetDeviceCaps(Printer.Handle, LOGPIXELSX)
 
其实很简单:
参数:PageWidth, PageHeight, PageMargin (unit: mm/0.1mm);
过程:GetDeviceCaps->CalcPageSize(MapToDC)->CalcPaintRect(MapToDC)
->SelectGDIObject(such as font, brush, pen)->DrawText->DrawText...
如果是一般的预览页面,传显示组件的 Canvas,如是是打印机,传打印机的Canvas:
==> Printer.begin
Doc;
//如果是打印机
while TaskIsNotFinisheddo
begin
Draw, Draw, Draw.............
==>Printer.NewPage;
//如果是打印机
end;
==>Printer.EndDoc;
如果是打印机
 
哦,还少了步:CalcMaxPageLines, CalcMaxPageIndex(Page-Count)
 
to ddev:
有没有源程序,让小弟看一个?谢谢!
 
ok,不过是用 BCB 写的,代码比较长,没有删减,辛苦各位,不好意思:
void __fastcall TReportForm::DoPreparePrint(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::DoPagePaint(TCanvas* ACanvas, const TRect&amp;
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), &amp;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]), &amp;TextRect, DT_SINGLELINE | DT_LEFT | DT_VCENTER);
DrawText(hDC, szSubTitle[1], strlen(szSubTitle[1]), &amp;TextRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
DrawText(hDC, szSubTitle[2], strlen(szSubTitle[2]), &amp;TextRect, DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
DrawRect.top += dwTextHeight;

//绘制网格
::DrawGrid(hDC, &amp;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];
::DrawCell(hDC, ACoord, szCellText.c_str(), uFormat, &amp;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];
::DrawCell(hDC, ACoord, szCellText.c_str(), uFormat, &amp;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);
::DrawCell(hDC, ACoord, szCellText.c_str(), uFormat, &amp;DrawRect, grdcls);
}
}
//释放内存
delete grdcls;
grdcls = NULL;
}
//---------------------------------------------------------------------------
 
谢谢大家!!!!!!!!!
 
谢谢大家!!!!!!!!!
 
后退
顶部