Newsgroup: it.comp.lang.visual-basic
Titolo: Stampare RichTextBox centrato su un foglio A4

Ciao a tutt,
è da un pò che sbatto la testa con l'oggetto del post... e non sono
riuscito ad arrivare a nessuna conclusione positiva. :(
Io ho una RichTextBox che carico con RTBox.Filename = App.Path &
"\prova.rtf" e quando vado per stamparla tramite RTBox.SelPrint
(Printer.hDC), me la stampa tutta in alto e a sinistra, io per ragioni
estetiche ho bisogno di stamparla centrata nella pagina, quindi sia
verticalmente che orizzontalmente.
Ho provato a cercare prima di postare ma non ho trovato niente di
buono....chi mi aiuta?
Grazie mille per la solita collaborazione.
Ciao Davide.

vedi se ti può essere utile la routine di stampa del mio texteditor dal 
sito comune.
http://www.it-lang-vb.net/archivio.asp?subMenu=Tutte&FullText=on&TypeRic=AND&keyword=texteditor&x=0&y=0
Nicola

Nicola Ottomano ha scritto:

No, mi correggo, in quella versione del texteditor non c'è un modulo che 
uso in un'altra versione.
Il modulo è questo:
Option Explicit
Private Type Rect
     Left As Long
     Top As Long
     Right As Long
     Bottom As Long
End Type
Private Type CharRange
     cpMin As Long    ' First character of range (0 for start of doc)
     cpMax As Long    ' Last character of range (-1 for end of doc)
End Type
Private Type FormatRange
     hDC As Long    ' Actual DC to draw on
     hdcTarget As Long    ' Target DC for determining text formatting
     rc As Rect    ' Region of the DC to draw to (in twips)
     rcPage As Rect    ' Region of the entire DC (page size) (in twips)
     chrg As CharRange    ' Range of text to draw (see above declaration)
End Type
Private Const WM_USER As Long = &H400
Private Const EM_FORMATRANGE As Long = WM_USER + 57
Private Const EM_SETTARGETDEVICE As Long = WM_USER + 72
Private Const PHYSICALOFFSETX As Long = 112
Private Const PHYSICALOFFSETY As Long = 113
Private Declare Function GetDeviceCaps Lib "gdi32" ( _
         ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
         (ByVal hwnd As Long, ByVal MSG As Long, ByVal wp As Long, _
         lp As Any) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" _
         (ByVal lpDriverName As String, ByVal lpDeviceName As String, _
         ByVal lpOutput As Long, ByVal lpInitData As Long) As Long
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' WYSIWYG_RTF - Sets an RTF control to display itself the same as it
'               would print on the default printer
'
' RTF - A RichTextBox control to set for WYSIWYG display.
'
' LeftMarginWidth - Width of desired left margin in twips
'
' RightMarginWidth - Width of desired right margin in twips
'
' Returns - The length of a line on the printer in twips
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub WYSIWYG_RTF(RTF As RichTextBox, LeftMarginWidth As Long, 
RightMarginWidth As Long, TopMarginWidth As Long, BottomMarginWidth As 
Long, PrintableWidth As Long, PrintableHeight As Long)
     Dim LeftOffset As Long
     Dim LeftMargin As Long
     Dim RightMargin As Long
     Dim TopOffset As Long
     Dim TopMargin As Long
     Dim BottomMargin As Long
     Dim PrinterhDC As Long
     Dim r As Long
     ' Start a print job to initialize printer object
     Printer.Print Space(1)
     Printer.ScaleMode = vbTwips
     ' Get the left offset to the printable area on the page in twips
     LeftOffset = GetDeviceCaps(Printer.hDC, PHYSICALOFFSETX)
     LeftOffset = Printer.ScaleX(LeftOffset, vbPixels, vbTwips)
     ' Calculate the Left, and Right margins
     LeftMargin = LeftMarginWidth - LeftOffset
     RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset
     ' Calculate the line width
     PrintableWidth = RightMargin - LeftMargin
     ' Get the top offset to the printable area on the page in twips
     TopOffset = GetDeviceCaps(Printer.hDC, PHYSICALOFFSETY)
     TopOffset = Printer.ScaleX(TopOffset, vbPixels, vbTwips)
     ' Calculate the Left, and Right margins
     TopMargin = TopMarginWidth - TopOffset
     BottomMargin = (Printer.Height - BottomMarginWidth) - TopOffset
     ' Calculate the line width
     PrintableHeight = BottomMargin - TopMargin
     ' Create an hDC on the Printer pointed to by the Printer object
     ' This DC needs to remain for the RTF to keep up the WYSIWYG display
     PrinterhDC = CreateDC(Printer.DriverName, Printer.DeviceName, 0, 0)
     ' Tell the RTF to base it's display off of the printer
     '    at the desired line width
     r = SendMessage(RTF.hwnd, EM_SETTARGETDEVICE, PrinterhDC, _
             ByVal PrintableWidth)
     ' Abort the temporary print job used to get printer info
     Printer.KillDoc
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' PrintRTF - Prints the contents of a RichTextBox control using the
'            provided margins
'
' RTF - A RichTextBox control to print
'
' LeftMarginWidth - Width of desired left margin in twips
'
' TopMarginHeight - Height of desired top margin in twips
'
' RightMarginWidth - Width of desired right margin in twips
'
' BottomMarginHeight - Height of desired bottom margin in twips
'
' Notes - If you are also using WYSIWYG_RTF() on the provided RTF
'         parameter you should specify the same LeftMarginWidth and
'         RightMarginWidth that you used to call WYSIWYG_RTF()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long, _
    TopMarginHeight, RightMarginWidth, BottomMarginHeight)
     Dim LeftOffset As Long, TopOffset As Long
     Dim LeftMargin As Long, TopMargin As Long
     Dim RightMargin As Long, BottomMargin As Long
     Dim fr As FormatRange
     Dim rcDrawTo As Rect
     Dim rcPage As Rect
     Dim TextLength As Long
     Dim NextCharPosition As Long
     Dim r As Long
     ' Start a print job to get a valid Printer.hDC
     Printer.Print Space(1)
     Printer.ScaleMode = vbTwips
     ' Get the offsett to the printable area on the page in twips
     LeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hDC, _
             PHYSICALOFFSETX), vbPixels, vbTwips)
     TopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hDC, _
             PHYSICALOFFSETY), vbPixels, vbTwips)
     ' Calculate the Left, Top, Right, and Bottom margins
     LeftMargin = LeftMarginWidth - LeftOffset
     TopMargin = TopMarginHeight - TopOffset
     RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset
     BottomMargin = (Printer.Height - BottomMarginHeight) - TopOffset
     ' Set printable area rect
     rcPage.Left = 0
     rcPage.Top = 0
     rcPage.Right = Printer.ScaleWidth
     rcPage.Bottom = Printer.ScaleHeight
     ' Set rect in which to print (relative to printable area)
     rcDrawTo.Left = LeftMargin
     rcDrawTo.Top = TopMargin
     rcDrawTo.Right = RightMargin
     rcDrawTo.Bottom = BottomMargin
     ' Set up the print instructions
     fr.hDC = Printer.hDC    ' Use the same DC for measuring and rendering
     fr.hdcTarget = Printer.hDC    ' Point at printer hDC
     fr.rc = rcDrawTo    ' Indicate the area on page to draw to
     fr.rcPage = rcPage    ' Indicate entire size of page
     fr.chrg.cpMin = 0    ' Indicate start of text through
     fr.chrg.cpMax = -1    ' end of the text
     ' Get length of text in RTF
     TextLength = Len(RTF.Text)
     ' Loop printing each page until done
     Do
         ' Print the page by sending EM_FORMATRANGE message
         NextCharPosition = SendMessage(RTF.hwnd, EM_FORMATRANGE, True, fr)
         If NextCharPosition >= TextLength Then Exit Do    'If done then 
exit
         fr.chrg.cpMin = NextCharPosition    ' Starting position for 
next page
         Printer.NewPage    ' Move on to next page
         Printer.Print Space(1)    ' Re-initialize hDC
         fr.hDC = Printer.hDC
         fr.hdcTarget = Printer.hDC
     Loop
     ' Commit the print job
     Printer.EndDoc
     ' Allow the RTF to free up memory
     r = SendMessage(RTF.hwnd, EM_FORMATRANGE, False, ByVal CLng(0))
End Sub

 >
http://www.it-lang-vb.net/archivio.asp?subMenu=Tutte&FullText=on&Type.
..

Grazie mille per il codice, adesso provo a implementarlo e poi ti
faccio sapere.
Un'ultima cosa....quale delle due Sub devo poi richiamare quando
voglio stampare?
Grazie,
Davide