473,594 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to print out RTF text into a PictureBox in VB.NET

Hi,

I saw the VB6 Code to do this at this link:

http://www.dotnet247.com/247referenc.../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 "SendMessag eA"
_
(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 NextCharPositio n 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
NextCharPositio n = SendMessage(RTF .hWnd, EM_FORMATRANGE,
True,
fr)
If NextCharPositio n >= TextLength Then Exit Do 'If done
then
exit
fr.chrg.cpMin = NextCharPositio n ' 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
Nov 20 '05 #1
16 9312
for VB.NET or C#, you require to use GDI+ to draw on picture box.

--
Dhaval Faria
Founder, Programmer
Hirdhav (http://www.hirdhav.com)
Microsoft India Community Star
"Neal" <ne**@milsys.co m> wrote in message
news:04******** *************** *****@phx.gbl.. .
Hi,

I saw the VB6 Code to do this at this link:

http://www.dotnet247.com/247referenc.../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 "SendMessag eA"
_
(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 NextCharPositio n 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
NextCharPositio n = SendMessage(RTF .hWnd, EM_FORMATRANGE,
True,
fr)
If NextCharPositio n >= TextLength Then Exit Do 'If done
then
exit
fr.chrg.cpMin = NextCharPositio n ' 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

Nov 20 '05 #2
To be more specific. Use the CreateGraphics method of the form to allow you
to draw on it.

Dim g as Graphics = FormName.Create Graphics()

Regards - OHM

Dhaval Faria wrote:
for VB.NET or C#, you require to use GDI+ to draw on picture box.
"Neal" <ne**@milsys.co m> wrote in message
news:04******** *************** *****@phx.gbl.. .
Hi,

I saw the VB6 Code to do this at this link:

http://www.dotnet247.com/247referenc.../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 "SendMessag eA"
_
(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 NextCharPositio n 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
NextCharPositio n = SendMessage(RTF .hWnd, EM_FORMATRANGE,
True,
fr)
If NextCharPositio n >= TextLength Then Exit Do 'If done
then
exit
fr.chrg.cpMin = NextCharPositio n ' 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


--
Best Regards - OHM

O_H_M{at}BTInte rnet{dot}com
Nov 20 '05 #3
Hi,

Here you go.
Declare Function SendMessage Lib "user32" Alias "SendMessag eA" _

(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, _

ByVal lParam As Integer) As Integer

Declare Function SendMessage Lib "user32" Alias "SendMessag eA" _

(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, _

ByRef lParam As FormatRange) As Integer

Public Structure Rect

Dim Left As Integer

Dim Top As Integer

Dim Right As Integer

Dim Bottom As Integer

End Structure

Public Structure CharRange

Dim cpMin As Integer ' First character of range (0 for start of doc)

Dim cpMax As Integer ' Last character of range (-1 for end of doc)

End Structure

Public Structure FormatRange

Dim hdc As Integer ' Actual DC to draw on

Dim hdcTarget As Integer ' Target DC for determining text formatting

Dim rc As Rect ' Region of the DC to draw to (in twips)

Dim rcPage As Rect ' Region of the entire DC (page size) (in twips)

Dim chrg As CharRange ' Range of text to draw (see above declaration)

End Structure

Private Const WM_USER As Integer = &H400S

Private Const EM_FORMATRANGE As Integer = WM_USER + 57

Public Sub PrintRTF(ByRef RTF As RichTextBox, ByRef PB As
System.Windows. Forms.PictureBo x)

Dim fr As FormatRange

Dim rcDrawTo As Rect

Dim rcPage As Rect

Dim TextLength As Integer

Dim NextCharPositio n As Integer

Dim r As Integer

Dim g As Graphics = PB.CreateGraphi cs

g.Clear(Color.W hite)

Dim hdc As IntPtr = g.GetHdc

' Start a print job to get a valid PB.hDC

rcPage.Left = 0

rcPage.Top = 0

rcPage.Right = PB.ClientRectan gle.Width

rcPage.Bottom = PB.ClientRectan gle.Height

' Set rect in which to print (relative to printable area)

rcDrawTo.Left = 0

rcDrawTo.Top = 0

rcDrawTo.Right = VB6.PixelsToTwi psX(PB.Width)

rcDrawTo.Bottom = VB6.PixelsToTwi psY(PB.Height)

' Set up the print instructions

fr.hdc = hdc.ToInt32 ' Use the same DC for measuring and rendering

fr.hdcTarget = hdc.ToInt32 ' 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 = RTF.Text.Length

' Loop printing each page until done

Do

' Print the page by sending EM_FORMATRANGE message

NextCharPositio n = SendMessage(RTF .Handle, EM_FORMATRANGE, True, fr)

If NextCharPositio n >= TextLength Then Exit Do

fr.chrg.cpMin = NextCharPositio n ' Starting position for next page()

fr.hdc = hdc.ToInt32

fr.hdcTarget = hdc.ToInt32

Loop

' Allow the RTF to free up memory

r = SendMessage(RTF .Handle, EM_FORMATRANGE, False, 0)

g.ReleaseHdc(hd c)

End Sub

Ken

-----------------------------
Nov 20 '05 #4
Dim g As Graphics

g = PictureBox1.Cre ateGraphics

Dim drawString As [String] = "Sample Text"
' Create font and brush.
Dim drawFont As New Font("Arial", 16)
Dim drawBrush As New SolidBrush(Colo r.Black)
' Create point for upper-left corner of drawing.
Dim x As Single = 50.0F
Dim y As Single = 50.0F
' Set format of string.
Dim drawFormat As New StringFormat
drawFormat.Form atFlags = StringFormatFla gs.DirectionRig htToLeft
' Draw string to screen.
Nov 20 '05 #5
* "Neal" <ne**@milsys.co m> scripsit:
I saw the VB6 Code to do this at this link:

http://www.dotnet247.com/247referenc.../11/56581.aspx

The VB6 Code reads as follows:


You will find a VB.NET implementation for setting up WYSIWYG print in a
RichTextBox here:

Getting WYSIWYG Print Results from a .NET RichTextBox
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/dnwinforms/html/wnf_RichTextBox .asp>

Maybe you can bring the wto solutions together.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
Cor
Hi Herfried,

I saw a very nice sample two lines above, I asume OHM did test it, so it is
in my HKW box with the addition "made by OHM"

Cor
Nov 20 '05 #7
+

g.DrawString(dr awString, drawFont, drawBrush, x, y, drawFormat)

Regards - OHM
One Handed Man [ OHM# ] wrote:
Dim g As Graphics

g = PictureBox1.Cre ateGraphics

Dim drawString As [String] = "Sample Text"
' Create font and brush.
Dim drawFont As New Font("Arial", 16)
Dim drawBrush As New SolidBrush(Colo r.Black)
' Create point for upper-left corner of drawing.
Dim x As Single = 50.0F
Dim y As Single = 50.0F
' Set format of string.
Dim drawFormat As New StringFormat
drawFormat.Form atFlags =
StringFormatFla gs.DirectionRig htToLeft ' Draw string to
screen.


--
Best Regards - OHM

O_H_M{at}BTInte rnet{dot}com
Nov 20 '05 #8
* "Cor" <no*@non.com> scripsit:
I saw a very nice sample two lines above, I asume OHM did test it, so it is
in my HKW box with the addition "made by OHM"


Yep. The sample will work, nevertheless the RichTextBox provides some
really nice features for formatting/... text and other content.

;-)

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #9
Cor
Hi Herfried,

Can you be a little bit precise because till now I never used the RTB?

The sample you did send looks for me more to C++ than to VB.net

To more precise, do you have one or two real managed code rows that replaces
the sample of OHM?

Cor
Yep. The sample will work, nevertheless the RichTextBox provides some
really nice features for formatting/... text and other content.


Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
12120
by: Mario | last post by:
I could you print jpg or bmp from the printer commands I tried something like ; Printer.picturepaint ("92.bmp") printer.enddoc Doesn't work.. I want to be able to print a logo or a picture in a page with my other printer.print statements.. is that possible ?
4
2213
by: Chris | last post by:
Hi, I found this code that can solve a problem I have. It sends print direct to printer. Imports System Imports System.Text Imports System.Runtime.InteropServices <StructLayout(LayoutKind.Sequential)> _ Public Structure DOCINFO<MarshalAs(UnmanagedType.LPWStr)>
1
11360
by: VJ | last post by:
I have a sample code.. below with my Print problem... I am not able to get the Label on Image print at the right location.. VJ private Label label4 = new Label(); Form1's Load... label4.BackColor=Color.White; label4.BorderStyle=BorderStyle.FixedSingle;
3
3117
by: **Developer** | last post by:
I need to paste an RTF non-ANSI string into a RichTextBox. I know how to do that if the string were all ANSI characters. But the text contains Unicode characters that have no ANSI equivalent. I don't know how to include them. Can you help?
0
960
by: Neal | last post by:
Hi, I have been trying to get various types of data to print out on a single report. The following kinds of data are to be included: 1) A company logo 2) Company info, based on an RTF file storing that data. 3) Detail info, based on a different RTF file with that data.
3
1509
by: active | last post by:
I draw text in a bitmap and then draw the bitmap on a picturebox and get text that is not all displayed the same. Note the different text style in the (40, 100) area. Got any insight into what is happening? Cal
3
2248
by: Neal Miller | last post by:
Hi Thanks to all of your help, I've got a working PrintRTF module that prints out RTF code, and it works well. The next issue to overcome is to determine how to page-break text that is too long for one page Is there some way to identify exactly how many characters I can print before I run out of room in the current rectangle that defines the output, and do to a new page. A VB.NET code sample with a loop to handle multiple pages would be...
2
4431
by: Christie | last post by:
In my application, lines (shapes such as circles) and text are drawn in a picturebox. I am struggling to find a way to print the text and shapes. There is no images in the picturebox. Could someone please show me the way. I am using Visual Studio 2005 (VB.Net) Operating System: XP Pro Thanks
24
2825
by: Tony Girgenti | last post by:
Hello. Developing a Windows Form program in VS.NET VB, .NET Framework 1.1.4322 on a windows XP Pro, SP2. Before printing a document, i want to set the font to a font that is only available with the printer that i am printing to(Zebra TLP2844). When i open Word and look at the fonts available for the default printer, it does not show the fonts i want. If i cahnge the printer to the printer that
0
7876
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8003
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8234
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5408
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3859
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3897
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1478
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1210
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.