OVH Cloud OVH Cloud

Concaténer RTF

28 réponses
Avatar
Barsalou
Bonjour

Un expert en RichTextBox saurait-il me dire:
Si l'on peut concaténer deux RichText, avec leur mise en forme évidemment
(l'opérateur & ne fonctionne pas);
Si l'on peut calculer le nombre de lignes dans un RichText;
Enfin si, grâce à ce nombre de ligne, , on peut insérer au bon endroit un
saut de page (pour l'impression).

Merci d'avance

P.S. où peut-on trouver la syntaxe RTF ?

8 réponses

1 2 3
Avatar
ng
> HOWTO: Set Up the RichTextBox Control for WYSIWYG Printing


Voila qui est interessant.

--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/
Avatar
ng
> Je dis des bêtises ou ça semble raisonnable?


Non je suis et ai toujours été d'accord avec ca :)

--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/
Avatar
Youky
Merci Pascal,
Mon code faisait 3 lignes, le tien fait 5 pages mais l'avantage ........CA
MARCHE ! !
C'est exactement ce que j'ai cherché un an.
Encore merci
Youky crie Youpi

"Pascal B." a écrit dans le message news:

Bonjour Youki,

Il existe bien la méthode SelPrint qui permet d'imprimer le RTF avec les


polices et la mise en forme. Mais cette méthode ne permet
que peu de possiblités (comme la position du texte sur la ou les pages).
Toutefois, dans l'aide MSDN, il y a un article qui propose une alternative


plus souple pour l'impression des RTF.
Lorsque vous en aurez compris le principe, il sera facile d'adapter le


code à vos besoins (par exemple pour une prévisualisation à
l'écran du texte en changeant l'hDC de l'imprimante par l'hDC d'une


PictureBox)

Cordialement
Pascal

Voici l'article en anglais
(désolé pour les anglophobes comme le Troll):
-----------------------------------------------


HOWTO: Set Up the RichTextBox Control for WYSIWYG Printing
Last reviewed: December 15, 1997
Article ID: Q146022
The information in this article applies to:
a.. Professional and Enterprise Editions of Microsoft Visual


Basic, 32-bit only, for Windows, version 4.0


SUMMARY
The SelPrint method of the RichTextBox control does not allow a


programmer to set the position of the output on the printer.
In addition, the RichTextBox control does not provide a method for


displaying its contents as they would show up on the printer.
This article explains how to set up a RichTextBox with a WYSIWYG (What You


See Is What You Get) display and then how to print it.



MORE INFORMATION
The Visual Basic 4.0 RichTextBox control is a sub-classed control


based on the RichTextBox provided by the Win32 operating
system. The operating system control supports many messages that are not


exposed in Visual Basic 4.0. One of these messages is
EM_SETTARGETDEVICE. The EM_SETTARGETDEVICE message is used to tell a


RichTextBox to base its display on a target device such as a
printer. Another message that is not fully exposed by Visual Basic 4.0 is


EM_FORMATRANGE. The EM_FORMATRANGE message sends a page at
a time to an output device using the specified coordinates. Using these


messages in Visual Basic 4.0, it is possible to make a
RichTextBox support WYSIWYG display and output.

The following example illustrates how to take advantage of the


EM_SETTARGETDEVICE and EM_FORMATRANGE messages from Visual
Basic 4.0. The example provides two re-usable procedures to send these


messages. The first procedure WYSIWYG_RTF() sets a
RichTextBox to provide a WYSIWYG display based on the default printer and


specified margins. The second procedure PrintRtf() prints
the contents of the RichTextBox with the specified margins.



EXAMPLE

1.. Start a new project in the Visual Basic 32-bit edition. Form1


is created by default.

2.. Put a CommandButton and a RichTextBox control on Form1.

3.. Add the following code to Form1:
Option Explicit


Private Sub Form_Load()
Dim LineWidth As Long

' Initialize Form and Command button
Me.Caption = "Rich Text Box WYSIWYG Printing Example"
Command1.Move 10, 10, 600, 380
Command1.Caption = "&Print"

' Set the font of the RTF to a TrueType font for best results
RichTextBox1.SelFontName = "Arial"
RichTextBox1.SelFontSize = 10

' Tell the RTF to base it's display off of the printer
LineWidth = WYSIWYG_RTF(RichTextBox1, 1440, 1440) '1440 Twips=1 Inch

' Set the form width to match the line width
Me.Width = LineWidth + 200
End Sub

Private Sub Form_Resize()
' Position the RTF on form
RichTextBox1.Move 100, 500, Me.ScaleWidth - 200, Me.ScaleHeight -


600
End Sub

Private Sub Command1_Click()
' Print the contents of the RichTextBox with a one inch margin
PrintRTF RichTextBox1, 1440, 1440, 1440, 1440 ' 1440 Twips = 1 Inch
End Sub


4.. Insert a new standard module into the project, Module1.bas is


created by default.

5.. Add the following code to Module1:
Option Explicit

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 Const EM_SETTARGETDEVICE As Long = WM_USER + 72
Private Const PHYSICALOFFSETX As Long = 112
Private Const PHYSICALOFFSETY As Long = 113

Private Declare Function GetDeviceCaps Lib "gdi32" ( _
ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA"


_
(ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, _
lp As Any) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" _
(ByVal lpDriverName As String, ByVal lpDeviceName As String, _
ByVal lpOutput As Long, ByVal lpInitData As Long) As Long




''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' 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 LineWidth As Long
Dim PrinterhDC As Long
Dim r As Long

' Start a print job to initialize printer object
Printer.Print Space(1)
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
RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset

' 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




''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' PrintRTF - Prints the contents of a RichTextBox control using the
' provided margins
'
' RTF - A RichTextBox control to print
'
' LeftMarginWidth - Width of desired left margin in twips
'
' TopMarginHeight - Height of desired top margin in twips
'
' RightMarginWidth - Width of desired right margin in twips
'
' BottomMarginHeight - Height of desired bottom margin in twips
'
' Notes - If you are also using WYSIWYG_RTF() on the provided RTF
' parameter you should specify the same LeftMarginWidth and
' RightMarginWidth that you used to call WYSIWYG_RTF()



''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long, _
TopMarginHeight, RightMarginWidth, BottomMarginHeight)
Dim LeftOffset As Long, TopOffset As Long
Dim LeftMargin As Long, TopMargin As Long
Dim RightMargin As Long, BottomMargin As Long
Dim fr As FormatRange
Dim rcDrawTo As Rect
Dim rcPage As Rect
Dim TextLength As Long
Dim NextCharPosition As Long
Dim r As Long

' Start a print job to get a valid Printer.hDC
Printer.Print Space(1)
Printer.ScaleMode = vbTwips

' Get the offsett to the printable area on the page in twips
LeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, _
PHYSICALOFFSETX), vbPixels, vbTwips)
TopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hdc, _
PHYSICALOFFSETY), vbPixels, vbTwips)

' Calculate the Left, Top, Right, and Bottom margins
LeftMargin = LeftMarginWidth - LeftOffset
TopMargin = TopMarginHeight - TopOffset
RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset
BottomMargin = (Printer.Height - BottomMarginHeight) - TopOffset

