CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2007
    Posts
    4

    CRichEditCtrl Version 2.0

    Hi,

    I am trying to print the content of the CRichEditCtrl v 2.0. The problem is that when I want to use pagination (with A4 pages), the printed text (which is just 4 sample line with 40 chars at most) at the code lTextPrinted =FormatRange(&fr,TRUE);
    is always lesser than the actual text pointed by the lTextLength variable making the loop run for ever. Please notice that using the RichEditControl Version 1 it works fine.



    Check the code. I am attaching also the full source code at
    https://rapidshare.com/#!download|17...lg.zip|140|0|0
    RichEditDlg.zip


    Code:
     
    	CPrintDialog printDialog(false);
    	
    	
    	if (bShowPrintDialog)
    	{
    		int r = printDialog.DoModal();
    		if (r == IDCANCEL)
    			return; // User pressed cancel, don't print.
    	}
    	else
    	{
    		printDialog.GetDefaults();
    	}
     
    	HDC hPrinterDC = printDialog.GetPrinterDC();
    	
    	CDC pMyPrinterDC; 
    	pMyPrinterDC.Attach(hPrinterDC);
     
    
     
    
     
    	// This code basically taken from MS KB article Q129860 
    	
    	FORMATRANGE fr;
    	  int	  nHorizRes = GetDeviceCaps(hPrinterDC, HORZRES);
    	  int	  nVertRes = GetDeviceCaps(hPrinterDC, VERTRES);
          int     nLogPixelsX = GetDeviceCaps(hPrinterDC, LOGPIXELSX);
    	  int     nLogPixelsY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);
    	  LONG	  lTextLength;   // Length of document.
    	  LONG	  lTextPrinted;  // Amount of document printed.
    
    	 // 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 = pMyPrinterDC.m_hDC;
    	  fr.hdcTarget = pMyPrinterDC.m_hDC;
     
    	  // Get the page width and height from the printer.
    	  long lPageWidth =  ::MulDiv(pMyPrinterDC.GetDeviceCaps(PHYSICALWIDTH), 1440, pMyPrinterDC.GetDeviceCaps(LOGPIXELSX));
    	  long lPageHeight = ::MulDiv(pMyPrinterDC.GetDeviceCaps(PHYSICALHEIGHT), 1440, pMyPrinterDC.GetDeviceCaps(LOGPIXELSY));
    	  CRect rcPage(0, 0, lPageWidth, lPageHeight);
     
    	  SetTargetDevice(hPrinterDC,lPageWidth);
     
    	  fr.rc = rcPage;
    	  fr.rcPage = rcPage;
     
    
    	 // Default the range of text to print as the entire document.
    	  fr.chrg.cpMin = 0;
    	  fr.chrg.cpMax = -1;
          this->FormatRange(&fr,TRUE);      
     
    	 // Set up the print job (standard printing stuff here).
    	  DOCINFO di;
    	  ZeroMemory(&di, sizeof(di));
    	  di.cbSize = sizeof(DOCINFO);
     
    	  di.lpszDocName = "GnosisExplorerNotes";
     
    	  // Do not print to file.
    	  di.lpszOutput = NULL;
    	     
    	// Update the display with the new formatting.
    	  RECT rcClient;
          GetClientRect(&rcClient);
          DisplayBand(&rcClient); 
     
    	 // Start the document.
    	  StartDoc(hPrinterDC, &di);
     
    	 // Find out real size of document in characters.
    	 lTextLength = GetTextLength();//SendMessage ( /*hRTFWnd,*/ WM_GETTEXTLENGTH, 0, 0 );
    	 //lTextLength = ::SendMessage (this->m_hWnd, WM_GETTEXTLENGTH, 0, 0); 
    
    	 do {
    	     // Start the page.
    	     StartPage(hPrinterDC);
     
    	    // Print as much text as can fit on a page. The return value is
    	     // the index of the first character on the next page. Using TRUE
    	     // for the wParam parameter causes the text to be printed.
    
    		//lTextPrinted = ::SendMessage (this->m_hWnd, EM_FORMATRANGE, FALSE, (LPARAM)&fr); 	    
    		lTextPrinted =FormatRange(&fr,TRUE);
    		
    		DisplayBand(&fr.rc );
    	    
    	    // Print last page.
    	    EndPage(hPrinterDC);
     
    	    // If there is more text to print, adjust the range of characters
    	     // to start printing at the first character of the next page.
    		if (lTextPrinted < lTextLength)	{
    			fr.chrg.cpMin = lTextPrinted;
    			fr.chrg.cpMax = -1;
    		}
     
    	  } while (lTextPrinted < lTextLength);
     
    	 // Tell the control to release cached information.
    	  FormatRange(NULL,false);
    	 //SendMessage( EM_FORMATRANGE, 0, (LPARAM)NULL);
    
    	 EndDoc (hPrinterDC);
     
    	  ///////
    	  
    	DeleteDC(hPrinterDC);
    	pMyPrinterDC.DeleteDC();
    Last edited by Marc G; October 9th, 2012 at 01:58 AM. Reason: Added code tags

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: CRichEditCtrl Version 2.0

    [ Added code tags ]
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Jun 2007
    Posts
    4

    Re: CRichEditCtrl Version 2.0

    The solution was given by Andrew Cherednik at the codeproject forums. The problem was the

    lTextLength = GetTextLength();

    that it should be replaced with the

    lTextLength = GetTextLengthEx(GTL_PRECISE | GTL_NUMCHARS);

    in case of RichEditControl version 2.0.

    Regards,
    George

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured