One more on print orientation and paper size specifications
-
Hello guys, I used to print the content of a RichEditText control with following code. But now I would like to print it in landscape and with paper specifications (12cm x8.5cm). All articles I read are talking about the famous DEVMODE, but I have no clue how to insert this DEVMODE in my existing code. Can you hep me ? Thanks in advance DD void CTestRichEditText::OnPrint() { // 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) return; } else { printDialog.GetDefaults(); } // recently added to try to print in landscape on special paper DEVMODE *MyPrintMode; MyPrintMode = printDialog.GetDevMode(); MyPrintMode->dmFields |= DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH; MyPrintMode->dmOrientation = DMORIENT_LANDSCAPE; MyPrintMode->dmPaperSize = 0; MyPrintMode->dmPaperLength = 120; MyPrintMode->dmPaperWidth = 85; // end of new added code HDC hPrinterDC = printDialog.GetPrinterDC(); FORMATRANGE fr; int nHorizRes = GetDeviceCaps(hPrinterDC, PHYSICALWIDTH), nVertRes = GetDeviceCaps(hPrinterDC, PHYSICALHEIGHT), nLogPixelsX = GetDeviceCaps(hPrinterDC, LOGPIXELSX), nLogPixelsY = GetDeviceCaps(hPrinterDC, LOGPIXELSY); LONG lTextLength; // Longueur total du document LONG lTextPrinted; // Longueur du document edite // Ensure the printer DC is in MM_TEXT mode. SetMapMode ( hPrinterDC, MM_TEXT ); // Rendering to the same DC we are measuring. ZeroMemory(&fr, sizeof(fr)); fr.hdc = fr.hdcTarget = hPrinterDC; // Set up the page. fr.rcPage.left = fr.rcPage.top = 0; fr.rcPage.right = (nHorizRes/nLogPixelsX) * 1440; fr.rcPage.bottom = (nVertRes/nLogPixelsY) * 1440; // Set up 1" margins all around. fr.rc.left = fr.rcPage.left + 570; // 1440; // 1440 TWIPS = 1 inch. fr.rc.top = fr.rcPage.top + 570; 1440; fr.rc.right = fr.rcPage.right - 570; // 1440 fr.rc.bottom = fr.rcPage.bottom - 570; // 1440 // Default the range of text to print as the entire document. fr.chrg.cpMin = 0; fr.chrg.cpMax = -1; // Set up the print job (standard printing stuff here). DOCINFO di; ZeroMemory(&di, sizeof(di)); di.cbSize = sizeof(DOCINFO); di.lpszDocName = "(Untitled)"; // Do not print to file. di.lpszOutput = NULL; // Start the document. StartDoc(hPrinterDC, &di); // Find out re
-
Hello guys, I used to print the content of a RichEditText control with following code. But now I would like to print it in landscape and with paper specifications (12cm x8.5cm). All articles I read are talking about the famous DEVMODE, but I have no clue how to insert this DEVMODE in my existing code. Can you hep me ? Thanks in advance DD void CTestRichEditText::OnPrint() { // 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) return; } else { printDialog.GetDefaults(); } // recently added to try to print in landscape on special paper DEVMODE *MyPrintMode; MyPrintMode = printDialog.GetDevMode(); MyPrintMode->dmFields |= DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH; MyPrintMode->dmOrientation = DMORIENT_LANDSCAPE; MyPrintMode->dmPaperSize = 0; MyPrintMode->dmPaperLength = 120; MyPrintMode->dmPaperWidth = 85; // end of new added code HDC hPrinterDC = printDialog.GetPrinterDC(); FORMATRANGE fr; int nHorizRes = GetDeviceCaps(hPrinterDC, PHYSICALWIDTH), nVertRes = GetDeviceCaps(hPrinterDC, PHYSICALHEIGHT), nLogPixelsX = GetDeviceCaps(hPrinterDC, LOGPIXELSX), nLogPixelsY = GetDeviceCaps(hPrinterDC, LOGPIXELSY); LONG lTextLength; // Longueur total du document LONG lTextPrinted; // Longueur du document edite // Ensure the printer DC is in MM_TEXT mode. SetMapMode ( hPrinterDC, MM_TEXT ); // Rendering to the same DC we are measuring. ZeroMemory(&fr, sizeof(fr)); fr.hdc = fr.hdcTarget = hPrinterDC; // Set up the page. fr.rcPage.left = fr.rcPage.top = 0; fr.rcPage.right = (nHorizRes/nLogPixelsX) * 1440; fr.rcPage.bottom = (nVertRes/nLogPixelsY) * 1440; // Set up 1" margins all around. fr.rc.left = fr.rcPage.left + 570; // 1440; // 1440 TWIPS = 1 inch. fr.rc.top = fr.rcPage.top + 570; 1440; fr.rc.right = fr.rcPage.right - 570; // 1440 fr.rc.bottom = fr.rcPage.bottom - 570; // 1440 // Default the range of text to print as the entire document. fr.chrg.cpMin = 0; fr.chrg.cpMax = -1; // Set up the print job (standard printing stuff here). DOCINFO di; ZeroMemory(&di, sizeof(di)); di.cbSize = sizeof(DOCINFO); di.lpszDocName = "(Untitled)"; // Do not print to file. di.lpszOutput = NULL; // Start the document. StartDoc(hPrinterDC, &di); // Find out re
Something like this should get you away ... Do this to prepare the printer DC before you start printing. (
pDC
is a pointer to the printer DC).CString csPrinterName = printDialog->GetDeviceName();
_TCHAR *szPrinterName = csPrinterName.GetBuffer(0);
DWORD dwRet;
HANDLE hPrinter;if (!OpenPrinter(szPrinterName, &hPrinter, NULL))
return;LPDEVMODE lpDevMode;
dwRet = DocumentProperties(m_hWnd, hPrinter, szPrinterName, NULL, NULL, 0);
lpDevMode = (LPDEVMODE)malloc(dwRet);
dwRet = DocumentProperties(m_hWnd, hPrinter, szPrinterName, lpDevMode, NULL, DM_OUT_BUFFER);
if (dwRet != IDOK)
{
free(lpDevMode);
ClosePrinter(hPrinter);
return;
}
if ((lpDevMode->dmFields & DM_ORIENTATION))
lpDevMode->dmOrientation = DMORIENT_LANDSCAPE;
//
// add in here any other changes to the DEVMODE you need
// (paper size etc.)
//
dwRet = DocumentProperties(m_hWnd, hPrinter, szPrinterName, lpDevMode, lpDevMode, DM_IN_BUFFER|DM_OUT_BUFFER);
ClosePrinter(hPrinter);
if (dwRet != IDOK)
{
free(lpDevMode);
return;
}
pDC->ResetDC(lpDevMode);
free(lpDevMode);
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
-
Something like this should get you away ... Do this to prepare the printer DC before you start printing. (
pDC
is a pointer to the printer DC).CString csPrinterName = printDialog->GetDeviceName();
_TCHAR *szPrinterName = csPrinterName.GetBuffer(0);
DWORD dwRet;
HANDLE hPrinter;if (!OpenPrinter(szPrinterName, &hPrinter, NULL))
return;LPDEVMODE lpDevMode;
dwRet = DocumentProperties(m_hWnd, hPrinter, szPrinterName, NULL, NULL, 0);
lpDevMode = (LPDEVMODE)malloc(dwRet);
dwRet = DocumentProperties(m_hWnd, hPrinter, szPrinterName, lpDevMode, NULL, DM_OUT_BUFFER);
if (dwRet != IDOK)
{
free(lpDevMode);
ClosePrinter(hPrinter);
return;
}
if ((lpDevMode->dmFields & DM_ORIENTATION))
lpDevMode->dmOrientation = DMORIENT_LANDSCAPE;
//
// add in here any other changes to the DEVMODE you need
// (paper size etc.)
//
dwRet = DocumentProperties(m_hWnd, hPrinter, szPrinterName, lpDevMode, lpDevMode, DM_IN_BUFFER|DM_OUT_BUFFER);
ClosePrinter(hPrinter);
if (dwRet != IDOK)
{
free(lpDevMode);
return;
}
pDC->ResetDC(lpDevMode);
free(lpDevMode);
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
Hello, Thanks for your answer but I have some additionnal questions for you : 1) compared to my code, what do you mean by "before you start printing" ? at which moment exactly should I do this ? 2) is the way I set my paper length and height ok? 3) I tried to insert your code in mine, just before my "HDC hPrinterDC = printDialog.CreatePrinterDC();" line and I get the following compil errors : OpenPrinter, DocumentProperties and ClosePrinter are considered as undeclared identifiers ... Do I need an special include or what ? 4) pDC is a pointer to the printer DC ? How do I get this ? 5) "// add in here any other changes to the DEVMODE you need" something like : lpDevMode->dmPaperSize = 0; lpDevMode->dmPaperLength = 120; lpDevMode->dmPaperWidth = 85; and what about the dmFields ? Thanks in advance for extra explanations DD
-
Hello, Thanks for your answer but I have some additionnal questions for you : 1) compared to my code, what do you mean by "before you start printing" ? at which moment exactly should I do this ? 2) is the way I set my paper length and height ok? 3) I tried to insert your code in mine, just before my "HDC hPrinterDC = printDialog.CreatePrinterDC();" line and I get the following compil errors : OpenPrinter, DocumentProperties and ClosePrinter are considered as undeclared identifiers ... Do I need an special include or what ? 4) pDC is a pointer to the printer DC ? How do I get this ? 5) "// add in here any other changes to the DEVMODE you need" something like : lpDevMode->dmPaperSize = 0; lpDevMode->dmPaperLength = 120; lpDevMode->dmPaperWidth = 85; and what about the dmFields ? Thanks in advance for extra explanations DD
My answer was intended to set you on the right track so that you could solve the problem for yourself and learn something aliong the way. I did not and do not intend to write the code for you. However... 1) "before you start printing" means beofre you start outputting anything to the printer DC. 3) The code is intended to prepare the printer DC so include it AFTER you create the printer DC. Work out the rest for yourself. Don't be afraid to press F1 occasionally and read the documentation. It takes a little longer the first time but you will have learned something instead of blindly using code you don't understand.
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
-
My answer was intended to set you on the right track so that you could solve the problem for yourself and learn something aliong the way. I did not and do not intend to write the code for you. However... 1) "before you start printing" means beofre you start outputting anything to the printer DC. 3) The code is intended to prepare the printer DC so include it AFTER you create the printer DC. Work out the rest for yourself. Don't be afraid to press F1 occasionally and read the documentation. It takes a little longer the first time but you will have learned something instead of blindly using code you don't understand.
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
Hello, <> If I asked you for more details, it is simply because I don't see any link between your code and mine and, as it is, it does not help me at all. I don't see how I can use it. Evermore, I received compilation errors. <> It is not what I expected from you, I just need some explanations that I could understand and, if possible, linked to the code I wrote . This will probably help me to understand this function usage <<1) "before you start printing" means before you start outputting anything to the printer DC.>> Is this answer supposed to help me ... really ?? <> Work is what I am doing almost every nights until 2am when I write my (quite big) application after my normal work day. I can't offer me the luxury to loose 2 days in searching and trying without success. <> I spend 2 days to "play" with MSDN documentation and search thru codeproject and codeguru articles to find a basic article that explains how to use this DEVMODE easily in a context that matches my type of program. I also wrote a lot of code to try it. Ok. I can retrive the current DEVMODE, but when I change the content, how do I precise that I want to use it ?? ResetDC is a CDC method, I have no CDC, just a HDC, so ... And it is because I did not find the correct way to use it, that I finally post my question ... <> I am trying to find a solution that suits my code, I am not copying your code without trying to understand it, it is not very funny. Nevertheless, thanks for your time and your example I can't understand. DD