' Set printable area rect
rcPage.Left = 0
rcPage.Top = 0
rcPage.Right = Printer.ScaleWidth
rcPage.Bottom = Printer.ScaleHeight

' Set rect in which to print (relative to printable area)
rcDrawTo.Left = LeftMargin
rcDrawTo.Top = TopMargin
rcDrawTo.Right = RightMargin
rcDrawTo.Bottom = BottomMargin

' Set up the print instructions
fr.hdc = Printer.hdc ' Use the same DC for measuring and rendering
fr.hdcTarget = Printer.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
NextCharPosition = SendMessage(RTF.hWnd, EM_FORMATRANGE, True,


fr)
If NextCharPosition >= TextLength Then Exit Do 'If done then


exit
fr.chrg.cpMin = NextCharPosition ' Starting position for next


page
Printer.NewPage ' Move on to next page
Printer.Print Space(1) ' Re-initialize hDC
fr.hDC = Printer.hDC
fr.hDCTarget = Printer.hDC
Loop

' Commit the print job
Printer.EndDoc

' Allow the RTF to free up memory
r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal CLng(0))
End Sub


6.. Save the project.

7.. Run the project.

8.. Enter or paste some text into the RichTextBox.

9.. Press the Print command button. Note that the printed output


should word-wrap at the same locations as displayed on the
screen. Also, the output should be printed with the specified one-inch


margin all around.
Keywords : APrgPrint vb432 VB4WIN vbwin kbprg kbprint
Technology : kbvba
Version : WINDOWS:4.0
Platform : NT WINDOWS
Issue type : kbhowto


--------------------------------------------------------------------------




=========================================================================== === >

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS


PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT
DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE


WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE


LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT,
INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES,


EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE


EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR
INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 15, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.





"Youky" wrote in message


news:
| Je confirme ng,
| Néanmoins j'ai compris le code, celui-ci ne gère pas les polices,


couleurs,
| gras, soulignés comme tu viens de le signaler.
| Pour ma part j'ai un logiciel en cours, un traitement de texte avec la
| gestion des polices, gras, souligné et couleurs
| avec des feuilles MDI (comme Word en +simple) hélas seul à l'écran cé
| nickel.
| Je n'arrive pas à gérer l'impression de +40lignes de mon RTF comportant
| différentes polices et tailles, encore moins l'apercu
| Dans l'aide il est bien mentionné que l'on peut facilement imprimer tout


ou
| une partie du texte.
| Voila un an que je but et suis à l'affut de la soluce.
| Donc j'ai choisis CreatObjet "Word.Application" et fait tout faire par


Word
| Merci à vous tous, Youky
|
| "ng" a écrit dans le message news:
|
| > Salut,
| >
| > Envoyer le projet avec aurait été plus astucieux, car la il faut


recrer un
| > projet, ajouter l'objet rtfbox, rajouter ta form... sinon on a le


