But how do I go about making a button that, when pressed make the RTF box in my application print out.
please be detailed or link me to somewhere that is...as I'm still not that great at VB.net
Thanks in advance to anyone who answers my call.
бо Posted by Nobugz
бо http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1044135&SiteID=1
Imports System
Imports System.Text
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class RichTextBoxPrinter
'--- P/Invoke declarations
<StructLayout(LayoutKind.Sequential)> _
private structure CHARRANGE
Public cpMin As Integer
Public cpMax As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure FORMATRANGE
Public hdc As IntPtr
Public hdcTarget As IntPtr
Public rc As RECT
Public rcPage As RECT
Public chrg As CHARRANGE
End Structure
Private Const WM_USER As Integer = &H400
private Const EM_FORMATRANGE As Integer = WM_USER + 57
Private Const Hundredth2Twips As Integer = 20 * 72 \ 100
Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
Public Shared Function Print(box As RichTextBox, ByRef charFrom As Integer, e As PrintPageEventArgs) As Boolean
'--- Prints text in <box>, starting at <charFrom>. Returns <true> if more pages are needed
If box.Text.Length = 0 Then Return False
Dim fmtRange As FORMATRANGE
'--- Allocate device context for output device
Dim hdc As IntPtr = e.Graphics.GetHdc()
fmtRange.hdc = hdc
fmtRange.hdcTarget = hdc
'--- Set printable area, converted from 0.01" to twips
fmtRange.rc.Top = Convert.ToInt32(e.MarginBounds.Top * Hundredth2Twips)
fmtRange.rc.Bottom = Convert.ToInt32(e.MarginBounds.Bottom * Hundredth2Twips)
fmtRange.rc.Left = Convert.ToInt32(e.MarginBounds.Left * Hundredth2Twips)
fmtRange.rc.Right = Convert.ToInt32(e.MarginBounds.Right * Hundredth2Twips)
'--- Set page area, converted from 0.01" to twips
fmtRange.rcPage.Top = Convert.ToInt32(e.PageBounds.Top * Hundredth2Twips)
fmtRange.rcPage.Bottom = Convert.ToInt32(e.PageBounds.Bottom * Hundredth2Twips)
fmtRange.rcPage.Left = Convert.ToInt32(e.PageBounds.Left * Hundredth2Twips)
fmtRange.rcPage.Right = Convert.ToInt32(e.PageBounds.Right * Hundredth2Twips)
'--- Set character range to print
fmtRange.chrg.cpMin = charFrom
fmtRange.chrg.cpMax = box.TextLength
'--- Marshal to unmanaged memory
Dim hdlRange As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
Marshal.StructureToPtr(fmtRange, hdlRange, False)
'--- Send RichTextBox the EM_FORMATRANGE message to print the text
Dim res As IntPtr = SendMessage(box.Handle, EM_FORMATRANGE, CType(1, IntPtr), hdlRange)
Dim err As Integer = Marshal.GetLastWin32Error()
'--- Release resources
Marshal.FreeCoTaskMem(hdlRange)
e.Graphics.ReleaseHdc(hdc)
'--- Throw exception on error so we don't endlessly print an empty page
if (res = IntPtr.Zero) then throw new ApplicationException(string.Format("Printing failed, error code={0}", err))
'--- Update <charFrom> to next character to print, return <true> if more pages needed
charFrom = res.ToInt32()
return charFrom < box.TextLength
End Function
End Class
Now drop a RichTextBox, a PrintPreviewDialog, a PrintDocument and a button control onto your form. Paste this code into the form:
Public Class Form1
Private mCharFrom As Integer
Public Sub New()
InitializeComponent()
Me.PrintPreviewDialog1.Document = Me.PrintDocument1
End Sub
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
mCharFrom = 0
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
e.HasMorePages = RichTextBoxPrinter.Print(RichTextBox1, mCharFrom, e)
Catch ex As Exception
MessageBox.Show(ex.Message, "Printing failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
e.Cancel = True
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintPreviewDialog1.ShowDialog()
End Sub
End Class
I did as you said above and got 8 error messages.
The first one refers to:
InitializeComponent()
Error 1 Reference to a non-shared member requires an object reference. C:\Documents and Settings\Lowell\Local Settings\Application Data\Temporary Projects\PrintRTFDoc\Form1.vb 82 13 PrintRTFDoc
This is also the first time I have posted anything, so if you could please solve the first error I would appreciate it. Then we can work on the others after I find out how the forum works.
Here are the other errors:
Error 2 'PrintPreviewDialog1' is not a member of 'PrintRTFDoc.Form1.Form1'. C:\Documents and Settings\Lowell\Local Settings\Application Data\Temporary Projects\PrintRTFDoc\Form1.vb 83 13 PrintRTFDoc
Error 3 'PrintDocument1' is not a member of 'PrintRTFDoc.Form1.Form1'. C:\Documents and Settings\Lowell\Local Settings\Application Data\Temporary Projects\PrintRTFDoc\Form1.vb 83 47 PrintRTFDoc
Error 4 Handles clause requires a WithEvents variable defined in the containing type or one of its base types. C:\Documents and Settings\Lowell\Local Settings\Application Data\Temporary Projects\PrintRTFDoc\Form1.vb 85 130 PrintRTFDoc
Error 5 Handles clause requires a WithEvents variable defined in the containing type or one of its base types. C:\Documents and Settings\Lowell\Local Settings\Application Data\Temporary Projects\PrintRTFDoc\Form1.vb 88 140 PrintRTFDoc
Error 6 Reference to a non-shared member requires an object reference. C:\Documents and Settings\Lowell\Local Settings\Application Data\Temporary Projects\PrintRTFDoc\Form1.vb 90 59 PrintRTFDoc
Error 7 Handles clause requires a WithEvents variable defined in the containing type or one of its base types. C:\Documents and Settings\Lowell\Local Settings\Application Data\Temporary Projects\PrintRTFDoc\Form1.vb 96 103 PrintRTFDoc
Error 8 Reference to a non-shared member requires an object reference. C:\Documents and Settings\Lowell\Local Settings\Application Data\Temporary Projects\PrintRTFDoc\Form1.vb 97 13 PrintRTFDoc