Here we go a working version YIPPIE :p

a_jam_sandwich

Well-known member
Joined
Dec 10, 2002
Messages
367
Location
Uk
Help adapted C not working for RTB printing

Complies. Put on form & error;

the user control could not be loaded. Ensure that the library containing the control has been built ...

C#:
namespace de.cortex.text.util
{
	using System;
	using System.Drawing.Printing;
	using System.Runtime.InteropServices;
	using System.Windows.Forms;

	// <doc>
	// <desc>
	//     RichTextPrintDocument prints the content of a RichTextBox
	//     control as RTF formatted text to a printer.
	//     
	//     Usage:
	//         RichTextBox rtb = new RichTextBox();
	//         ... define content of rich text box control
	//         RichTextPrintDocument pd = new RichTextPrintDocument(rtb);
	//         ... define default page settings
	//         ... show standard or custom print dialog
	//         if (dlgResult == DialogResult.OK) 
	//             pd.Print();
	// </desc>
	// </doc>
	public class RichTextPrintDocument : PrintDocument 
	{
		// internal data structures needed for EM_FORMATRANGE message
		// send to RichTextBox control windows handle via SendMessage()
		// The windows message handling is based on code posted by
		// Martin M
 
You might want to check here as this guy appears to have successfully implemented printing.
 
Youre missing the StructLayout attributes from your structures, for a start.
 
Also, you have a load of useless form designer code which you should delete. You have mistaken the constructor in the C# example for a function, your RichTextPrintDocument function should actually be Sub New.
 
right should it be like this ?

Code:
    <StructLayout(LayoutKind.Sequential)> _
    Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure
 
im getting closer

Code:
Imports System
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Namespace RichTextExLib
    Public Class printRTF
        Inherits RichTextBox

        <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 CHARRANGE
            Public cpMin As Integer
            Public cpMax As Integer
        End Structure

        <StructLayout(LayoutKind.Sequential)> _
        Private Structure sFORMATRANGE
            Public hdc As IntPtr
            Public hdcTarget As IntPtr
            Public rc As RECT
            Public rcPage As RECT
            Public chrg As CHARRANGE
        End Structure

        Private Declare Function SendMessage Lib "user32" Alias "SendMessage" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

        Private Const WM_USER As Integer = 1024
        Private Const EM_FORMATRANGE As Integer = 1081
        Private Const EM_SETTARGETDEVICE As Integer = 1096

        Private Function HundredthInchToTwips(ByVal n As Integer) As Integer
            Return CInt(n * 14.4)
        End Function

        Public Function SetTargetDevice(ByVal g As Graphics, ByVal lineLen As Integer) As Boolean
            Dim res As IntPtr
            Dim wpar As IntPtr = g.GetHdc()
            Dim lpar As IntPtr = New IntPtr(HundredthInchToTwips(lineLen))
            res = SendMessage(Handle, EM_SETTARGETDEVICE, wpar, lpar)
            g.ReleaseHdc(wpar)
            Return (Not res.ToInt32() = 0)
        End Function

        Public Function FormatRange(ByVal e As PrintPageEventArgs, ByVal charFrom As Integer, ByVal charTo As Integer) As Integer
            Dim cr As CHARRANGE
            cr.cpMin = charFrom
            cr.cpMax = charTo

            Dim rc As RECT
            rc.Top = HundredthInchToTwips(e.MarginBounds.Top)
            rc.Bottom = HundredthInchToTwips(e.MarginBounds.Bottom)
            rc.Left = HundredthInchToTwips(e.MarginBounds.Left)
            rc.Right = HundredthInchToTwips(e.MarginBounds.Right)

            Dim rcPage As RECT
            rcPage.Top = HundredthInchToTwips(e.PageBounds.Top)
            rcPage.Bottom = HundredthInchToTwips(e.PageBounds.Bottom)
            rcPage.Left = HundredthInchToTwips(e.PageBounds.Left)
            rcPage.Right = HundredthInchToTwips(e.PageBounds.Right)

            Dim hdc As IntPtr = e.Graphics.GetHdc()

            Dim fr As sFORMATRANGE
            fr.chrg = cr
            fr.hdc = hdc
            fr.hdcTarget = hdc
            fr.rc = rc
            fr.rcPage = rcPage

            Dim res As IntPtr
            Dim wpar As IntPtr = New IntPtr(1)
            Dim lpar As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(fr))
            Marshal.StructureToPtr(fr, lpar, False)

            res = CObj(SendMessage(Handle, EM_FORMATRANGE, wpar, lpar))

            Marshal.FreeCoTaskMem(lpar)
            e.Graphics.ReleaseHdc(hdc)

            Return res.ToInt32()
        End Function

        Public Function FormatRangeDone()
            Dim wpar As IntPtr = New IntPtr(0)
            Dim lpar As IntPtr = New IntPtr(0)
            SendMessage(Handle, EM_FORMATRANGE, wpar, lpar)
        End Function

    End Class

End Namespace

Now it doesnt display when you do a print preview no pages to display
 
Project so far for RTB printing in VB.NET

Here is the project as it is so far anyone help ?

Almost had enough of it

Andy

[edit] Removed binaries from attachment - divil [/edit]
 

Attachments

Last edited by a moderator:
You still havent implemented the constructor, like I said.

Code:
Public Sub New(ByVal tRichTextbox As richTextBox)
    richTextBox = tRichTextbox
    textLength = tRichTextbox.TextLength
    firstChar = 0
End Function
 
Here we go a working version YIPPIE :p

It can load multiple files into the class and then you can print preview them all in one go.

Take a look

Thanks to Martin M
 
Wow, that site (page) is attempting to run a ton of ActiveX when loading. Or its stuck in a loop because I disabled ActiveX.

Sorry I cant see it.


[edit]Oh, the thread has been merged, I guess that I can see it now...[/code]
 
Back
Top