droit à
| > une zolie erreur...
| >
| > En plus y a des goto partout :)
| >
| > --
| > Nicolas G.
| > FAQ VB : http://faq.vb.free.fr
| > API Guide : http://www.allapi.net
| > Google Groups : http://groups.google.fr/
| > MZ-Tools : http://www.mztools.com/
| >
| > LE TROLL wrote:
| > > Salut, ça vaut ce que ça vaut, lol, mais ça marche,
| > > dis-moi si c'est ça que tu veux???
| > > Ci-joitn en fichier aussi (form + fichier rtf)-> vb6
| > > -------
| > > Routines avec RTF (impression, recherche, poursuite de a
| > > recherche)
| > >
| > > OBJETS
| > > -1-
| > > aideF = rtf (fichier Aide.rtf) incorporé à l'exe, chargé
| > > dans FileName = path + Aide.rtf
| > > -2-
| > > textBox = quoi-cherche
| > > -3- menus
| > > m_cherche
| > > m_poursuivre
| > > m_imp
| > > m_aide
| > >
| > >
| > > ' aide
| > > '
| > > Dim ou As Long
| > > Dim ici As Long
| > > Dim recherche As String
| > > '
| > > Sub Form_Load()
| > > ' éditeur du logiciel pour lien Internet...
| > > If Form1.BRIDE = False Then: Form11.Caption > | > > Form1.titre
| > > If Form1.BRIDE = True Then: Form11.Caption > | > > Form1.titre_bride
| > > '
| > > Label1.Visible = False
| > > quoi_cherche.Visible = False
| > > End Sub
| > >
| > > Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) '
| > > touches
| > > If KeyCode = 113 Then: Call va_recherche 'F2
| > > If KeyCode = 114 Then: Call va_continue 'F3
| > > End Sub
| > >
| > > Sub m_recherche_Click()
| > > Call va_recherche
| > > End Sub
| > >
| > > Sub va_recherche() 'recherche
| > > Dim quoi As Integer
| > > '
| > > On Error Resume Next
| > > quoi_cherche = ""
| > > recherche = ""
| > > Label1.Visible = False
| > > quoi_cherche.Visible = False
| > > '
| > > recherche = InputBox("Aide" + Chr(10) + "Saisir le
| > > texte recherché ?")
| > > '
| > > quoi = Len(recherche)
| > > If quoi = 0 Then: Exit Sub
| > > '
| > > Label1.Visible = True
| > > quoi_cherche.Visible = True
| > > '
| > > quoi_cherche.Text = recherche 'affiche recherche
| > > ou = aideF.Find(recherche, 0, , 2 Or 8)
| > > aideF.SetFocus
| > > If ou = (-1) Then
| > > MsgBox "Texte non trouvé", 48
| > > Exit Sub
| > > End If
| > > aideF.SelStart = ou ' n°octet texte
| > > aideF.Span " ,;:.?!+*-/=", True, True
| > > End Sub
| > >
| > > Sub m_poursuivre_Click()
| > > Call va_continue
| > > End Sub
| > >
| > > Sub va_continue() 'continue la recherche
| > > Dim quoi As Integer
| > > '
| > > If quoi_cherche = "" Then
| > > MsgBox "Aucune chaîne n'est en recherche", 48
| > > Exit Sub
| > > End If
| > > On Error Resume Next
| > > aideF.SetFocus
| > > aideF.SelStart = ou
| > > ou = ou + 1
| > > ici = aideF.Find(recherche, ou, , 2 Or 8)
| > > If ici = (-1) Then: MsgBox "Texte non trouvé", 48
| > > ou = ici
| > > aideF.SelStart = ou
| > > aideF.Span " ,;:.?!+*-/=", True, True
| > > End Sub
| > >
| > > Sub m_aide_Click()
| > > MsgBox "RECHERCHER" + Chr(10) + "Cherche le mot saisi
| > > (du curseur vers le bas)." + Chr(10) + "Vouv pouvez dans la
| > > table des recherches ou dans la totalité de l'aide, copier
| > > un texte et le coller dans la boîte de saisie des
| > > recherches." + Chr(10) + Chr(10) + "POURSUIVRE LA RECHERCHE"
| > > + Chr(10) + "Continue la recherche demandée." + Chr(10) +
| > > Chr(10) + "IMPRESSION" + Chr(10) + "Outre l'impression
| > > papier prévue dans le menu haut, à l'aide de la souris vous
| > > pouvez sélectionner le texte désiré, le copier, puis le
| > > coller dans une application Windows pour impression.", 64,
| > > "Aide sur l'aide"
| > > End Sub
| > >
| > > Sub m_imp_Click() ' impression papier RTF géré en lignes,
| > > colonnes et blancs insécables...
| > > Dim maxi As Long
| > > Dim combien As Long
| > > Dim avance As Long
| > > Dim texte_1 As String
| > > Dim texte_2 As String
| > > Dim o_s, p_s As String * 1
| > > Dim o_n As Integer
| > > Dim b As Long
| > > '
| > > reponse = MsgBox("Confirmez l'impression", vbQuestion +
| > > vbYesNo + vbDefaultButton2)
| > > If reponse <> vbYes Then Exit Sub
| > > '
| > > texte_1 = aideF.Text ' balance le contenu dans
| > > variable aideF = RTF
| > > maxi = Len(texte_1) ' taille tu texte
| > > combien = 1
| > > avance = 1
| > > For b = 1 To maxi
| > > o_s = Mid(texte_1, b, 1) ' extraction de chaque
| > > octet
| > > texte_2 = texte_2 + o_s ' sauvegarde text_1 dans
| > > text_2
| > > o_n = Asc(o_s) ' contrôle la valeur ascii de
| > > l'octet extrait
| > > If o_n = 13 Then: combien = 0 ' saut de ligne
| > > If combien > 64 And o_n = 32 Then ' saut de page
| > > '
| > > If o_n = 32 Then ' gestion blanc insécable
| > > o_s = Mid(texte_1, b + 1, 1)
| > > If o_s = "?" Or p_s = "!" Or o_s = ":" Or o_s
| > > = ";" Then
| > > texte_2 = texte_2 + o_s
| > > b = b + 1
| > > GoTo ici
| > > End If
| > > End If
| > > '
| > > texte_2 = texte_2 + Chr(13)
| > > texte_2 = texte_2 + Chr(10)
| > > combien = 0
| > > End If
| > > combien = combien + 1
| > > avance = avance + 1
| > > ici:
| > > Next b
| > > '''''''''''''''''''''
| > > Printer.FontName = "courrier new"
| > > Printer.FontSize = 12
| > > Printer.Print texte_2
| > > Printer.EndDoc
| > > End Sub
| > >
| > > Sub Form_Unload(Cancel As Integer) ' end_système + <alt>_F4
| > > Cancel = Cancel - 1
| > > Call ends
| > > End Sub
| > >
| > > Sub ends()
| > > Form11.Hide
| > > Unload Form11
| > > Form1.SetFocus
| > > End Sub
| > > ---------------
| > >
| > >
| > > "Barsalou" a écrit
| > > dans le message de news:
| > >
| > >> Je suis également intéressé.
| > >> Peut-être devrais-tu mettre ton code sur le forum.
| >
| >
|
|




Avatar
LE TROLL
Oui, autant pour moi, 12 form, 10 line...
Mais si c'est pour sauter de page, su tu peux compter les
lignes, tu peux gérer des lignes vide pour sauter...
--------

"Barsalou" a écrit
dans le message de news:

Non Form Feed est bien le caractère ASCII 12.
D'après MSDN il ne serait pas géré par Windows.


Avatar
Barsalou
Merci pour ces renseignements.

J'ai trouvé qu'il valait mieux ajouter selRTF (après sélection idoine)
plutôt que textRTF; ceci évite des sauts de lignes inutiles.

Par ailleurs j'ai trouvé le moyen d'insérer un saut de page, lequel marche
dans Word si on y lit le texte sauvé par savefile.
Voici mon code:
'ajouter un saut de page à la fin de RTB (avant d'ajouter autre chose)
RTB.SelStart = Len(RT4.Text)
RTB.SelLength = 0
RTB.SelRTF = "{parpagebbpar}"

Bizarrement on ne peut se passer d'aucun des par, ce qui crée d'ailleurs un
saut de ligne au début de la page suivante.
Quelqu'un trouvera sans doute mieux.
Avatar
Patrice Henrio
Et même où serait le plaisir de la programmation si ce n'était pas justement
imaginer une solution et vérifier qu'elle marche ou comprendre pourquoi elle
ne marche pas.

"ng" a écrit dans le message de news:

Je dis des bêtises ou ça semble raisonnable?


Non je suis et ai toujours été d'accord avec ca :)

--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/



Avatar
Pascal B.
Je savais bien que cet article allait t'intersser ...
Je suis content de t'avoir rendu service ;-)

Pascal

"Youky" wrote in message news:
| Merci Pascal,
| Mon code faisait 3 lignes, le tien fait 5 pages mais l'avantage ........CA
| MARCHE ! !
| C'est exactement ce que j'ai cherché un an.
| Encore merci
| Youky crie Youpi
|
| "Pascal B." a écrit dans le message news:
|
| > Bonjour Youki,
| >
| > Il existe bien la méthode SelPrint qui permet d'imprimer le RTF avec les
| polices et la mise en forme. Mais cette méthode ne permet
| > que peu de possiblités (comme la position du texte sur la ou les pages).
| > Toutefois, dans l'aide MSDN, il y a un article qui propose une alternative
| plus souple pour l'impression des RTF.
| > Lorsque vous en aurez compris le principe, il sera facile d'adapter le
| code à vos besoins (par exemple pour une prévisualisation à
| > l'écran du texte en changeant l'hDC de l'imprimante par l'hDC d'une
| PictureBox)
| >
| > Cordialement
| > Pascal
| >
| > Voici l'article en anglais
| > (désolé pour les anglophobes comme le Troll):
| > -----------------------------------------------
| >
| >
| > HOWTO: Set Up the RichTextBox Control for WYSIWYG Printing
| > Last reviewed: December 15, 1997
| > Article ID: Q146022
| > The information in this article applies to:
| > a.. Professional and Enterprise Editions of Microsoft Visual
| Basic, 32-bit only, for Windows, version 4.0
| >
| >
| > SUMMARY
| > The SelPrint method of the RichTextBox control does not allow a
| programmer to set the position of the output on the printer.
| > In addition, the RichTextBox control does not provide a method for
| displaying its contents as they would show up on the printer.
| > This article explains how to set up a RichTextBox with a WYSIWYG (What You
| See Is What You Get) display and then how to print it.
| >
| >
| >
| > MORE INFORMATION
| > The Visual Basic 4.0 RichTextBox control is a sub-classed control
| based on the RichTextBox provided by the Win32 operating
| > system. The operating system control supports many messages that are not
| exposed in Visual Basic 4.0. One of these messages is
| > EM_SETTARGETDEVICE. The EM_SETTARGETDEVICE message is used to tell a
| RichTextBox to base its display on a target device such as a
| > printer. Another message that is not fully exposed by Visual Basic 4.0 is
| EM_FORMATRANGE. The EM_FORMATRANGE message sends a page at
| > a time to an output device using the specified coordinates. Using these
| messages in Visual Basic 4.0, it is possible to make a
| > RichTextBox support WYSIWYG display and output.
| >
| > The following example illustrates how to take advantage of the
| EM_SETTARGETDEVICE and EM_FORMATRANGE messages from Visual
| > Basic 4.0. The example provides two re-usable procedures to send these
| messages. The first procedure WYSIWYG_RTF() sets a
| > RichTextBox to provide a WYSIWYG display based on the default printer and
| specified margins. The second procedure PrintRtf() prints
| > the contents of the RichTextBox with the specified margins.
| >
| >
| >
| > EXAMPLE
| >
| > 1.. Start a new project in the Visual Basic 32-bit edition. Form1
| is created by default.
| >
| > 2.. Put a CommandButton and a RichTextBox control on Form1.
| >
| > 3.. Add the following code to Form1:
| > Option Explicit
| >
| >
| > Private Sub Form_Load()
| > Dim LineWidth As Long
| >
| > ' Initialize Form and Command button
| > Me.Caption = "Rich Text Box WYSIWYG Printing Example"
| > Command1.Move 10, 10, 600, 380
| > Command1.Caption = "&Print"
| >
| > ' Set the font of the RTF to a TrueType font for best results
| > RichTextBox1.SelFontName = "Arial"
| > RichTextBox1.SelFontSize = 10
| >
| > ' Tell the RTF to base it's display off of the printer
| > LineWidth = WYSIWYG_RTF(RichTextBox1, 1440, 1440) '1440 Twips=1 Inch
| >
| > ' Set the form width to match the line width
| > Me.Width = LineWidth + 200
| > End Sub
| >
| > Private Sub Form_Resize()
| > ' Position the RTF on form
| > RichTextBox1.Move 100, 500, Me.ScaleWidth - 200, Me.ScaleHeight -
| 600
| > End Sub
| >
| > Private Sub Command1_Click()
| > ' Print the contents of the RichTextBox with a one inch margin
| > PrintRTF RichTextBox1, 1440, 1440, 1440, 1440 ' 1440 Twips = 1 Inch
| > End Sub
| >
| >
| > 4.. Insert a new standard module into the project, Module1.bas is
| created by default.
| >
| > 5.. Add the following code to Module1:
| > Option Explicit
| >
| > 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 Const EM_SETTARGETDEVICE As Long = WM_USER + 72
| > Private Const PHYSICALOFFSETX As Long = 112
| > Private Const PHYSICALOFFSETY As Long = 113
| >
| > Private Declare Function GetDeviceCaps Lib "gdi32" ( _
| > ByVal hdc As Long, ByVal nIndex As Long) As Long
| > Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA"
| _
| > (ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, _
| > lp As Any) As Long
| > Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" _
| > (ByVal lpDriverName As String, ByVal lpDeviceName As String, _
| > ByVal lpOutput As Long, ByVal lpInitData As Long) As Long
| >
| >
| ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
| > '
| > ' 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 LineWidth As Long
| > Dim PrinterhDC As Long
| > Dim r As Long
| >
| > ' Start a print job to initialize printer object
| > Printer.Print Space(1)
| > 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
| > RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset
| >
| > ' 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
| >
| >
| ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
| > '
| > ' PrintRTF - Prints the contents of a RichTextBox control using the
| > ' provided margins
| > '
| > ' RTF - A RichTextBox control to print
| > '
| > ' LeftMarginWidth - Width of desired left margin in twips
| > '
| > ' TopMarginHeight - Height of desired top margin in twips
| > '
| > ' RightMarginWidth - Width of desired right margin in twips
| > '
| > ' BottomMarginHeight - Height of desired bottom margin in twips
| > '
| > ' Notes - If you are also using WYSIWYG_RTF() on the provided RTF
| > ' parameter you should specify the same LeftMarginWidth and
| > ' RightMarginWidth that you used to call WYSIWYG_RTF()
| >
| ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
| > Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long, _
| > TopMarginHeight, RightMarginWidth, BottomMarginHeight)
| > Dim LeftOffset As Long, TopOffset As Long
| > Dim LeftMargin As Long, TopMargin As Long
| > Dim RightMargin As Long, BottomMargin As Long
| > Dim fr As FormatRange
| > Dim rcDrawTo As Rect
| > Dim rcPage As Rect
| > Dim TextLength As Long
| > Dim NextCharPosition As Long
| > Dim r As Long
| >
| > ' Start a print job to get a valid Printer.hDC
| > Printer.Print Space(1)
| > Printer.ScaleMode = vbTwips
| >
| > ' Get the offsett to the printable area on the page in twips
| > LeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, _
| > PHYSICALOFFSETX), vbPixels, vbTwips)
| > TopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hdc, _
| > PHYSICALOFFSETY), vbPixels, vbTwips)
| >
| > ' Calculate the Left, Top, Right, and Bottom margins
| > LeftMargin = LeftMarginWidth - LeftOffset
| > TopMargin = TopMarginHeight - TopOffset
| > RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset
| > BottomMargin = (Printer.Height - BottomMarginHeight) - TopOffset
| >
| > ' Set printable area rect
| > rcPage.Left = 0
| > rcPage.Top = 0
| > rcPage.Right = Printer.ScaleWidth
| > rcPage.Bottom = Printer.ScaleHeight
| >
| > ' Set rect in which to print (relative to printable area)
| > rcDrawTo.Left = LeftMargin
| > rcDrawTo.Top = TopMargin
| > rcDrawTo.Right = RightMargin
| > rcDrawTo.Bottom = BottomMargin
| >
| > ' Set up the print instructions
| > fr.hdc = Printer.hdc ' Use the same DC for measuring and rendering
| > fr.hdcTarget = Printer.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
| > NextCharPosition = SendMessage(RTF.hWnd, EM_FORMATRANGE, True,
| fr)
| > If NextCharPosition >= TextLength Then Exit Do 'If done then
| exit
| > fr.chrg.cpMin = NextCharPosition ' Starting position for next
| page
| > Printer.NewPage ' Move on to next page
| > Printer.Print Space(1) ' Re-initialize hDC
| > fr.hDC = Printer.hDC
| > fr.hDCTarget = Printer.hDC
| > Loop
| >
| > ' Commit the print job
| > Printer.EndDoc
| >
| > ' Allow the RTF to free up memory
| > r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal CLng(0))
| > End Sub
| >
| >
| > 6.. Save the project.
| >
| > 7.. Run the project.
| >
| > 8.. Enter or paste some text into the RichTextBox.
| >
| > 9.. Press the Print command button. Note that the printed output
| should word-wrap at the same locations as displayed on the
| > screen. Also, the output should be printed with the specified one-inch
| margin all around.
| > Keywords : APrgPrint vb432 VB4WIN vbwin kbprg kbprint
| > Technology : kbvba
| > Version : WINDOWS:4.0
| > Platform : NT WINDOWS
| > Issue type : kbhowto
| >
| >
| > --------------------------------------------------------------------------
| >
| >
| =========================================================================== | === | >
| >
| > THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS
| PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT
| > DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
| > PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE
| LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT,
| > INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES,
| EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN
| > ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE
| EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR
| > INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
| >
| > Last reviewed: December 15, 1997
| > © 1998 Microsoft Corporation. All rights reserved. Terms of Use.
| >
| >
| >
| >
| >
| > "Youky" wrote in message
| news:
| > | Je confirme ng,
| > | Néanmoins j'ai compris le code, celui-ci ne gère pas les polices,
| couleurs,
| > | gras, soulignés comme tu viens de le signaler.
| > | Pour ma part j'ai un logiciel en cours, un traitement de texte avec la
| > | gestion des polices, gras, souligné et couleurs
| > | avec des feuilles MDI (comme Word en +simple) hélas seul à l'écran cé
| > | nickel.
| > | Je n'arrive pas à gérer l'impression de +40lignes de mon RTF comportant
| > | différentes polices et tailles, encore moins l'apercu
| > | Dans l'aide il est bien mentionné que l'on peut facilement imprimer tout
| ou
| > | une partie du texte.
| > | Voila un an que je but et suis à l'affut de la soluce.
| > | Donc j'ai choisis CreatObjet "Word.Application" et fait tout faire par
| Word
| > | Merci à vous tous, Youky
| > |
| > | "ng" a écrit dans le message news:
| > |
| > | > Salut,
| > | >
| > | > Envoyer le projet avec aurait été plus astucieux, car la il faut
| recrer un
| > | > projet, ajouter l'objet rtfbox, rajouter ta form... sinon on a le
| droit à
| > | > une zolie erreur...
| > | >
| > | > En plus y a des goto partout :)
| > | >
| > | > --
| > | > Nicolas G.
| > | > FAQ VB : http://faq.vb.free.fr
| > | > API Guide : http://www.allapi.net
| > | > Google Groups : http://groups.google.fr/
| > | > MZ-Tools : http://www.mztools.com/
| > | >
| > | > LE TROLL wrote:
| > | > > Salut, ça vaut ce que ça vaut, lol, mais ça marche,
| > | > > dis-moi si c'est ça que tu veux???
| > | > > Ci-joitn en fichier aussi (form + fichier rtf)-> vb6
| > | > > -------
| > | > > Routines avec RTF (impression, recherche, poursuite de a
| > | > > recherche)
| > | > >
| > | > > OBJETS
| > | > > -1-
| > | > > aideF = rtf (fichier Aide.rtf) incorporé à l'exe, chargé
| > | > > dans FileName = path + Aide.rtf
| > | > > -2-
| > | > > textBox = quoi-cherche
| > | > > -3- menus
| > | > > m_cherche
| > | > > m_poursuivre
| > | > > m_imp
| > | > > m_aide
| > | > >
| > | > >
| > | > > ' aide
| > | > > '
| > | > > Dim ou As Long
| > | > > Dim ici As Long
| > | > > Dim recherche As String
| > | > > '
| > | > > Sub Form_Load()
| > | > > ' éditeur du logiciel pour lien Internet...
| > | > > If Form1.BRIDE = False Then: Form11.Caption | > | > > Form1.titre
| > | > > If Form1.BRIDE = True Then: Form11.Caption | > | > > Form1.titre_bride
| > | > > '
| > | > > Label1.Visible = False
| > | > > quoi_cherche.Visible = False
| > | > > End Sub
| > | > >
| > | > > Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) '
| > | > > touches
| > | > > If KeyCode = 113 Then: Call va_recherche 'F2
| > | > > If KeyCode = 114 Then: Call va_continue 'F3
| > | > > End Sub
| > | > >
| > | > > Sub m_recherche_Click()
| > | > > Call va_recherche
| > | > > End Sub
| > | > >
| > | > > Sub va_recherche() 'recherche
| > | > > Dim quoi As Integer
| > | > > '
| > | > > On Error Resume Next
| > | > > quoi_cherche = ""
| > | > > recherche = ""
| > | > > Label1.Visible = False
| > | > > quoi_cherche.Visible = False
| > | > > '
| > | > > recherche = InputBox("Aide" + Chr(10) + "Saisir le
| > | > > texte recherché ?")
| > | > > '
| > | > > quoi = Len(recherche)
| > | > > If quoi = 0 Then: Exit Sub
| > | > > '
| > | > > Label1.Visible = True
| > | > > quoi_cherche.Visible = True
| > | > > '
| > | > > quoi_cherche.Text = recherche 'affiche recherche
| > | > > ou = aideF.Find(recherche, 0, , 2 Or 8)
| > | > > aideF.SetFocus
| > | > > If ou = (-1) Then
| > | > > MsgBox "Texte non trouvé", 48
| > | > > Exit Sub
| > | > > End If
| > | > > aideF.SelStart = ou ' n°octet texte
| > | > > aideF.Span " ,;:.?!+*-/=", True, True
| > | > > End Sub
| > | > >
| > | > > Sub m_poursuivre_Click()
| > | > > Call va_continue
| > | > > End Sub
| > | > >
| > | > > Sub va_continue() 'continue la recherche
| > | > > Dim quoi As Integer
| > | > > '
| > | > > If quoi_cherche = "" Then
| > | > > MsgBox "Aucune chaîne n'est en recherche", 48
| > | > > Exit Sub
| > | > > End If
| > | > > On Error Resume Next
| > | > > aideF.SetFocus
| > | > > aideF.SelStart = ou
| > | > > ou = ou + 1
| > | > > ici = aideF.Find(recherche, ou, , 2 Or 8)
| > | > > If ici = (-1) Then: MsgBox "Texte non trouvé", 48
| > | > > ou = ici
| > | > > aideF.SelStart = ou
| > | > > aideF.Span " ,;:.?!+*-/=", True, True
| > | > > End Sub
| > | > >
| > | > > Sub m_aide_Click()
| > | > > MsgBox "RECHERCHER" + Chr(10) + "Cherche le mot saisi
| > | > > (du curseur vers le bas)." + Chr(10) + "Vouv pouvez dans la
| > | > > table des recherches ou dans la totalité de l'aide, copier
| > | > > un texte et le coller dans la boîte de saisie des
| > | > > recherches." + Chr(10) + Chr(10) + "POURSUIVRE LA RECHERCHE"
| > | > > + Chr(10) + "Continue la recherche demandée." + Chr(10) +
| > | > > Chr(10) + "IMPRESSION" + Chr(10) + "Outre l'impression
| > | > > papier prévue dans le menu haut, à l'aide de la souris vous
| > | > > pouvez sélectionner le texte désiré, le copier, puis le
| > | > > coller dans une application Windows pour impression.", 64,
| > | > > "Aide sur l'aide"
| > | > > End Sub
| > | > >
| > | > > Sub m_imp_Click() ' impression papier RTF géré en lignes,
| > | > > colonnes et blancs insécables...
| > | > > Dim maxi As Long
| > | > > Dim combien As Long
| > | > > Dim avance As Long
| > | > > Dim texte_1 As String
| > | > > Dim texte_2 As String
| > | > > Dim o_s, p_s As String * 1
| > | > > Dim o_n As Integer
| > | > > Dim b As Long
| > | > > '
| > | > > reponse = MsgBox("Confirmez l'impression", vbQuestion +
| > | > > vbYesNo + vbDefaultButton2)
| > | > > If reponse <> vbYes Then Exit Sub
| > | > > '
| > | > > texte_1 = aideF.Text ' balance le contenu dans
| > | > > variable aideF = RTF
| > | > > maxi = Len(texte_1) ' taille tu texte
| > | > > combien = 1
| > | > > avance = 1
| > | > > For b = 1 To maxi
| > | > > o_s = Mid(texte_1, b, 1) ' extraction de chaque
| > | > > octet
| > | > > texte_2 = texte_2 + o_s ' sauvegarde text_1 dans
| > | > > text_2
| > | > > o_n = Asc(o_s) ' contrôle la valeur ascii de
| > | > > l'octet extrait
| > | > > If o_n = 13 Then: combien = 0 ' saut de ligne
| > | > > If combien > 64 And o_n = 32 Then ' saut de page
| > | > > '
| > | > > If o_n = 32 Then ' gestion blanc insécable
| > | > > o_s = Mid(texte_1, b + 1, 1)
| > | > > If o_s = "?" Or p_s = "!" Or o_s = ":" Or o_s
| > | > > = ";" Then
| > | > > texte_2 = texte_2 + o_s
| > | > > b = b + 1
| > | > > GoTo ici
| > | > > End If
| > | > > End If
| > | > > '
| > | > > texte_2 = texte_2 + Chr(13)
| > | > > texte_2 = texte_2 + Chr(10)
| > | > > combien = 0
| > | > > End If
| > | > > combien = combien + 1
| > | > > avance = avance + 1
| > | > > ici:
| > | > > Next b
| > | > > '''''''''''''''''''''
| > | > > Printer.FontName = "courrier new"
| > | > > Printer.FontSize = 12
| > | > > Printer.Print texte_2
| > | > > Printer.EndDoc
| > | > > End Sub
| > | > >
| > | > > Sub Form_Unload(Cancel As Integer) ' end_système + <alt>_F4
| > | > > Cancel = Cancel - 1
| > | > > Call ends
| > | > > End Sub
| > | > >
| > | > > Sub ends()
| > | > > Form11.Hide
| > | > > Unload Form11
| > | > > Form1.SetFocus
| > | > > End Sub
| > | > > ---------------
| > | > >
| > | > >
| > | > > "Barsalou" a écrit
| > | > > dans le message de news:
| > | > >
| > | > >> Je suis également intéressé.
| > | > >> Peut-être devrais-tu mettre ton code sur le forum.
| > | >
| > | >
| > |
| > |
| >
| >
|
|
Avatar
Youky
Encore Merci ,
Je ne sais pas comment ni ou vous trouvez ou pondez ces lignes, c'est les
mysteres du VB
Faut 'il être copain de Bill ? ?
A+
"Pascal B." a écrit dans le message news:

Je savais bien que cet article allait t'intersser ...
Je suis content de t'avoir rendu service ;-)

Pascal

"Youky" wrote in message


news:
| Merci Pascal,
| Mon code faisait 3 lignes, le tien fait 5 pages mais l'avantage


........CA
| MARCHE ! !
| C'est exactement ce que j'ai cherché un an.
| Encore merci
| Youky crie Youpi
|
| "Pascal B." a écrit dans le message


news:
|
| > Bonjour Youki,
| >
| > Il existe bien la méthode SelPrint qui permet d'imprimer le RTF avec


les
| polices et la mise en forme. Mais cette méthode ne permet
| > que peu de possiblités (comme la position du texte sur la ou les


pages).
| > Toutefois, dans l'aide MSDN, il y a un article qui propose une


alternative
| plus souple pour l'impression des RTF.
| > Lorsque vous en aurez compris le principe, il sera facile d'adapter le
| code à vos besoins (par exemple pour une prévisualisation à
| > l'écran du texte en changeant l'hDC de l'imprimante par l'hDC d'une
| PictureBox)
| >
| > Cordialement
| > Pascal
| >
| > Voici l'article en anglais
| > (désolé pour les anglophobes comme le Troll):
| > -----------------------------------------------
| >
| >
| > HOWTO: Set Up the RichTextBox Control for WYSIWYG Printing
| > Last reviewed: December 15, 1997
| > Article ID: Q146022
| > The information in this article applies to:
| > a.. Professional and Enterprise Editions of Microsoft Visual
| Basic, 32-bit only, for Windows, version 4.0
| >
| >
| > SUMMARY
| > The SelPrint method of the RichTextBox control does not allow a
| programmer to set the position of the output on the printer.
| > In addition, the RichTextBox control does not provide a method for
| displaying its contents as they would show up on the printer.
| > This article explains how to set up a RichTextBox with a WYSIWYG (What


You
| See Is What You Get) display and then how to print it.
| >
| >
| >
| > MORE INFORMATION
| > The Visual Basic 4.0 RichTextBox control is a sub-classed


control
| based on the RichTextBox provided by the Win32 operating
| > system. The operating system control supports many messages that are


not
| exposed in Visual Basic 4.0. One of these messages is
| > EM_SETTARGETDEVICE. The EM_SETTARGETDEVICE message is used to tell a
| RichTextBox to base its display on a target device such as a
| > printer. Another message that is not fully exposed by Visual Basic 4.0


is
| EM_FORMATRANGE. The EM_FORMATRANGE message sends a page at
| > a time to an output device using the specified coordinates. Using


these
| messages in Visual Basic 4.0, it is possible to make a
| > RichTextBox support WYSIWYG display and output.
| >
| > The following example illustrates how to take advantage of the
| EM_SETTARGETDEVICE and EM_FORMATRANGE messages from Visual
| > Basic 4.0. The example provides two re-usable procedures to send these
| messages. The first procedure WYSIWYG_RTF() sets a
| > RichTextBox to provide a WYSIWYG display based on the default printer


and
| specified margins. The second procedure PrintRtf() prints
| > the contents of the RichTextBox with the specified margins.
| >
| >
| >
| > EXAMPLE
| >
| > 1.. Start a new project in the Visual Basic 32-bit edition.


Form1
| is created by default.
| >
| > 2.. Put a CommandButton and a RichTextBox control on Form1.
| >
| > 3.. Add the following code to Form1:
| > Option Explicit
| >
| >
| > Private Sub Form_Load()
| > Dim LineWidth As Long
| >
| > ' Initialize Form and Command button
| > Me.Caption = "Rich Text Box WYSIWYG Printing Example"
| > Command1.Move 10, 10, 600, 380
| > Command1.Caption = "&Print"
| >
| > ' Set the font of the RTF to a TrueType font for best results
| > RichTextBox1.SelFontName = "Arial"
| > RichTextBox1.SelFontSize = 10
| >
| > ' Tell the RTF to base it's display off of the printer
| > LineWidth = WYSIWYG_RTF(RichTextBox1, 1440, 1440) '1440 Twips=1


Inch
| >
| > ' Set the form width to match the line width
| > Me.Width = LineWidth + 200
| > End Sub
| >
| > Private Sub Form_Resize()
| > ' Position the RTF on form
| > RichTextBox1.Move 100, 500, Me.ScaleWidth - 200,


Me.ScaleHeight -
| 600
| > End Sub
| >
| > Private Sub Command1_Click()
| > ' Print the contents of the RichTextBox with a one inch margin
| > PrintRTF RichTextBox1, 1440, 1440, 1440, 1440 ' 1440 Twips = 1


Inch
| > End Sub
| >
| >
| > 4.. Insert a new standard module into the project, Module1.bas


is
| created by default.
| >
| > 5.. Add the following code to Module1:
| > Option Explicit
| >
| > 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 Const EM_SETTARGETDEVICE As Long = WM_USER + 72
| > Private Const PHYSICALOFFSETX As Long = 112
| > Private Const PHYSICALOFFSETY As Long = 113
| >
| > Private Declare Function GetDeviceCaps Lib "gdi32" ( _
| > ByVal hdc As Long, ByVal nIndex As Long) As Long
| > Private Declare Function SendMessage Lib "USER32" Alias


"SendMessageA"
| _
| > (ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, _
| > lp As Any) As Long
| > Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" _
| > (ByVal lpDriverName As String, ByVal lpDeviceName As String, _
| > ByVal lpOutput As Long, ByVal lpInitData As Long) As Long
| >
| >
| ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
| > '
| > ' 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 LineWidth As Long
| > Dim PrinterhDC As Long
| > Dim r As Long
| >
| > ' Start a print job to initialize printer object
| > Printer.Print Space(1)
| > 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
| > RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset
| >
| > ' 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
| >
| >
| ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
| > '
| > ' PrintRTF - Prints the contents of a RichTextBox control using the
| > ' provided margins
| > '
| > ' RTF - A RichTextBox control to print
| > '
| > ' LeftMarginWidth - Width of desired left margin in twips
| > '
| > ' TopMarginHeight - Height of desired top margin in twips
| > '
| > ' RightMarginWidth - Width of desired right margin in twips
| > '
| > ' BottomMarginHeight - Height of desired bottom margin in twips
| > '
| > ' Notes - If you are also using WYSIWYG_RTF() on the provided RTF
| > ' parameter you should specify the same LeftMarginWidth and
| > ' RightMarginWidth that you used to call WYSIWYG_RTF()
| >
| ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
| > Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long, _
| > TopMarginHeight, RightMarginWidth, BottomMarginHeight)
| > Dim LeftOffset As Long, TopOffset As Long
| > Dim LeftMargin As Long, TopMargin As Long
| > Dim RightMargin As Long, BottomMargin As Long
| > Dim fr As FormatRange
| > Dim rcDrawTo As Rect
| > Dim rcPage As Rect
| > Dim TextLength As Long
| > Dim NextCharPosition As Long
| > Dim r As Long
| >
| > ' Start a print job to get a valid Printer.hDC
| > Printer.Print Space(1)
| > Printer.ScaleMode = vbTwips
| >
| > ' Get the offsett to the printable area on the page in twips
| > LeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, _
| > PHYSICALOFFSETX), vbPixels, vbTwips)
| > TopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hdc, _
| > PHYSICALOFFSETY), vbPixels, vbTwips)
| >
| > ' Calculate the Left, Top, Right, and Bottom margins
| > LeftMargin = LeftMarginWidth - LeftOffset
| > TopMargin = TopMarginHeight - TopOffset
| > RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset
| > BottomMargin = (Printer.Height - BottomMarginHeight) - TopOffset
| >
| > ' Set printable area rect
| > rcPage.Left = 0
| > rcPage.Top = 0
| > rcPage.Right = Printer.ScaleWidth
| > rcPage.Bottom = Printer.ScaleHeight
| >
| > ' Set rect in which to print (relative to printable area)
| > rcDrawTo.Left = LeftMargin
| > rcDrawTo.Top = TopMargin
| > rcDrawTo.Right = RightMargin
| > rcDrawTo.Bottom = BottomMargin
| >
| > ' Set up the print instructions
| > fr.hdc = Printer.hdc ' Use the same DC for measuring and


