1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| Option Explicit
Private Const DT_BOTTOM = &H8
Private Const DT_CALCRECT = &H400
Private Const DT_CENTER = &H1
Private Const DT_EXPANDTABS = &H40
Private Const DT_EXTERNALLEADING = &H200
Private Const DT_LEFT = &H0
Private Const DT_NOCLIP = &H100
Private Const DT_NOPREFIX = &H800
Private Const DT_RIGHT = &H2
Private Const DT_SINGLELINE = &H20
Private Const DT_TABSTOP = &H80
Private Const DT_TOP = &H0
Private Const DT_VCENTER = &H4
Private Const DT_WORDBREAK = &H10
Private Const PS_NULL = 5
Private Const PS_INSIDEFRAME = 6
Private Const PS_SOLID = 0
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
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 Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function CopyRect Lib "user32" (lpDestRect As RECT, lpSourceRect As RECT) As Long
Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Enum HorzAlign
HORZ_LEFT = 0
HORZ_CENTER = 1
HORZ_RIGHT = 2
End Enum
Private Enum VertAlign
VERT_TOP = 0
VERT_CENTER = 1
VERT_BOTTOM = 2
End Enum
Private Sub Command1_Click()
Dim Txt As String
Dim Rct As RECT
Dim Horz As HorzAlign
Dim Vert As VertAlign
MousePointer = 11: DoEvents
'on prend comme rectangle la surface client du picturebox
GetClientRect Picture1.hwnd, Rct
Txt = "Exemple de texte simple"
DessineTout Txt, Rct
Txt = "Exemple de " & vbCrLf & "texte multilignes"
DessineTout Txt, Rct
'on definit un rectangle plus petit à l'interieur du picturebox
Rct.Top = 100
Rct.Left = 100
Rct.Bottom = 200
Rct.Right = 300
Txt = "Exemple de texte simple"
DessineTout Txt, Rct
Txt = "Exemple de " & vbCrLf & "texte multilignes"
DessineTout Txt, Rct
MousePointer = 0
Unload Me
End Sub
Private Sub Form_Load()
Me.Font = 20
Me.Move 0, 0, 6000, 6000
Picture1.Move 100, 100, 5000, 4000
Command1.Move 100, 4500
End Sub
Private Sub DessineTout(Txt As String, Rct As RECT)
'Execute la commande dessine pour les neuf possibilités d'alignement
Dim Horz As HorzAlign
Dim Vert As VertAlign
Vert = VERT_TOP ' en haut
Horz = HORZ_LEFT: Dessine Txt, Rct, Horz, Vert 'a gauche
Horz = HORZ_CENTER: Dessine Txt, Rct, Horz, Vert 'au centre
Horz = HORZ_RIGHT: Dessine Txt, Rct, Horz, Vert 'a droite
Vert = VERT_CENTER 'au milieu
Horz = HORZ_LEFT: Dessine Txt, Rct, Horz, Vert 'a gauche
Horz = HORZ_CENTER: Dessine Txt, Rct, Horz, Vert 'au centre
Horz = HORZ_RIGHT: Dessine Txt, Rct, Horz, Vert 'a droite
Vert = VERT_BOTTOM 'en bas
Horz = HORZ_LEFT: Dessine Txt, Rct, Horz, Vert 'a gauche
Horz = HORZ_CENTER: Dessine Txt, Rct, Horz, Vert 'au centre
Horz = HORZ_RIGHT: Dessine Txt, Rct, Horz, Vert 'a droite
End Sub
Private Sub Dessine(Txt As String, Rct As RECT, Horz As HorzAlign, Vert As VertAlign)
Dim CalcRect As RECT
Dim HauteurTexte As Long
Dim Flag As Long
'on efface le picturebox
Picture1.Cls
'on dessine le rectangle
Rectangle Picture1.hdc, Rct.Left, Rct.Top, Rct.Right, Rct.Bottom
'On copie le rectangle dans un autre rectangle qui servira pour les calculs
CopyRect CalcRect, Rct
'Calcul de la Hauteur du Texte
DrawText Picture1.hdc, Txt, Len(Txt), CalcRect, DT_LEFT Or DT_WORDBREAK Or DT_CALCRECT
HauteurTexte = CalcRect.Bottom - CalcRect.Top
'On copie le rectangle dans un autre rectangle qui servira pour les calculs
CopyRect CalcRect, Rct
'calcul du flag
Select Case Horz
Case HORZ_LEFT
Flag = DT_LEFT Or DT_WORDBREAK
Case HORZ_CENTER
Flag = DT_CENTER Or DT_WORDBREAK
Case Else
Flag = DT_RIGHT Or DT_WORDBREAK
End Select
'
Select Case Vert
Case VERT_TOP
'
Case VERT_CENTER
CalcRect.Top = CalcRect.Top + ((Rct.Bottom - Rct.Top) - HauteurTexte) \ 2
Case Else
CalcRect.Top = CalcRect.Bottom - HauteurTexte
End Select
CalcRect.Bottom = CalcRect.Top + HauteurTexte
DrawText Picture1.hdc, Txt, Len(Txt), CalcRect, Flag
'on attend une seconde pour laisser le temps de voir
Sleep 1000
End Sub |
Partager