N
Neal
Hi,
I saw the VB6 Code to do this at this link:
http://www.dotnet247.com/247reference/msgs/11/56581.aspx
The VB6 Code reads as follows:
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 Declare Function SendMessage Lib "USER32"
Alias "SendMessageA"
_
(ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long,
_
lp As Any) As Long
Public Sub PrintRTF(RTF As RichTextBox, PB As PictureBox)
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 PB.hDC
PB.Cls
PB.ScaleMode = vbTwips
' Calculate the Left, Top, Right, and Bottom margins
' Set printable area rect
rcPage.Left = 0
rcPage.Top = 0
rcPage.Right = PB.ScaleWidth
rcPage.Bottom = PB.ScaleHeight
' Set rect in which to print (relative to printable area)
rcDrawTo.Left = 0
rcDrawTo.Top = 0
rcDrawTo.Right = PB.Width
rcDrawTo.Bottom = PB.Height
' Set up the print instructions
fr.hdc = PB.hdc ' Use the same DC for measuring and
rendering
fr.hdcTarget = PB.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
fr.hdc = PB.hdc
fr.hdcTarget = PB.hdc
Loop
' Allow the RTF to free up memory
r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal
CLng(0))
End Sub
What changes do I need to make to this code to port it to
VB.NET?
Thanks,
Neal
I saw the VB6 Code to do this at this link:
http://www.dotnet247.com/247reference/msgs/11/56581.aspx
The VB6 Code reads as follows:
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 Declare Function SendMessage Lib "USER32"
Alias "SendMessageA"
_
(ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long,
_
lp As Any) As Long
Public Sub PrintRTF(RTF As RichTextBox, PB As PictureBox)
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 PB.hDC
PB.Cls
PB.ScaleMode = vbTwips
' Calculate the Left, Top, Right, and Bottom margins
' Set printable area rect
rcPage.Left = 0
rcPage.Top = 0
rcPage.Right = PB.ScaleWidth
rcPage.Bottom = PB.ScaleHeight
' Set rect in which to print (relative to printable area)
rcDrawTo.Left = 0
rcDrawTo.Top = 0
rcDrawTo.Right = PB.Width
rcDrawTo.Bottom = PB.Height
' Set up the print instructions
fr.hdc = PB.hdc ' Use the same DC for measuring and
rendering
fr.hdcTarget = PB.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
fr.hdc = PB.hdc
fr.hdcTarget = PB.hdc
Loop
' Allow the RTF to free up memory
r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal
CLng(0))
End Sub
What changes do I need to make to this code to port it to
VB.NET?
Thanks,
Neal