rendering
| > fr.hdcTarget = Printer.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
| > NextCharPosition = SendMessage(RTF.hWnd, EM_FORMATRANGE,


True,
| fr)
| > If NextCharPosition >= TextLength Then Exit Do 'If done then
| exit
| > fr.chrg.cpMin = NextCharPosition ' Starting position for next
| page
| > Printer.NewPage ' Move on to next page
| > Printer.Print Space(1) ' Re-initialize hDC
| > fr.hDC = Printer.hDC
| > fr.hDCTarget = Printer.hDC
| > Loop
| >
| > ' Commit the print job
| > Printer.EndDoc
| >
| > ' Allow the RTF to free up memory
| > r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal CLng(0))
| > End Sub
| >
| >
| > 6.. Save the project.
| >
| > 7.. Run the project.
| >
| > 8.. Enter or paste some text into the RichTextBox.
| >
| > 9.. Press the Print command button. Note that the printed


output
| should word-wrap at the same locations as displayed on the
| > screen. Also, the output should be printed with the specified one-inch
| margin all around.
| > Keywords : APrgPrint vb432 VB4WIN vbwin kbprg kbprint
| > Technology : kbvba
| > Version : WINDOWS:4.0
| > Platform : NT WINDOWS
| > Issue type : kbhowto
| >
| >
|
--------------------------------------------------------------------------
| >
| >
|


