[ Home | Contents | Search | Next | Previous | Up ]
Date: 11/30/99
Time: 12:10:49 AM
Q. Well I'm doing this project, and fore some reason my client insist
on being able to print line by line to a printer,- simular to the way it was done with
matrixprinters. The program is a win95 application and does anyone know. I can twist owl
or windows to be able to do this trick. Currently I'm writing to a file, with the
exstensions lpt1 og 2, that does put som print on the paper, but I cannot format my text
with bold, italic etc.
A.Here is some code may be helpful to you. The idea is just use TextOut to
printer dc.
if (theApp->Printing && theApp->Printer &&
!rect.IsEmpty()) {
// Use pageSize to get the size of the window to render
into. For a Window it's the client area,
// for a printer it's the printer DC dimensions and for
print preview it's the layout window.
TSize pageSize(rect.right - rect.left, rect.bottom - rect.top);
HFONT hFont = (HFONT)GetWindowFont();
TFont font("Courier New", -12);
if (hFont == 0)
dc.SelectObject(font);
//dc.SelectObject(TFont(hFont));
else
dc.SelectObject(TFont(hFont));
TEXTMETRIC tm;
int fHeight = (dc.GetTextMetrics(tm) == true) ? tm.tmHeight +
tm.tmExternalLeading : 10;
// How many lines of this font can we fit on a page.
int linesPerPage = MulDiv(pageSize.cy-200, 1, fHeight);
if (linesPerPage) {
TPrintDialog::TData &printerData =
theApp->Printer->GetSetup();
int maxPg = ((GetCount() / linesPerPage) +
1.0);
// Compute the number of pages to print.
printerData.MinPage = 1;
printerData.MaxPage = maxPg;
// Do the text stuff:
int fromPage = printerData.FromPage == -1 ? 1 :
printerData.FromPage;
int toPage = printerData.ToPage == -1 ? 1 :
//printerData.ToPage;
char buffer[255];
int currentPage = fromPage;
while (currentPage <= toPage) {
int startLine =
(currentPage - 1) * linesPerPage;
int lineIdx = 0;
while (lineIdx <
linesPerPage) {
// If the string is no longer valid then there's nothing more to display.
if (GetString(buffer, startLine + lineIdx) < 0)
break;
// this is the key code to print text line by line to printer:
dc.TabbedTextOut(TPoint(50, lineIdx * fHeight + 100), buffer, strlen(buffer), 0, NULL, 0);
lineIdx++;
}
currentPage++;
}
}
}
Guoxing Yang