cs_DARKSIDIOUS
Messages postés
15814
Date d'inscription
jeudi 8 août 2002
Statut
Membre
Dernière intervention
4 mars 2013
130
4 sept. 2005 à 18:12
Alors, voici le code au complet :
A mettre dans un module :
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" ( _
ByVal hdc As Long, _
ByVal lpStr As String, _
ByVal nCount As Long, _
lpRect As RECT, _
ByVal wFormat As Long) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Const DT_TOP As Long = &H0
Private Const DT_BOTTOM As Long = &H8
Private Const DT_CENTER As Long = &H1
Private Const DT_VCENTER As Long = &H4
Private Const DT_LEFT As Long = &H0
Private Const DT_RIGHT As Long = &H2
Private Const DT_SINGLELINE As Long = &H20
Private Const DT_MULTILINE As Long = (&H1)
Private Const DT_WORDBREAK As Long = &H10
Public Sub PrintText(ByRef oPrinter As Object, ByVal sText As String,
ByVal lLeft As Long, ByVal lTop As Long, ByVal lWidth As Long, ByVal
lHeight As Long, ByVal lAlignment As Long, Optional ByVal bMultiLine As
Boolean = False)
'on active la routine de traitement d'erreur
On Error Resume Next
'l'erreur etant minime, on continue l'éxécution normalement
'déclaration des variables privées
Dim iAlignmentX As Integer 'stocke l'alignement sur l'axe X
Dim iAlignmentY As Integer 'stocke l'alignement sur l'axe Y
Dim pRect As RECT 'stocke le rectangle contenant le texte
Let iAlignmentY = CInt(lAlignment / 3)
Let iAlignmentX = lAlignment Mod 3
Select Case lAlignment
Case 0, 1, 2
Let iAlignmentY = DT_TOP
Case 3, 4, 5
Let iAlignmentY = DT_VCENTER
Case 6, 7, 8
Let iAlignmentY = DT_BOTTOM
End Select
Select Case lAlignment
Case 0, 3, 6
Let iAlignmentX = DT_LEFT
Case 1, 4, 7
Let iAlignmentX = DT_RIGHT
Case 2, 5, 8
Let iAlignmentX = DT_CENTER
End Select
Call DrawText(oPrinter.hdc, sText, Len(sText), pRect, iAlignmentX
Or iAlignmentY Or IIf(bMultiLine, DT_WORDBREAK, DT_SINGLELINE))
End Sub
Et pour l'utiliser, il te suffit d'appeler la fonction comme ceci (tu
peux mettre l'instruction dans un événement de bouton_click par
exemple) :
Call PrintText(Printer, Text1.Text, 0, 0, Text1.Width, Text1.Height, Text1.Alignment, True)
La fonction est intégrée dans le système Windows depuis Windows 3.1,
donc tu ne devrais rencontrer aucun problème de compatibilité (elle
marche parfaitement sous Win 95, 98, 2000 et Xp en tout cas !).
DarK Sidious
Un API Viewer (pour le VB, VB.NET, C, C# et Delphi) : www.ProgOtoP.com/popapi/