=========================================================================== > | === > | >
| >
| > THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS
| PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT
| > DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
| > PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE
| LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT,
| > INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL


DAMAGES,
| EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN
| > ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW


THE
| EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR
| > INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
| >
| > Last reviewed: December 15, 1997
| > © 1998 Microsoft Corporation. All rights reserved. Terms of Use.
| >
| >
| >
| >
| >
| > "Youky" wrote in message
| news:
| > | Je confirme ng,
| > | Néanmoins j'ai compris le code, celui-ci ne gère pas les polices,
| couleurs,
| > | gras, soulignés comme tu viens de le signaler.
| > | Pour ma part j'ai un logiciel en cours, un traitement de texte avec


la
| > | gestion des polices, gras, souligné et couleurs
| > | avec des feuilles MDI (comme Word en +simple) hélas seul à l'écran



| > | nickel.
| > | Je n'arrive pas à gérer l'impression de +40lignes de mon RTF


comportant
| > | différentes polices et tailles, encore moins l'apercu
| > | Dans l'aide il est bien mentionné que l'on peut facilement imprimer


tout
| ou
| > | une partie du texte.
| > | Voila un an que je but et suis à l'affut de la soluce.
| > | Donc j'ai choisis CreatObjet "Word.Application" et fait tout faire


