Printing to exact coordinates on paper or lottery tickets
-
How to? I know the basic printing functions and using the CPrintDialog plus PRINTDLG pd, a little sample that only prints a statement but shows what I am using:
CPrintDialog dlg(FALSE, pd.Flags); if (dlg.DoModal() != IDOK) { AfxMessageBox("Abort or Unknown Printer or Printer device error"); return; } pd.hDC = dlg.CreatePrinterDC(); ASSERT(pd.hDC !=0);
/////////////////////////////////////////////////////////////////////////////////////////
CDC \* dc = new CDC; dc = CDC::FromHandle(pd.hDC); // Get Height and Width of large character int cxPage = ::GetDeviceCaps (pd.hDC, HORZRES) ; int cyPage = ::GetDeviceCaps (pd.hDC, VERTRES) ; sizePrn.cx = cxPage; sizePrn.cy = cyPage; memset(&lf, 0, sizeof(lf)); lf.lfHeight = -MulDiv(12, dc->GetDeviceCaps(LOGPIXELSY), 72);//abs(lf.lfHeight) lf.lfWeight = FW\_BOLD; //FW\_MEDIUM; //; lf.lfPitchAndFamily = FIXED\_PITCH | FF\_MODERN; lf.lfQuality = PROOF\_QUALITY; //NEW lstrcpy(lf.lfFaceName, "Times New Roman"); //"Courier"); VERIFY(font.CreateFontIndirect(&lf)); dc->SetMapMode (MM\_ISOTROPIC) ; dc->SetWindowExt ( 1000, 1000) ; dc->SetViewportExt (cxPage / 2, -cyPage / 2) ; dc->SetViewportOrg (cxPage / 2, cyPage / 2) ; dc->SetTextAlign(TA\_BASELINE | TA\_CENTER); dc->SetBkMode(OPAQUE); dc->SetMapMode(MM\_TEXT); dc->PatBlt(0, 0, sizePrn.cx, sizePrn.cy, WHITENESS); dc->LPtoDP(&sizePrn); dc->SelectObject(&font) ; CSize extentChar = dc ->GetTextExtent("M",1); int nCharHeight = extentChar.cy+4; int nCharWidth = extentChar.cx+10;
// Begin printing the doc
BeginWaitCursor();CString printDate = GetMyCurDateTime(); DOCINFO docinfo; memset(&docinfo, 0, sizeof(docinfo)); docinfo.cbSize = sizeof(docinfo); docinfo.lpszDocName = \_T("Calif Lottery Winnings"); docinfo.fwType = 0; rc = dc->StartDocA(&docinfo); if (rc < 0) { sprintf(temp, "Unable to Begin printing - Error\[%d\]", rc); MessageBox(temp, NULL, MB\_OK); dc->ReleaseAttribDC(); dc->ReleaseOutputDC(); dc->DeleteTempMap(); EndWaitCursor(); DeleteDC(pd.hDC); if(pd.hDevMode != NULL) GlobalFree(pd.hDevMode); if(pd.hDevNames != NULL) GlobalFree(pd.hDevNames); dc = 0; return; } szTitle=CString("California Lottery System Printout ") + printDate; //Print a Page Header dc -> StartPage(); dc -> SetTextAlign(TA\_LEFT | TA\_TOP); dc -> TextOut(0, 0, szTitle, szTitle.GetLength() );
-
How to? I know the basic printing functions and using the CPrintDialog plus PRINTDLG pd, a little sample that only prints a statement but shows what I am using:
CPrintDialog dlg(FALSE, pd.Flags); if (dlg.DoModal() != IDOK) { AfxMessageBox("Abort or Unknown Printer or Printer device error"); return; } pd.hDC = dlg.CreatePrinterDC(); ASSERT(pd.hDC !=0);
/////////////////////////////////////////////////////////////////////////////////////////
CDC \* dc = new CDC; dc = CDC::FromHandle(pd.hDC); // Get Height and Width of large character int cxPage = ::GetDeviceCaps (pd.hDC, HORZRES) ; int cyPage = ::GetDeviceCaps (pd.hDC, VERTRES) ; sizePrn.cx = cxPage; sizePrn.cy = cyPage; memset(&lf, 0, sizeof(lf)); lf.lfHeight = -MulDiv(12, dc->GetDeviceCaps(LOGPIXELSY), 72);//abs(lf.lfHeight) lf.lfWeight = FW\_BOLD; //FW\_MEDIUM; //; lf.lfPitchAndFamily = FIXED\_PITCH | FF\_MODERN; lf.lfQuality = PROOF\_QUALITY; //NEW lstrcpy(lf.lfFaceName, "Times New Roman"); //"Courier"); VERIFY(font.CreateFontIndirect(&lf)); dc->SetMapMode (MM\_ISOTROPIC) ; dc->SetWindowExt ( 1000, 1000) ; dc->SetViewportExt (cxPage / 2, -cyPage / 2) ; dc->SetViewportOrg (cxPage / 2, cyPage / 2) ; dc->SetTextAlign(TA\_BASELINE | TA\_CENTER); dc->SetBkMode(OPAQUE); dc->SetMapMode(MM\_TEXT); dc->PatBlt(0, 0, sizePrn.cx, sizePrn.cy, WHITENESS); dc->LPtoDP(&sizePrn); dc->SelectObject(&font) ; CSize extentChar = dc ->GetTextExtent("M",1); int nCharHeight = extentChar.cy+4; int nCharWidth = extentChar.cx+10;
// Begin printing the doc
BeginWaitCursor();CString printDate = GetMyCurDateTime(); DOCINFO docinfo; memset(&docinfo, 0, sizeof(docinfo)); docinfo.cbSize = sizeof(docinfo); docinfo.lpszDocName = \_T("Calif Lottery Winnings"); docinfo.fwType = 0; rc = dc->StartDocA(&docinfo); if (rc < 0) { sprintf(temp, "Unable to Begin printing - Error\[%d\]", rc); MessageBox(temp, NULL, MB\_OK); dc->ReleaseAttribDC(); dc->ReleaseOutputDC(); dc->DeleteTempMap(); EndWaitCursor(); DeleteDC(pd.hDC); if(pd.hDevMode != NULL) GlobalFree(pd.hDevMode); if(pd.hDevNames != NULL) GlobalFree(pd.hDevNames); dc = 0; return; } szTitle=CString("California Lottery System Printout ") + printDate; //Print a Page Header dc -> StartPage(); dc -> SetTextAlign(TA\_LEFT | TA\_TOP); dc -> TextOut(0, 0, szTitle, szTitle.GetLength() );
When things get too complicate (re: printing), I create "forms" / pages / windows that match the required output; then "capture" the visual and print that in one go (as an "image" or whatever). Total WYSIWYG. Users can even "update" the "report" (if you let them).
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
When things get too complicate (re: printing), I create "forms" / pages / windows that match the required output; then "capture" the visual and print that in one go (as an "image" or whatever). Total WYSIWYG. Users can even "update" the "report" (if you let them).
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
That is a good idea, will look into it.