antoineraymond
Messages postés99Date d'inscriptionmardi 6 avril 2004StatutMembreDernière intervention 4 décembre 2008
-
20 mars 2008 à 18:25
imajneb
Messages postés9Date d'inscriptionsamedi 1 février 2003StatutMembreDernière intervention10 mars 2010
-
28 juil. 2010 à 16:45
Bonjour
Dans une de mes applications, j'ai besoin d'imprimer automatiquement les exceptions.
Voici une partie du code que j'utilise:
Dim
WithEvents m_prdImprime
As Printing.PrintDocument
Dim m_sbrMsg
As System.Text.StringBuilder
Public
Const CrLf2
As
String = vbCrLf & vbCrLf
Private
Sub mprdImprime_PrintPage(
ByVal sender
As
Object,
ByVal e
As System.Drawing.Printing.PrintPageEventArgs)
Handles m_prdImprime.PrintPage
e.Graphics.DrawString(m_sbrMsg.ToString,
New Font(
"Courier New", 8), Brushes.Black, 50, 50)
End
SubPrivate
Sub PrintErreur(
ByVal Erreur
As Exception)
m_sbrMsg =
New System.Text.StringBuilder
Dim m_prdImprime
As
New Printing.PrintDocument
m_prdImprime.DefaultPageSettings.Landscape =
False
m_prdImprime.Print()
End
SubLe problème est que la partie Erreur.ToString est trop large pour la page. À la place de changer de ligne à l'impression, le texte est tout simplement coupé. J'aimerais savoir si il y a une façon de forcer le changement de ligne du texte trop long selon les dimensions de la page ou si vous avez une autre solution?
imajneb
Messages postés9Date d'inscriptionsamedi 1 février 2003StatutMembreDernière intervention10 mars 2010 28 juil. 2010 à 16:45
Salut,
Voici un exemple de code qui analyse le texte mot par mot afin de faire un retour a la ligne si le texte sort de la page:
'//Dessine le texte multiligne
Private Sub DrawStringMultiLine(ByVal MyTxt As String,ByVal Left As short, ByVal Top As short, ByVal g As Graphics)
Dim Xmax As Integer = Form1.PageSetupDialog1.PageSettings.PaperSize.Width - Form1.PageSetupDialog1.PageSettings.Margins.Right()
Dim Text_size As SizeF
Dim i As Integer, Tab() As String
Dim Font As System.Drawing.Font = New Font("Arial", 10, FontStyle.Bold)
Dim X As Single Left, Spacing As Single 1.5 'Intervale entre deux lignes
Dim YShift As Single = 0 'Décalage sur Y des lignes
Dim drawFormat As New StringFormat
drawFormat.Alignment = 0
drawFormat.Trimming = StringTrimming.Character
drawFormat.FormatFlags = StringFormatFlags.LineLimit
drawFormat.Trimming = StringTrimming.Word
'Si la chaine est trop longue, multiligne
If g.MeasureString(MyTxt, Font).Width + X - Xmax >= 0 Then
'Met la ligne dans un tableau séparé par des espaces
Tab = MyTxt.Split(" ")
MyTxt = Tab(0)
For i = 1 To UBound(Tab) 'On ajoute les mots un par un en messurant
'On messure la chaine de caractere
Text_size = g.MeasureString(MyTxt & " " & Tab(i), Font)
If Text_size.Width + X - Xmax >= 0 Then 'Si rentre pas, on dessine la ligne
g.DrawString(MyTxt, Font, Brushes.Black, Left, Top + YShift, drawFormat )
YShift = YShift + Spacing
MyTxt = Tab(i)
Else
'on ajoute le mot
MyTxt = MyTxt & " " & Tab(i)
End If
Next
End If
'Dessine la derniere ligne avant de sortir
g.DrawString(MyTxt, Ctl.Font, Brushes.Black, Left, Top + YShift, drawFormat )