Michael Knight
asked on
How to set printers fonts while printing using TextOut
How can I do this? I'm reading a file from disk line-by-line and then spitting to the printer spooler using TextOut. On a couple of line nums I need to reduce the point size to 5. It needs to start at 6. Here's the code that prints it all at about 11-12 point. Thanks.
Michael A Knight
// create the print object (display print dlg set, print all pages w/o page numbers, hide print to file, disable selection)
CPrintDialog *PrintTheReportDlg = new CPrintDialog(FALSE,PD_ALLP AGES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION | PD_RETURNDC);
// display the Print Dialog where user can select their preferred settings if Print button was pressed
if (ShowPrintDlg)
{
int return_value = PrintTheReportDlg->DoModal ();
// if dialog did not return IDOK, then check for an error occurance
if (return_value != IDOK)
{
if (return_value != IDCANCEL)
AfxMessageBox("The Printer Dialog Returned An Error.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
delete PrintTheReportDlg;
return;
}
}
// on AutoPrint for problem batches (when selected in the Property Pages), we just call for the default printer info
else
PrintTheReportDlg->GetDefa ults();
// get a handle to the selected printer device context
HDC PrinterHandle = PrintTheReportDlg->GetPrin terDC();
// if we have a NULL handle, display error to user and return
if (PrinterHandle == NULL)
{
AfxMessageBox("Error Getting Handle For Printer.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
delete PrintTheReportDlg;
return;
}
// if we have a good handle, let's keep going and try to print this thing
else
{
CDC *PrinterCDCHandle = new CDC; // declare a device context object
DOCINFO ReportDocumentInformation; // structure to contain document (report) info
TEXTMETRIC PrinterMetrics; // structure to contain printer page info (for GetTextMetrics)
int NumberOfLinesPrinted; // used for looping through the number of available lines per page (set to 60)
int EndOfFileFound = FALSE; // flag to tell page loop when we have reached the end of the document
FILE *ReportFile; // file handle for the report data file
// attach to the printer handle
PrinterCDCHandle->Attach(P rinterHand le);
// set the map mode (what are we printing- determines metrics sizing)
PrinterCDCHandle->SetMapMo de(MM_TEXT );
// get the printer metrics (exact printable space on page)
PrinterCDCHandle->GetTextM etrics(&Pr interMetri cs);
// initialize the DOCINFO structure to all zeroes (do this when you have a structure with a size member)
memset(&ReportDocumentInfo rmation,0, sizeof(DOC INFO));
// set up the document info structure
ReportDocumentInformation. lpszDocNam e = "Solid Test " + StationID + " EOT Report";
ReportDocumentInformation. lpszOutput = NULL;
ReportDocumentInformation. cbSize = sizeof(DOCINFO);
// open report file for reading
sprintf(TempStr,"d:\\repor t.dat"); // construct the filename
ReportFile = fopen(TempStr,"r"); // open file
// if there was an error opening the file, then display error to user, clear mem and return
if (ReportFile == NULL)
AfxMessageBox("Error Opening Report File.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
// if we opened the file okay, then continue
else
{
// tell the system that we are starting the document
if (PrinterCDCHandle->StartDo c(&ReportD ocumentInf ormation) == -1)
AfxMessageBox("Error Starting Document.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
// cycle through the pages (should only be one page, but I'm ready for more <g>)
for (;;)
{
// if we have found the end of the file, then break outta main for loop
if (EndOfFileFound)
break;
// set the system to start the page
if (PrinterCDCHandle->StartPa ge() < 0)
{
AfxMessageBox("Error Starting Page.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
break;
}
// cycle through the number of available lines on a page
for (NumberOfLinesPrinted = 1; NumberOfLinesPrinted <= 60; NumberOfLinesPrinted++)
{
// get new line of data and check for end of file
if (fgets(TempStr,900,ReportF ile) == NULL)
{
EndOfFileFound = TRUE; // set the end of file flag for main page loop
break; // break outta line per page loop
}
// strip any newlines from text read in
char *pdest = strchr(TempStr,'\n');
int result = pdest - TempStr;
if(pdest != NULL)
TempStr[result] = 0;
// send the data to the printer spool
if (PrinterCDCHandle->TextOut (0,((Print erMetrics. tmHeight+P rinterMetr ics.tmExte rnalLeadin g)*NumberO fLinesPrin ted),
TempStr,strlen(TempStr)) == 0)
{
AfxMessageBox("Error Writing To Device.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
EndOfFileFound = TRUE;
break;
}
}
// set the system to the end of page
if (PrinterCDCHandle->EndPage () < 0)
{
AfxMessageBox("Error Ending Page.\nA Page May Print.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
break;
}
}
// close the report file
_fcloseall();
// tell system to end the document (close spool)
if (PrinterCDCHandle->EndDoc( ) < 0)
AfxMessageBox("Error Closing Document.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
}
// delete the device context handle got by GetPrinterDC
if (PrinterCDCHandle->Detach( ) == NULL)
AfxMessageBox("Error Detaching Handle For Printer.\nReport May Print.",MB_OK | MB_ICONEXCLAMATION);
// delete CDC class created with new operator
delete PrinterCDCHandle;
}
// delete CPrintDialog class created with new operator
delete PrintTheReportDlg;
Michael A Knight
// create the print object (display print dlg set, print all pages w/o page numbers, hide print to file, disable selection)
CPrintDialog *PrintTheReportDlg = new CPrintDialog(FALSE,PD_ALLP
// display the Print Dialog where user can select their preferred settings if Print button was pressed
if (ShowPrintDlg)
{
int return_value = PrintTheReportDlg->DoModal
// if dialog did not return IDOK, then check for an error occurance
if (return_value != IDOK)
{
if (return_value != IDCANCEL)
AfxMessageBox("The Printer Dialog Returned An Error.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
delete PrintTheReportDlg;
return;
}
}
// on AutoPrint for problem batches (when selected in the Property Pages), we just call for the default printer info
else
PrintTheReportDlg->GetDefa
// get a handle to the selected printer device context
HDC PrinterHandle = PrintTheReportDlg->GetPrin
// if we have a NULL handle, display error to user and return
if (PrinterHandle == NULL)
{
AfxMessageBox("Error Getting Handle For Printer.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
delete PrintTheReportDlg;
return;
}
// if we have a good handle, let's keep going and try to print this thing
else
{
CDC *PrinterCDCHandle = new CDC; // declare a device context object
DOCINFO ReportDocumentInformation;
TEXTMETRIC PrinterMetrics; // structure to contain printer page info (for GetTextMetrics)
int NumberOfLinesPrinted; // used for looping through the number of available lines per page (set to 60)
int EndOfFileFound = FALSE; // flag to tell page loop when we have reached the end of the document
FILE *ReportFile; // file handle for the report data file
// attach to the printer handle
PrinterCDCHandle->Attach(P
// set the map mode (what are we printing- determines metrics sizing)
PrinterCDCHandle->SetMapMo
// get the printer metrics (exact printable space on page)
PrinterCDCHandle->GetTextM
// initialize the DOCINFO structure to all zeroes (do this when you have a structure with a size member)
memset(&ReportDocumentInfo
// set up the document info structure
ReportDocumentInformation.
ReportDocumentInformation.
ReportDocumentInformation.
// open report file for reading
sprintf(TempStr,"d:\\repor
ReportFile = fopen(TempStr,"r"); // open file
// if there was an error opening the file, then display error to user, clear mem and return
if (ReportFile == NULL)
AfxMessageBox("Error Opening Report File.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
// if we opened the file okay, then continue
else
{
// tell the system that we are starting the document
if (PrinterCDCHandle->StartDo
AfxMessageBox("Error Starting Document.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
// cycle through the pages (should only be one page, but I'm ready for more <g>)
for (;;)
{
// if we have found the end of the file, then break outta main for loop
if (EndOfFileFound)
break;
// set the system to start the page
if (PrinterCDCHandle->StartPa
{
AfxMessageBox("Error Starting Page.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
break;
}
// cycle through the number of available lines on a page
for (NumberOfLinesPrinted = 1; NumberOfLinesPrinted <= 60; NumberOfLinesPrinted++)
{
// get new line of data and check for end of file
if (fgets(TempStr,900,ReportF
{
EndOfFileFound = TRUE; // set the end of file flag for main page loop
break; // break outta line per page loop
}
// strip any newlines from text read in
char *pdest = strchr(TempStr,'\n');
int result = pdest - TempStr;
if(pdest != NULL)
TempStr[result] = 0;
// send the data to the printer spool
if (PrinterCDCHandle->TextOut
TempStr,strlen(TempStr)) == 0)
{
AfxMessageBox("Error Writing To Device.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
EndOfFileFound = TRUE;
break;
}
}
// set the system to the end of page
if (PrinterCDCHandle->EndPage
{
AfxMessageBox("Error Ending Page.\nA Page May Print.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
break;
}
}
// close the report file
_fcloseall();
// tell system to end the document (close spool)
if (PrinterCDCHandle->EndDoc(
AfxMessageBox("Error Closing Document.\nCannot Print Report.",MB_OK | MB_ICONEXCLAMATION);
}
// delete the device context handle got by GetPrinterDC
if (PrinterCDCHandle->Detach(
AfxMessageBox("Error Detaching Handle For Printer.\nReport May Print.",MB_OK | MB_ICONEXCLAMATION);
// delete CDC class created with new operator
delete PrinterCDCHandle;
}
// delete CPrintDialog class created with new operator
delete PrintTheReportDlg;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
// create the FONTs used by the Solid Test Program
CFont LargeTextFont,SmallTextFon
MediumTextFont.CreatePoint