|
Thanks much Rishi.
In fact, I read that article after I posted - and it fixed my problem too (still don't understand why the bitmap printed some of the time).
-sf
|
|
|
|
|
When i am using the sample code of article mentined by Chris Munde i got same problem as u have told that my printer goes in Spooling mode.
I have seen the link that u have mentioned to look for solving this problem. But i have question that it there is something like StretchBelt in the sample code.
Now what to do for this?? THis is funny problem.
If possible give me sample code that solve the problem.
|
|
|
|
|
I insert a View into dialog and want to print the View ,but I dont know how to add code in OnBeginPrinting ,OnEndPrinting and OnPrint,please give me a example .
thanks
I love all help me.
|
|
|
|
|
i want to print from a
CScrollView which is not
my base class for my MFC application.
Do u have any idea how to printpreview & print
the doc on the scrollview.
thank u
Vikas Amin
Embin Technology
Bombay
vikas.amin@embin.com
|
|
|
|
|
Thanx Chris. It helped me a lot. But i faced one problem. I didnt initialize the Max. Pages for the CPrintInfo structure and the it made the loop endless. So, i suggest the readers to make sure to initialize the max. no. of pages so that they can actually see the output on the printer
Regards
Atul
|
|
|
|
|
Chris, thank you very much for this article, it solves my troubles!
William G.S.
|
|
|
|
|
Hi. I need to print the contens of a multiline CEdit control. Can someone tell me which is the easiest way to do it? A few tips would be fine. Thanks.
|
|
|
|
|
This is about as easy as you can do it...
void CMyDlg::OnPrintEditControl()
{
CPrintDialog dlg(FALSE);
CDC dc;
#if 0 //Show print dialog.
if (dlg.DoModal() == IDCANCEL)
return;
dc.Attach(dlg.GetPrinterDC());
#else //Don't show print dialog.
PRINTDLG prtDlg;
AfxGetApp()->GetPrinterDeviceDefaults(&prtDlg);
dlg.m_pd.hDevMode = prtDlg.hDevMode;
dlg.m_pd.hDevNames = prtDlg.hDevNames;
dc.Attach(dlg.CreatePrinterDC());
#endif
dc.m_bPrinting = TRUE;
CRect printArea;
printArea.SetRect(0, 0, dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
DOCINFO docinfo;
memset(&docinfo, 0, sizeof(docinfo));
docinfo.cbSize = sizeof(docinfo);
docinfo.lpszDocName = _T("Edit Control Print");
dc.StartDoc(&docinfo);
dc.StartPage();
CString sText;
m_edit1.GetWindowText(sText);
dc.DrawText(sText, printArea, DT_NOPREFIX | DT_WORDBREAK);
dc.EndPage();
dc.EndDoc();
} While this works it does not handle pagination. It also uses the default font.
Hope this helps...
Jonathan Craig
www.mcw-tech.com
|
|
|
|
|
Hello every one :
How can I dealing the date in Hijjry ?
thank you for your effort
Omar Halabiah
|
|
|
|
|
hi,
the tute was real cool,
thanks mate,
kreep.
|
|
|
|
|
I need help!!
I use the method, dc.StartDoc, dc.StartPage, dc.TextOut, dc.EndPage and dc.EndDoc for printing documents, but I need to print in a different page size (equivalent to third part of a letter page).
So, how can I do for changing the page size in customing way and printer print in that page size and not advance form-feed like a letter page?
thanks
|
|
|
|
|
I'm not sure what you are asking, but whenever printing I always start by getting the printer's page size. I use the method below to get the information. This will get the page size of the CDC passed as parameter one:
#define PRINTER_PAGE_HIMETRIC 1
#define PRINTER_PAGE_DEVICE_UINTS 2
void GetPrinterPageSizeByCDC(CDC &dc, int nUnits,
CSize &sizePage, CSize &sizePrintArea, CSize &sizePhysicalOffset,
int &nLogPixelsX, int &nLogPixelsY)
{
nLogPixelsX = dc.GetDeviceCaps(LOGPIXELSX);
nLogPixelsY = dc.GetDeviceCaps(LOGPIXELSY);
if(nUnits == PRINTER_PAGE_HIMETRIC)
{
double x_HIMETRICperDU = 25.4 * 100.0 / double(nLogPixelsX);
double y_HIMETRICperDU = 25.4 * 100.0 / double(nLogPixelsY);
sizePage.cx = int(double(dc.GetDeviceCaps(PHYSICALWIDTH)) * x_HIMETRICperDU);
sizePage.cy = int(double(dc.GetDeviceCaps(PHYSICALHEIGHT)) * y_HIMETRICperDU);
sizePhysicalOffset.cx = int(double(dc.GetDeviceCaps(PHYSICALOFFSETX)) * x_HIMETRICperDU);
sizePhysicalOffset.cy = int(double(dc.GetDeviceCaps(PHYSICALOFFSETY)) * y_HIMETRICperDU);
sizePrintArea.cx = int(double(dc.GetDeviceCaps(HORZRES)) * x_HIMETRICperDU);
sizePrintArea.cy = int(double(dc.GetDeviceCaps(VERTRES)) * y_HIMETRICperDU);
}
else
{
sizePage.cx = dc.GetDeviceCaps(PHYSICALWIDTH);
sizePage.cy = dc.GetDeviceCaps(PHYSICALHEIGHT);
sizePhysicalOffset.cx = dc.GetDeviceCaps(PHYSICALOFFSETX);
sizePhysicalOffset.cy = dc.GetDeviceCaps(PHYSICALOFFSETY);
sizePrintArea.cx = dc.GetDeviceCaps(HORZRES);
sizePrintArea.cy = dc.GetDeviceCaps(VERTRES);
}
} You can modify this function as needed. I have it setup to return the page size as either PRINTER_PAGE_HIMETRIC or PRINTER_PAGE_DEVICE_UINTS. Just set parameter 2 to one of the defines. It will return to you the following:
CSize sizePage - The size of the physical page.
CSize sizePrintArea - The size of the actual printable area.
CSize sizePhysicalOffset - The offset from the upper left corner of the physical page to the upper left corner of the printable area.
int nLogPixelsX - Units per inch on the X axis.
int nLogPixelsY - Units per inch on the Y axis.
With this information you can easily calculate 1/3 of the page.
Hope this helps...
Jonathan Craig
www.mcw-tech.com
|
|
|
|
|
In order to print a Mschart from a dialog, I wanted to use this way of printing. I finally succeed to make it work or almost work ...
In fact everything works well (it prints !) on my development PC (win2k, quite recent, with all visual studio stuff) but nothing is printed on another PC (win98, much older, and just with the 2 files -.ocx and .dll- added for MSchart). It's not the same printer too but the one connected to the old PC is an epson stylus 760 supposed to be able to perform the StretchBlt.
Here my code launched when the Print button is clicked :
void CGraphDlg::OnEssai()
{
CDC dc;
BOOL ok;
// order chart to copy itself onto clipboard
m_Chart.EditCopy();
CloseClipboard();
// make sure the item in clipboard is a bitmap
if(IsClipboardFormatAvailable(CF_BITMAP))
{
if(OpenClipboard())
{
m_hbitmap = (HBITMAP)::GetClipboardData(CF_BITMAP);
CloseClipboard();
}
}
// lance l'interface pour l'imprimante
CPrintDialog printDialog(false);
bool bShowPrintDialog = TRUE; //FALSE lance directement l'impression
if (bShowPrintDialog)
{
int r = printDialog.DoModal();
if (r == IDCANCEL) // Get printer settings from user
return;
}
else
{
printDialog.GetDefaults();
}
dc.Attach(printDialog.GetPrinterDC()); // Attach a printer DC
dc.m_bPrinting = TRUE;
CString strTitle; // Get the application title
strTitle.LoadString(AFX_IDS_APP_TITLE);
DOCINFO di; // Initialise print document details
::ZeroMemory (&di, sizeof (DOCINFO));
di.cbSize = sizeof (DOCINFO);
di.lpszDocName = strTitle;
int bPrintingOK = dc.StartDoc(&di); // Begin a new print job
// Get the printing extents and store in the m_rectDraw field of a
CPrintInfo Info;
CString toto;
Info.m_rectDraw.SetRect(0,0, dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
OnBeginPrinting(&dc, &Info); // empty funtion
dc.StartPage(); // begin new page
Info.m_nCurPage = 1;
OnPrint(&dc, &Info); // my "Print page" function
OnEndPrinting(&dc, &Info); // empty funtion
dc.EndPage(); // end new page
dc.EndDoc(); // end a print job
dc.Detach(); // detach the printer DC
}
and here in my OnPrint fctn (based on an example):
void CGraphDlg::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// Create a text for the title
CString sTitleHeader=_T("My First Chart");
CString toto;
// Get the page size and boundaries
CRect rectPage = pInfo->m_rectDraw;
TEXTMETRIC tm;
CFont font;
CSize textSize;
int cyChar;
// Create the font we will be using
font.CreatePointFont(240, "Arial", pDC);
CFont *pOldFont=pDC->SelectObject(&font);
//Set Margin
rectPage.top+=rectPage.bottom/48;
rectPage.bottom-=rectPage.bottom/48;
rectPage.left+=200;
rectPage.right-=200;
// Get Text size in order to center
pDC->GetTextMetrics(&tm);
textSize=pDC->GetTextExtent(sTitleHeader);
cyChar = tm.tmHeight;
// Draw Text (centered)
pDC->TextOut(((rectPage.right+rectPage.left)/2)-(textSize.cx/2), rectPage.top, sTitleHeader);
rectPage.top += cyChar + cyChar / 4;
// Draw header line divider
pDC->MoveTo(rectPage.left, rectPage.top);
pDC->LineTo(rectPage.right, rectPage.top);
// Go to next line
rectPage.top += cyChar / 4;
if(m_hbitmap)
{
BITMAP bm;
AfxMessageBox ("dans impression bitmap",MB_OK,MB_ICONSTOP);
::GetObject(m_hbitmap, sizeof(BITMAP), &bm);
CSize chartSize(bm.bmWidth, bm.bmHeight);
CDC dcMemory,dcScreen;
dcScreen.Attach(::GetDC(NULL));
// create "from" device context and select the loaded bitmap into it
dcMemory.CreateCompatibleDC(&dcScreen);
dcMemory.SelectObject(m_hbitmap);
// Print at 85% size within left/right margin
CSize printSize;
printSize.cx=(int)(rectPage.right*.85);
printSize.cy=printSize.cx/chartSize.cx*chartSize.cy;
toto.Format ( "cx = %d\ncy = %d\n",printSize.cx,printSize.cy);
AfxMessageBox (toto,MB_OK,MB_ICONSTOP);
// Print chart centered
pDC->StretchBlt( ((rectPage.right+rectPage.left)/2)-(printSize.cx/2),
rectPage.top, printSize.cx, printSize.cy, &dcMemory,
0, 0, chartSize.cx, chartSize.cy, SRCCOPY);
dcMemory.DeleteDC();
}
// Revert and Destroy
pDC->SelectObject(pOldFont);
font.DeleteObject();
}
Do you have an idea of what happens or how to progress in my investigations ?
Do you guess if problem comes from OnPrint routine itself or from the print without Doc/view framework ?
Note : I simplified my OnPrint routine to just print the title and drop the print of MsChart and get same problem, nothing is printed. At EndPage() call, I can see the printer spooler saying "print page 0 on 0 pages" that must not be a good sign ...
Thanks in advance for your help.
DD
|
|
|
|
|
hello chris,
I have a really Big problem.Iam using "HP LaserJet 2100" printer.
Iam attaching the sample code in this.PL help me by trying that code in your PC and get me a modified code which prints for landsacpe printing.The code is like this
This prints only potraiot...pl help me
/**********************************************************/
void CEditboxView::OnPrinting()
{
// TODO: Add your command handler code here
CString temp;
CPrintDialog dlg(FALSE);
if (IDOK == dlg.DoModal())
{
HANDLE hPrinter;
DOC_INFO_1 DocInfo;
DWORD dwJob;
DWORD dwBytesWritten;
CString szPrinterName = dlg.GetDeviceName();
DEVMODE* devMode = dlg.GetDevMode();
PRINTER_DEFAULTS defaults;
defaults.pDatatype = NULL;
defaults.DesiredAccess = PRINTER_ACCESS_USE;
defaults.pDevMode =devMode;
// Need a handle to the printer.
BOOL bRtn = OpenPrinter( (char*)(const char *)szPrinterName, &hPrinter,&defaults );
if( bRtn == FALSE)
return ;
// Fill in the structure with info about this "document."
DocInfo.pDocName = "My Document";
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = "RAW";
// Inform the spooler the document is beginning.
if( (dwJob = StartDocPrinter( hPrinter, 1, (unsigned char *)&DocInfo )) == 0 )
{
ClosePrinter( hPrinter );
return ;
}
// Start a page.
if( ! StartPagePrinter( hPrinter ) )
{
EndDocPrinter( hPrinter );
ClosePrinter( hPrinter );
return ;
}
// Send the data to the printer.
if( !WritePrinter( hPrinter, "hello how are you", 20, &dwBytesWritten ) )
{
EndPagePrinter( hPrinter );
EndDocPrinter( hPrinter );
ClosePrinter( hPrinter );
return ;
}
// End the page.
if( ! EndPagePrinter( hPrinter ) )
{
EndDocPrinter( hPrinter );
ClosePrinter( hPrinter );
return ;
}
// Inform the spooler that the document is ending.
if( ! EndDocPrinter( hPrinter ) )
{
ClosePrinter( hPrinter );
return ;
}
// Tidy up the printer handle.
ClosePrinter( hPrinter );
}
}
/*********************************************************/
nbeed solution and advance thanx
|
|
|
|
|
I used that method for line-printing: OpenPrinter, StartDocPrinter, etc, but How can I do for change the font size and style?
|
|
|
|
|
set
defaults.pDevMode->dmFields = defaults.pDevMode->dmFields |DM_ORIENTATION;
defaults.pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
before opening printer
|
|
|
|
|
I have dialog and tree control in it. Now, i want to print only tree screen (not all dialog). How can i do it ? If somebody knows, help me, please
|
|
|
|
|
As told by Chris, derive ur dialog from public CDialog, TreeCtrlPrint. Add functions :
OnBeginPrinting
OnEndPrinting
For Printing, u have to refer to an article by Roger Allen
(search "print the content of a CTreeCtrl
")
U have to derive ur TreeCtrl from the tree control given by him which is printable.
When the event for printing is called, say on the Press of a button from the dialog, Prepare CDC and CPrintInfo and pass it to the Print() function in which u have to write the code as suggested by Roger. The tree control has APIs which will take care of the rest of the work.
Regards
Atul
|
|
|
|
|
print the content of screen as it is to the printer using MFC dialog based application
please give me full code
|
|
|
|
|
Please help me to print content of screen as it is to printer and also showing the print preview without doc/view for single frame window.
thanks
|
|
|
|
|
void CRamsDlg::OnPrint()
{
// TODO: Add your control notification handler code here
CPrintDialog cpd(FALSE);//,PD_ALLPAGES,this);
cpd.GetDefaults(); //Default printer
CDC printerDC;
printerDC.Attach(cpd.GetPrinterDC());
DOCINFO di;
di.cbSize=sizeof(DOCINFO);
di.lpszDocName="ACS Bill";
di.fwType=NULL;
di.lpszDatatype=NULL;
di.lpszOutput=NULL;
int PageWidth = 1500;
int PageLength =1500;
//Start new Document
printerDC.StartDoc(&di);
//Start new page
printerDC.StartPage();
printerDC.SetMapMode(MM_ANISOTROPIC); //MM_TEXT//1 to 1 pixel mapping
CDC *pDC=GetDC(); //DC of dialog
CRect cr;
GetClientRect(cr);
printerDC.BitBlt(0, 0, cr.Width()*3, cr.Height()*3, pDC, 0, 0, SRCCOPY);
printerDC.EndPage();
//Close printer document
printerDC.EndDoc();
//Finished
printerDC.DeleteDC();
ReleaseDC(pDC);
}
try this code .if there is any problem contact me
regards
meenachi
|
|
|
|
|
How can I make the image fill the whole page?
Thanks by the way.
|
|
|
|
|
CPrintDialog printDlg(FALSE);
printDlg.m_pd.nCopies = 1 ;
printDlg.m_pd.nMaxPage = m_CountPage ;
printDlg.m_pd.nMinPage = 1 ;
printDlg.m_pd.nFromPage = 1 ;
printDlg.m_pd.nToPage = m_CountPage ;
printDlg.m_pd.Flags =PD_ALLPAGES|PD_HIDEPRINTTOFILE|PD_PAGENUMS|PD_SELECTION|
PD_COLLATE|PD_RETURNDC|PD_ENABLEPRINTHOOK|PD_ENABLESETUPHOOK ;
printDlg.DoModal() ; // not show dialog
{CPrintDialog hWnd=0x00000000}
default printer Epson LQ-300 ESC/P 2 .
Thanks .
|
|
|
|
|
Start off by saying that the code works great in Release mode and is fine for what I'm trying to do.
However, when running the program in debug mode I get 1 assertation error, which is coming from a function: _free_dbg().
I have created a Print class, inserted the majority of your code into a function Print.PrintFile() but the debugger fails when I leave my OnPrint function in the dialog, e.g.
OnPrint()
{
set up DC and File Objects
Open File for reading
Call Print.PrintFile(File)
Close File
DeleteDC()
} <- DEBUGGER HAS PROBLEM WHEN GETTING HERE
However, in Debug mode the code still prints out the file before crashing.
Just wondering if anyone else had come across this problem, and/or is there a solution.
Cheers
Greg
|
|
|
|
|
Your best chance to get this problem sorted is to post all you code from your printing. It sounds like you have a dynamically allocated array which you are writing beyond the beginning/end off. So either post the whole OnPrint() procedure here, or mail it to me, and I will have a look.
Roger Allen
Sonork 100.10016
If I had a quote, it would be a very good one.
|
|
|
|
|