Results 1 to 9 of 9

Thread: [RESOLVED] [2008] Print out font's and colours with richtextbox

  1. #1

    Thread Starter
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    Resolved [RESOLVED] [2008] Print out font's and colours with richtextbox

    I can't find out how to print out font's and colours with the richtextbox.
    I can just print out small black font...
    I use the font dialog box.

    Can anyone help me?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: [2008] Print out font's and colours with richtextbox

    heres an extended richtextbox control with rtf printing support built in.

    vb.net Code:
    1. Imports System
    2. Imports System.Windows.Forms
    3. Imports System.Drawing
    4. Imports System.Runtime.InteropServices
    5. Imports System.Drawing.Printing
    6.  
    7.  
    8. Public Class myRichTextBoxEx
    9.     Inherits RichTextBox
    10.  
    11.     <StructLayout(LayoutKind.Sequential)> _
    12.         Private Structure STRUCT_RECT
    13.         Public left As Int32
    14.         Public top As Int32
    15.         Public right As Int32
    16.         Public bottom As Int32
    17.     End Structure
    18.  
    19.     <StructLayout(LayoutKind.Sequential)> _
    20.     Private Structure STRUCT_CHARRANGE
    21.         Public cpMin As Int32
    22.         Public cpMax As Int32
    23.     End Structure
    24.  
    25.     <StructLayout(LayoutKind.Sequential)> _
    26.     Private Structure STRUCT_FORMATRANGE
    27.         Public hdc As IntPtr
    28.         Public hdcTarget As IntPtr
    29.         Public rc As STRUCT_RECT
    30.         Public rcPage As STRUCT_RECT
    31.         Public chrg As STRUCT_CHARRANGE
    32.     End Structure
    33.  
    34.     <StructLayout(LayoutKind.Sequential)> _
    35.     Private Structure STRUCT_CHARFORMAT
    36.         Public cbSize As Integer
    37.         Public dwMask As UInt32
    38.         Public dwEffects As UInt32
    39.         Public yHeight As Int32
    40.         Public yOffset As Int32
    41.         Public crTextColor As Int32
    42.         Public bCharSet As Byte
    43.         Public bPitchAndFamily As Byte
    44.         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
    45.         Public szFaceName As Char()
    46.     End Structure
    47.  
    48.     <DllImport("user32.dll")> _
    49.     Private Shared Function SendMessage(ByVal hWnd As IntPtr, _
    50.                                     ByVal msg As Int32, _
    51.                                     ByVal wParam As Int32, _
    52.                                     ByVal lParam As IntPtr) As Int32
    53.     End Function
    54.  
    55.     Private Const WM_USER As Int32 = &H400&
    56.     Private Const EM_FORMATRANGE As Int32 = WM_USER + 57
    57.  
    58.     ' Calculate or render the contents of our RichTextBox for printing
    59.     '
    60.     ' Parameter "measureOnly": If true, only the calculation is performed,
    61.     '                          otherwise the text is rendered as well
    62.     ' Parameter "e": The PrintPageEventArgs object from the PrintPage event
    63.     ' Parameter "charFrom": Index of first character to be printed
    64.     ' Parameter "charTo": Index of last character to be printed
    65.     ' Return value: (Index of last character that fitted on the page) + 1
    66.     Public Function FormatRange(ByVal measureOnly As Boolean, _
    67.                                 ByVal e As PrintPageEventArgs, _
    68.                                 ByVal charFrom As Integer, _
    69.                                 ByVal charTo As Integer) As Integer
    70.         ' Specify which characters to print
    71.         Dim cr As STRUCT_CHARRANGE
    72.         cr.cpMin = charFrom
    73.         cr.cpMax = charTo
    74.  
    75.         ' Specify the area inside page margins
    76.         Dim rc As STRUCT_RECT
    77.         rc.top = HundredthInchToTwips(e.MarginBounds.Top)
    78.         rc.bottom = HundredthInchToTwips(e.MarginBounds.Bottom)
    79.         rc.left = HundredthInchToTwips(e.MarginBounds.Left)
    80.         rc.right = HundredthInchToTwips(e.MarginBounds.Right)
    81.  
    82.         ' Specify the page area
    83.         Dim rcPage As STRUCT_RECT
    84.         rcPage.top = HundredthInchToTwips(e.PageBounds.Top)
    85.         rcPage.bottom = HundredthInchToTwips(e.PageBounds.Bottom)
    86.         rcPage.left = HundredthInchToTwips(e.PageBounds.Left)
    87.         rcPage.right = HundredthInchToTwips(e.PageBounds.Right)
    88.  
    89.         ' Get device context of output device
    90.         Dim hdc As IntPtr
    91.         hdc = e.Graphics.GetHdc()
    92.  
    93.         ' Fill in the FORMATRANGE structure
    94.         Dim fr As STRUCT_FORMATRANGE
    95.         fr.chrg = cr
    96.         fr.hdc = hdc
    97.         fr.hdcTarget = hdc
    98.         fr.rc = rc
    99.         fr.rcPage = rcPage
    100.  
    101.         ' Non-Zero wParam means render, Zero means measure
    102.         Dim wParam As Int32
    103.         If measureOnly Then
    104.             wParam = 0
    105.         Else
    106.             wParam = 1
    107.         End If
    108.  
    109.         ' Allocate memory for the FORMATRANGE struct and
    110.         ' copy the contents of our struct to this memory
    111.         Dim lParam As IntPtr
    112.         lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fr))
    113.         Marshal.StructureToPtr(fr, lParam, False)
    114.  
    115.         ' Send the actual Win32 message
    116.         Dim res As Integer
    117.         res = SendMessage(Handle, EM_FORMATRANGE, wParam, lParam)
    118.  
    119.         ' Free allocated memory
    120.         Marshal.FreeCoTaskMem(lParam)
    121.  
    122.         ' and release the device context
    123.         e.Graphics.ReleaseHdc(hdc)
    124.  
    125.         Return res
    126.     End Function
    127.  
    128.     ' Convert between 1/100 inch (unit used by the .NET framework)
    129.     ' and twips (1/1440 inch, used by Win32 API calls)
    130.     '
    131.     ' Parameter "n": Value in 1/100 inch
    132.     ' Return value: Value in twips
    133.     Private Function HundredthInchToTwips(ByVal n As Integer) As Int32
    134.         Return Convert.ToInt32(n * 14.4)
    135.     End Function
    136.  
    137.     ' Free cached data from rich edit control after printing
    138.     Public Sub FormatRangeDone()
    139.         Dim lParam As New IntPtr(0)
    140.         SendMessage(Handle, EM_FORMATRANGE, 0, lParam)
    141.     End Sub
    142.  
    143.     Public Sub PrintRichTextContents()
    144.         Dim printDoc As New PrintDocument()
    145.         AddHandler printDoc.BeginPrint, AddressOf printDoc_BeginPrint
    146.         AddHandler printDoc.PrintPage, AddressOf printDoc_PrintPage
    147.         AddHandler printDoc.EndPrint, AddressOf printDoc_EndPrint
    148.         ' Start printing process
    149.         printDoc.Print()
    150.     End Sub
    151.  
    152.     ' variable to trace text to print for pagination
    153.     Private m_nFirstCharOnPage As Integer
    154.  
    155.     Private Sub printDoc_BeginPrint(ByVal sender As Object, _
    156.         ByVal e As System.Drawing.Printing.PrintEventArgs)
    157.         ' Start at the beginning of the text
    158.         m_nFirstCharOnPage = 0
    159.     End Sub
    160.  
    161.     Private Sub printDoc_PrintPage(ByVal sender As Object, _
    162.         ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    163.         ' To print the boundaries of the current page margins
    164.         ' uncomment the next line:
    165.         ' e.Graphics.DrawRectangle(System.Drawing.Pens.Blue, e.MarginBounds)
    166.  
    167.         ' make the RichTextBoxEx calculate and render as much text as will
    168.         ' fit on the page and remember the last character printed for the
    169.         ' beginning of the next page
    170.         m_nFirstCharOnPage = FormatRange(False, _
    171.         e, _
    172.         m_nFirstCharOnPage, _
    173.         MyBase.TextLength)
    174.  
    175.         ' check if there are more pages to print
    176.         If (m_nFirstCharOnPage < MyBase.TextLength) Then
    177.             e.HasMorePages = True
    178.         Else
    179.             e.HasMorePages = False
    180.         End If
    181.     End Sub
    182.  
    183.     Private Sub printDoc_EndPrint(ByVal sender As Object, _
    184.         ByVal e As System.Drawing.Printing.PrintEventArgs)
    185.         ' Clean up cached information
    186.         FormatRangeDone()
    187.     End Sub
    188.  
    189. End Class

    to print the text from the extended richtextbox:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         MyRichTextBoxEx.PrintRichTextContents()
    5.     End Sub
    6.  
    7. End Class
    Attached Files Attached Files
    Last edited by .paul.; May 12th, 2008 at 05:27 PM.

  3. #3
    New Member
    Join Date
    Jan 2010
    Posts
    14

    Re: [RESOLVED] [2008] Print out font's and colours with richtextbox

    Code:
    MyRichTextBoxEx.PrintRichTextContents()
    for tis code i gt prob.. it show reference to a non-shared member required an object reference..

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2008] Print out font's and colours with richtextbox

    Quote Originally Posted by kokloong View Post
    Code:
    MyRichTextBoxEx.PrintRichTextContents()
    for tis code i gt prob.. it show reference to a non-shared member required an object reference..
    That was just an EXAMPLE. You're supposed to refer to YOUR instance of that class. Have you added one to your form? That is the one you call PrintRichTextContents on.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    New Member
    Join Date
    Jan 2010
    Posts
    14

    Re: [RESOLVED] [2008] Print out font's and colours with richtextbox

    Quote Originally Posted by jmcilhinney View Post
    That was just an EXAMPLE. You're supposed to refer to YOUR instance of that class. Have you added one to your form? That is the one you call PrintRichTextContents on.
    i din understand that's y i ask.. nw i supposed to refer to my instance of which class? i need 2 add wat in my form as well? can guide me? thx..

  6. #6
    Lively Member
    Join Date
    Sep 2009
    Posts
    99

    Re: [RESOLVED] [2008] Print out font's and colours with richtextbox

    This is a pretty old thread. But never the less, I was curious if there's a simple way to hook this up to PrinterDialog? I've read and read and read on these but frankly I'm still confused.

    Thanks.

  7. #7
    Lively Member
    Join Date
    Sep 2009
    Posts
    99

    Re: [RESOLVED] [2008] Print out font's and colours with richtextbox

    Why is it that all you have to do is post something or ask someone and suddenly the answer drops on you on your way to get coffee?

    The following allows me to use the Printdialog stuff with this class. Note that MyForm1 is whatever Form owns Printdialog1. Clearly, you could code all this without forms as well.

    My changes are in bold below.

    Code:
    Public Sub PrintRichTextContents()
            Dim printDoc As New PrintDocument()
            AddHandler printDoc.BeginPrint, AddressOf printDoc_BeginPrint
            AddHandler printDoc.PrintPage, AddressOf printDoc_PrintPage
            AddHandler printDoc.EndPrint, AddressOf printDoc_EndPrint
            MyForm1.PrintDialog1.Document = printDoc
            MyForm1.PrintDialog1.ShowDialog()
            ' Start printing process
            printDoc.Print()
        End Sub

  8. #8
    New Member
    Join Date
    Aug 2013
    Posts
    4

    Re: [RESOLVED] [2008] Print out font's and colours with richtextbox

    Paul, I see you haven't posted to this for some time, but thought it was easier to ask under your posting of the code. I'm having a bit of a problem with this. If I print a full page document and then come back later and print a half page, the bottom part of the first document prints on the half page document.

    Basically, I have a form that has an rtfBox on it. I format the rtfBox the way I want. Then I set the myRichTextBoxEx1.Rtf = rtfBox.Rtf and print it. Under debug, I can see that both the rtfBox and the myRichTextBoxEx1 only has the half page of information, but what gets sent to the print is a full page. I unload the form between prints. Is there something else that needs to get cleared or initialized between prints?
    Thanks

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: [RESOLVED] [2008] Print out font's and colours with richtextbox

    I don't remember the full code + what it does or doesn't do.
    Start a new thread, + post the code you're using with your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width