tags:

views:

310

answers:

2

Hi,

i would like to print the rich text box contents with formatting to any device context, for example I would like to print on panel or any other control which is associated to actual print device.

I am simulating print preview using panel by drawing some contents from a custom designed form, rich text content is one among the content of that form

is there any best solution to address this??

A: 

below is code that prints an rtf control's contents to the printer. i could be adapted to print to any old dc fairly easy. the language is powerbasic, but it could be easily translated in to C, pascal or whatever:

SUB PrintRichTextBox (  hWnd as LONG, hInst as LONG, rtfEdit as LONG, LM as Single, _
                        RM as Single, TM as Single, BM as Single )
   '
   '  Purpose:
   '           Prints the contents of an RTF text box given it's handle, the
   '           calling program's handle(s), and the page margins.
   '
   '  Parameters:
   '           hWnd     = Parent window (used for print common dlg)
   '           hInst    = Instance of calling program
   '           rtfEdit  = Handle of rich edit control
   '           LM       = Left Margin in inches
   '           RM       = Right Margin in inches
   '           TM       = Top Margin in inches
   '           BM       = Bottom Margin in inches
   '
   Dim fr as FORMATRANGE
   Dim rDocInfo as DOCINFO
   Dim iTextOut as LONG
   Dim iTextAmt as LONG
   Dim pd as PRINTDLGAPI
   Dim zString as ASCIIZ * 200
   Dim iWidthTwips&
   Dim iHeightTwips&

   '- Setup the print common dialog
   pd.lStructSize = len(pd)
   pd.hwndOwner = hWnd
   pd.hDevMode = %NULL
   pd.hDevNames = %NULL
   pd.nFromPage = 0
   pd.nToPage = 0
   pd.nMinPage = 0
   pd.nMaxPage = 0
   pd.nCopies = 0
   pd.hInstance = hInst
   pd.Flags = %PD_RETURNDC or %PD_NOPAGENUMS or %PD_PRINTSETUP
   pd.lpfnSetupHook = %NULL
   pd.lpPrintSetupTemplateName = %NULL
   pd.lpfnPrintHook = %NULL
   pd.lpPrintTemplateName = %NULL

   if PrintDlg(pd) then

      SetCursor LoadCursor( %NULL, BYVAL %IDC_WAIT )

      '- Fill format range structure
      '
      '  NOTE:
      '     This gave me fits. I was looking at the book from
      '     Microsoft Press called Programming the Windows 95
      '     Iterface. It said (via example) that the
      '     Rectagle was defined in Pixels. This didn't work right.
      '     The SDK, however, said the measurements needed to be
      '     in Twips! This seems to work fine.
      '
      '
      fr.hdc = pd.hDC
      fr.hdcTarget = pd.hDC
      fr.chrg.cpMin = 0
      fr.chrg.cpMax = -1

      fr.rc.nTop = TM * 1440
      fr.rcPage.nTop = fr.rc.nTop

      fr.rc.nLeft = LM * 1440
      fr.rcPage.nLeft = fr.rc.nLeft

      '- Get page dimensions in Twips
      iWidthTwips& = int((GetDeviceCaps(pd.hDC, %HORZRES) / GetDeviceCaps(pd.hDC, %LOGPIXELSX)) * 1440)
      iHeightTwips& = int((GetDeviceCaps(pd.hDC, %VERTRES) / GetDeviceCaps(pd.hDC, %LOGPIXELSY)) * 1440)

      fr.rc.nRight = iWidthTwips& - RM * 1440
      fr.rcPage.nRight = fr.rc.nRight

      fr.rc.nBottom = iHeightTwips& - BM * 1440
      fr.rcPage.nBottom = fr.rc.nBottom

      '- Fill rDocInfo structure
      rDocInfo.cbSize = len(rDocInfo)
      zString = "RTF Printer"
      rDocInfo.lpszDocName = VARPTR(zString)
      rDocInfo.lpszOutput = %NULL

      '- Here we go
      StartDoc pd.hDC, rDocInfo
      StartPage pd.hDC

      '- This does the printing. We send messages
      '  to the edit box telling it to format it's
      '  text to fit the Printer's DC.
      '
      iTextOut = 0
      iTextAmt = SendMessage(rtfEdit, %WM_GETTEXTLENGTH, 0, 0)

      do while iTextOut < iTextAmt

         iTextOut = SendMessage(rtfEdit, %EM_FORMATRANGE, _
                     1, VARPTR(fr))

         if iTextOut < iTextAmt then
            EndPage pd.hDC
            StartPage pd.hDC
            fr.chrg.cpMin = iTextOut
            fr.chrg.cpMax = -1
         end if

      loop

      SendMessage rtfEdit, %EM_FORMATRANGE, 1, %NULL

      '- Finish the printing.
      EndPage pd.hDC
      EndDoc pd.hDC

      DeleteDC pd.hDC
      SetCursor LoadCursor( %NULL, BYVAL %IDC_ARROW )

   else
      ' MsgBox "Canceled !"
   end if


END SUB
Don Dickinson
Thanks for giving a source, but I am a C# guy, it's bit difficult to translate to C#.. any help??
KSH
Don Dickinson
A: 

Raymond Chen addressed this question at his blog How do I print the contents of a rich text control?

John Knoeller
thanks for helping, if I need to print some of other contents(say example a logo of the page which is kept in an image box) on top of the page/dc and then the rich text box control contents will it be possible?
KSH