par
| Word
| > | Merci à vous tous, Youky
| > |
| > | "ng" a écrit dans le message news:
| > |
| > | > Salut,
| > | >
| > | > Envoyer le projet avec aurait été plus astucieux, car la il faut
| recrer un
| > | > projet, ajouter l'objet rtfbox, rajouter ta form... sinon on a le
| droit à
| > | > une zolie erreur...
| > | >
| > | > En plus y a des goto partout :)
| > | >
| > | > --
| > | > Nicolas G.
| > | > FAQ VB : http://faq.vb.free.fr
| > | > API Guide : http://www.allapi.net
| > | > Google Groups : http://groups.google.fr/
| > | > MZ-Tools : http://www.mztools.com/
| > | >
| > | > LE TROLL wrote:
| > | > > Salut, ça vaut ce que ça vaut, lol, mais ça marche,
| > | > > dis-moi si c'est ça que tu veux???
| > | > > Ci-joitn en fichier aussi (form + fichier rtf)-> vb6
| > | > > -------
| > | > > Routines avec RTF (impression, recherche, poursuite de a
| > | > > recherche)
| > | > >
| > | > > OBJETS
| > | > > -1-
| > | > > aideF = rtf (fichier Aide.rtf) incorporé à l'exe, chargé
| > | > > dans FileName = path + Aide.rtf
| > | > > -2-
| > | > > textBox = quoi-cherche
| > | > > -3- menus
| > | > > m_cherche
| > | > > m_poursuivre
| > | > > m_imp
| > | > > m_aide
| > | > >
| > | > >
| > | > > ' aide
| > | > > '
| > | > > Dim ou As Long
| > | > > Dim ici As Long
| > | > > Dim recherche As String
| > | > > '
| > | > > Sub Form_Load()
| > | > > ' éditeur du logiciel pour lien Internet...
| > | > > If Form1.BRIDE = False Then: Form11.Caption > | > | > > Form1.titre
| > | > > If Form1.BRIDE = True Then: Form11.Caption > | > | > > Form1.titre_bride
| > | > > '
| > | > > Label1.Visible = False
| > | > > quoi_cherche.Visible = False
| > | > > End Sub
| > | > >
| > | > > Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) '
| > | > > touches
| > | > > If KeyCode = 113 Then: Call va_recherche 'F2
| > | > > If KeyCode = 114 Then: Call va_continue 'F3
| > | > > End Sub
| > | > >
| > | > > Sub m_recherche_Click()
| > | > > Call va_recherche
| > | > > End Sub
| > | > >
| > | > > Sub va_recherche() 'recherche
| > | > > Dim quoi As Integer
| > | > > '
| > | > > On Error Resume Next
| > | > > quoi_cherche = ""
| > | > > recherche = ""
| > | > > Label1.Visible = False
| > | > > quoi_cherche.Visible = False
| > | > > '
| > | > > recherche = InputBox("Aide" + Chr(10) + "Saisir le
| > | > > texte recherché ?")
| > | > > '
| > | > > quoi = Len(recherche)
| > | > > If quoi = 0 Then: Exit Sub
| > | > > '
| > | > > Label1.Visible = True
| > | > > quoi_cherche.Visible = True
| > | > > '
| > | > > quoi_cherche.Text = recherche 'affiche recherche
| > | > > ou = aideF.Find(recherche, 0, , 2 Or 8)
| > | > > aideF.SetFocus
| > | > > If ou = (-1) Then
| > | > > MsgBox "Texte non trouvé", 48
| > | > > Exit Sub
| > | > > End If
| > | > > aideF.SelStart = ou ' n°octet texte
| > | > > aideF.Span " ,;:.?!+*-/=", True, True
| > | > > End Sub
| > | > >
| > | > > Sub m_poursuivre_Click()
| > | > > Call va_continue
| > | > > End Sub
| > | > >
| > | > > Sub va_continue() 'continue la recherche
| > | > > Dim quoi As Integer
| > | > > '
| > | > > If quoi_cherche = "" Then
| > | > > MsgBox "Aucune chaîne n'est en recherche", 48
| > | > > Exit Sub
| > | > > End If
| > | > > On Error Resume Next
| > | > > aideF.SetFocus
| > | > > aideF.SelStart = ou
| > | > > ou = ou + 1
| > | > > ici = aideF.Find(recherche, ou, , 2 Or 8)
| > | > > If ici = (-1) Then: MsgBox "Texte non trouvé", 48
| > | > > ou = ici
| > | > > aideF.SelStart = ou
| > | > > aideF.Span " ,;:.?!+*-/=", True, True
| > | > > End Sub
| > | > >
| > | > > Sub m_aide_Click()
| > | > > MsgBox "RECHERCHER" + Chr(10) + "Cherche le mot saisi
| > | > > (du curseur vers le bas)." + Chr(10) + "Vouv pouvez dans la
| > | > > table des recherches ou dans la totalité de l'aide, copier
| > | > > un texte et le coller dans la boîte de saisie des
| > | > > recherches." + Chr(10) + Chr(10) + "POURSUIVRE LA RECHERCHE"
| > | > > + Chr(10) + "Continue la recherche demandée." + Chr(10) +
| > | > > Chr(10) + "IMPRESSION" + Chr(10) + "Outre l'impression
| > | > > papier prévue dans le menu haut, à l'aide de la souris vous
| > | > > pouvez sélectionner le texte désiré, le copier, puis le
| > | > > coller dans une application Windows pour impression.", 64,
| > | > > "Aide sur l'aide"
| > | > > End Sub
| > | > >
| > | > > Sub m_imp_Click() ' impression papier RTF géré en lignes,
| > | > > colonnes et blancs insécables...
| > | > > Dim maxi As Long
| > | > > Dim combien As Long
| > | > > Dim avance As Long
| > | > > Dim texte_1 As String
| > | > > Dim texte_2 As String
| > | > > Dim o_s, p_s As String * 1
| > | > > Dim o_n As Integer
| > | > > Dim b As Long
| > | > > '
| > | > > reponse = MsgBox("Confirmez l'impression", vbQuestion +
| > | > > vbYesNo + vbDefaultButton2)
| > | > > If reponse <> vbYes Then Exit Sub
| > | > > '
| > | > > texte_1 = aideF.Text ' balance le contenu dans
| > | > > variable aideF = RTF
| > | > > maxi = Len(texte_1) ' taille tu texte
| > | > > combien = 1
| > | > > avance = 1
| > | > > For b = 1 To maxi
| > | > > o_s = Mid(texte_1, b, 1) ' extraction de chaque
| > | > > octet
| > | > > texte_2 = texte_2 + o_s ' sauvegarde text_1 dans
| > | > > text_2
| > | > > o_n = Asc(o_s) ' contrôle la valeur ascii de
| > | > > l'octet extrait
| > | > > If o_n = 13 Then: combien = 0 ' saut de ligne
| > | > > If combien > 64 And o_n = 32 Then ' saut de page
| > | > > '
| > | > > If o_n = 32 Then ' gestion blanc insécable
| > | > > o_s = Mid(texte_1, b + 1, 1)
| > | > > If o_s = "?" Or p_s = "!" Or o_s = ":" Or o_s
| > | > > = ";" Then
| > | > > texte_2 = texte_2 + o_s
| > | > > b = b + 1
| > | > > GoTo ici
| > | > > End If
| > | > > End If
| > | > > '
| > | > > texte_2 = texte_2 + Chr(13)
| > | > > texte_2 = texte_2 + Chr(10)
| > | > > combien = 0
| > | > > End If
| > | > > combien = combien + 1
| > | > > avance = avance + 1
| > | > > ici:
| > | > > Next b
| > | > > '''''''''''''''''''''
| > | > > Printer.FontName = "courrier new"
| > | > > Printer.FontSize = 12
| > | > > Printer.Print texte_2
| > | > > Printer.EndDoc
| > | > > End Sub
| > | > >
| > | > > Sub Form_Unload(Cancel As Integer) ' end_système + <alt>_F4
| > | > > Cancel = Cancel - 1
| > | > > Call ends
| > | > > End Sub
| > | > >
| > | > > Sub ends()
| > | > > Form11.Hide
| > | > > Unload Form11
| > | > > Form1.SetFocus
| > | > > End Sub
| > | > > ---------------
| > | > >
| > | > >
| > | > > "Barsalou" a écrit
| > | > > dans le message de news:
| > | > >
| > | > >> Je suis également intéressé.
| > | > >> Peut-être devrais-tu mettre ton code sur le forum.
| > | >
| > | >
| > |
| > |
| >
| >
|
|




1 2 3