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