Hi,
I found some code on the net which showed how to print text on a page. I modified it for my use, but i encountered a problem: when the function printed sth, it wrote some weird characters at the end of each line. I can't solve the problem, i need help. Here is the code:
Code:
bool PrintTextPRN(char* outputText)
{
PRINTDLG pd;
ZeroMemory(&pd, sizeof(PRINTDLG));
// populate it
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = NULL;
pd.hDevMode = NULL;
pd.hDevNames = NULL;
pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;
pd.nCopies = 1;
pd.nFromPage = 0xFFFF;
pd.nToPage = 0xFFFF;
pd.nMinPage = 1;
pd.nMaxPage = 0xFFFF;
if (!PrintDlg(&pd)) {
MessageBox (NULL, "A printer error has occurred.\r\n\r\nPlease check the print cable and\r\nmake sure the printer is turned on.", "Print Error!", MB_OK);
}
// declare a DOCINFO structure and populate it
DOCINFO di;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Glowdot Port Scanner";
di.lpszOutput = (LPTSTR)NULL;
di.fwType = 0;
int leftMargin = 1 * GetDeviceCaps(pd.hDC, LOGPIXELSX);
int rightMargin = 1 * GetDeviceCaps(pd.hDC, LOGPIXELSX);
int topMargin = 1 * GetDeviceCaps(pd.hDC, LOGPIXELSY);
int bottomMargin = 1 * GetDeviceCaps(pd.hDC, LOGPIXELSY);
int leftOffset = leftMargin - GetDeviceCaps(pd.hDC, PHYSICALOFFSETX);
int rightOffset = rightMargin - (GetDeviceCaps(pd.hDC, PHYSICALWIDTH) - GetDeviceCaps(pd.hDC, PHYSICALOFFSETX)- GetDeviceCaps(pd.hDC, HORZRES));
int topOffset = topMargin - GetDeviceCaps(pd.hDC, PHYSICALOFFSETY);
int bottomOffset = bottomMargin - (GetDeviceCaps(pd.hDC, PHYSICALHEIGHT) - GetDeviceCaps(pd.hDC, PHYSICALOFFSETY)- GetDeviceCaps(pd.hDC, VERTRES));
int prnWidth = GetDeviceCaps(pd.hDC, HORZRES) - (leftOffset + rightOffset);
int prnHeight = GetDeviceCaps(pd.hDC, VERTRES) - (topOffset + bottomOffset);
TEXTMETRIC tm;
HFONT hfont = (HFONT)GetStockObject(ANSI_FIXED_FONT);
int yChar;
SetMapMode(pd.hDC, MM_TEXT);
SelectObject(pd.hDC, hfont);
GetTextMetrics(pd.hDC, &tm);
yChar = tm.tmHeight;
int xChar=tm.tmMaxCharWidth;
int HeaderHeight = 0;
int LinesPerPage = (prnHeight - HeaderHeight) / yChar;
StartDoc (pd.hDC, &di);
StartPage(pd.hDC);
char thisLine[80];
int i,j;
for (i = 0; i < strlen(outputText); i++) {
for (j = 0; j < 80; j++) {
if (*outputText == '\r') {
thisLine[j] = '\0';
outputText += 2;
break;
} else if (j == 80) {
thisLine[j] = '\0';
} else {
thisLine[j] = *outputText++;
}
}
if (((i - 1) % LinesPerPage == 0) && (i != 1)) {
// We should start a new page first
EndPage(pd.hDC);
StartPage(pd.hDC);
TextOut(pd.hDC,leftOffset, (yChar * i + HeaderHeight + topOffset)
- ((i - 1)/LinesPerPage)*prnHeight, thisLine, lstrlen(thisLine));
} else {
TextOut(pd.hDC,leftOffset, (yChar * i + HeaderHeight + topOffset)
- ((i - 1)/LinesPerPage)*prnHeight, thisLine, lstrlen(thisLine));
}
}
EndPage(pd.hDC);
EndDoc(pd.hDC);
DeleteDC(pd.hDC);
return TRUE;
}