WYSIWYG in RichTextBox

The code below is from VB6. It caused a RichTextBox to display text as it would appear for the page orientation and margins for the default printer. I have tried to crack the code on upgrading this to VB 2005, but so far, no success. Can anyone tell me how to achieve the same thing in .NET

Thanks,

Mike

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' WYSIWYG_RTF - Sets an RTF control to display itself the same as it
' would print on the default printer
'
' RTF - A RichTextBox control to set for WYSIWYG display.
'
' LeftMarginWidth - Width of desired left margin in twips
'
' RightMarginWidth - Width of desired right margin in twips
'
' Returns - The length of a line on the printer in twips
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function WYSIWYG_RTF( _
RTF As RichTextBox, _
LeftMarginWidth As Long, _
RightMarginWidth As Long) _
As Long

Dim LeftOffset As Long, LeftMargin As Long, RightMargin As Long
Dim RightOffset As Long
Dim LineWidth As Long
Dim PrinterhDC As Long
Dim r As Long

On Error Resume Next

'Set g_intOrientation and g_lngPaperSize
Printer.Orientation = g_intOrientation
Printer.Papersize = g_lngPaperSize

' Start a print job to initialize printer object
'Printer.Print Space(1)
'Printer.Print ""
Printer.ScaleMode = vbTwips

' Get the offset to the printable area on the page in twips
LeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hDC, _
PHYSICALOFFSETX), vbPixels, vbTwips)

' Calculate the Left, and Right margins
LeftMargin = LeftMarginWidth - LeftOffset

' Prevent the left margin from being set to less than zero
If LeftMargin < 0 Then LeftMargin = 0

RightMargin = (Printer.Width - RightMarginWidth) - (LeftOffset * 2)

' Calculate the line width
LineWidth = RightMargin - LeftMargin

' Create an hDC on the Printer pointed to by the Printer object
' This DC needs to remain for the RTF to keep up the WYSIWYG display
PrinterhDC = CreateDC(Printer.DriverName, Printer.Devicename, 0, 0)

' Tell the RTF to base it's display off of the printer
' at the desired line width
r = SendMessage(RTF.hwnd, EM_SETTARGETDEVICE, PrinterhDC, _
ByVal LineWidth)

' Abort the temporary print job used to get printer info
Printer.KillDoc

WYSIWYG_RTF = LineWidth

End Function




Answer this question

WYSIWYG in RichTextBox

  • HeathM

    Thanks for that Mike

    Or if you have Microsoft word on the same machines where you will be wanting to print your RTF box, you can reference word and copy the RTF text to a non visable word app and print it with Word, - The wrapping is not going to match, but all formatting is maintained nicely...

    Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click

            Dim TheFileName As String = "C:\temp\printthis.rtf"

            Dim wordApp As New Word.Application()

            Dim doc As New Word.Document

            Me.RichTextBox1.SaveFile(TheFileName)

    doc = wordApp.Documents.Open(TheFileName, ConfirmConversions:=False,    ReadOnly:=True, AddToRecentFiles:=False, Format:=Word.WdOpenFormat.wdOpenFormatRTF)

            wordApp.Visible = False      'not visible by default, just being explicit

            doc.PrintOut(Background:=False)

            doc.Close(SaveChanges:=False)

            wordApp.Quit(SaveChanges:=False)

            doc = Nothing

            wordApp = Nothing

    End Sub

     



  • ChenWang

    Try posting this is the upgrade newsgroup. There are a lot of people will be able to help you out there.


    http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=879&SiteID=1



  • dragoncells

    I worked out the answer. In case this could help anyone else, here's the code:

    Call the procedure in the Form_Load event, or after everytime the page setup is changed:

    Private Sub btnPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPageSetup.Click

    Dim LineWidth As Integer = 0

    PageSetupDialog1.ShowDialog()

    LineWidth = WYSIWYG_RTF(Me.RichTextBoxPrintCtrl1, Me.PrintDocument1)

    Me.RichTextBoxPrintCtrl1.Refresh()

    End Sub

    Imports System.Drawing.Printing

    Module modWYSIWYG

    Private Const WM_USER As Long = &H400

    Private Const EM_FORMATRANGE As Long = WM_USER + 57

    Private Const EM_SETTARGETDEVICE As Long = WM_USER + 72

    Public Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, _

    ByVal lpOutput As Integer, ByVal lpInitData As Integer) As Integer

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, _

    ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Public Function WYSIWYG_RTF(ByRef RTF As RichTextBox, ByRef pd As PrintDocument)

    Dim LineWidth As Integer

    Dim PrinterhDC As Integer

    Dim r As Integer

    PrinterhDC = CreateDC(pd.DefaultPageSettings.PrinterSettings.GetHdevnames.ToString, pd.DefaultPageSettings.PrinterSettings.PrinterName, 0, 0)

    If pd.DefaultPageSettings.Landscape Then

    '1440 Twips=1 Inch

    LineWidth = (pd.DefaultPageSettings.PaperSize.Height - (pd.DefaultPageSettings.Margins.Right + pd.DefaultPageSettings.Margins.Left)) * 14.4

    Else

    LineWidth = (pd.DefaultPageSettings.PaperSize.Width - (pd.DefaultPageSettings.Margins.Right + pd.DefaultPageSettings.Margins.Left)) * 14.4

    End If

    r = SendMessage(RTF.Handle, EM_SETTARGETDEVICE, PrinterhDC, LineWidth)

    WYSIWYG_RTF = LineWidth

    End Function

    End Module



  • polymorphicx

    Thanks,

    Mike



  • WYSIWYG in RichTextBox