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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
'Baric est un exercice de construction d'une barre d'outils.
'L'objectif est de montrer comment contrôler des images
'pour leur donner un look de boutons de commandes.
'***************************************************************
'HCath - 12/9/2002 - h.cathelineau@infonie.fr
'***************************************************************
'Pour tester Baric, démarrer un nouveau projet avec deux
'PictureBox et coller le code.
'***************************************************************
'La structure suivante sera utilisée pour décrire chaque image :
'son titre, sa position X0 dans la banque d'images (Picture2), sa
'largeur W0 et sa position logique X1 puis définitive XD dans la
'barre d'outils.
'Dans le cadre d'une barre d'outils plus sophistiquée, on pourra
'compléter la structure avec, par exemple d'autres chaînes de
'caractères ou la position verticale (Y0 - Y1) de l'image et sa
'hauteur (H0) si l'on veut construire deux étages d'images ou
'montrer des commandes de tailles variées.
Private Type CMDIMG
TitreImg As String
Msg As String
X0 As Long
W0 As Long
X1 As Long
XD As Long
End Type
'L'API BitBlt est le moteur de cette démonstration. Elle va recopier
'sur la barre (Picture1) les images contenues dans Picture2 en fonction
'des mouvements de la souris.
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
'Tableau Img() - il contiendra les coordonnées source et destination de
'chaque image.
Dim Img() As CMDIMG
'IndexImg est l'identifiant de la dernière image modifiée en réponse
'à un mouvement de la souris.
Dim IndexImg As Integer
Private Sub Form_Load()
Dim H As Long, L As Long, Couleur As Long
Dim I As Integer
'Description des images
ReDim Img(4)
Img(0).TitreImg = "France"
Img(0).Msg = "La France est un grand pays, cliquez !"
Img(0).X0 = 0
Img(0).W0 = 20
Img(0).X1 = 3
Img(1).TitreImg = "Belgique"
Img(1).Msg = "Small is beautiful"
Img(1).X0 = 20
Img(1).W0 = 20
Img(1).X1 = -23
Img(2).TitreImg = "Suivez le guide..."
Img(2).Msg = "Voyez mes images"
Img(2).X0 = 40
Img(2).W0 = 40
Img(2).X1 = 63
Img(3).TitreImg = "Barre à gauche, Toute !"
Img(3).Msg = "On ne montre pas les coulisses"
Img(3).X0 = 80
Img(3).W0 = 40
Img(3).X1 = -103
Img(4).TitreImg = "Heureux ?"
Img(4).Msg = "Attention, ne cliquez pas, ça va fermer !"
Img(4).X0 = 120
Img(4).W0 = 30
Img(4).X1 = 10000 - 15
With Me
.Caption = "Baric"
.BackColor = vbWhite
.Width = Screen.TwipsPerPixelX * 480
.FontSize = 14
End With
With Picture1
.Appearance = 0
.BorderStyle = 0
.BackColor = RGB(192, 192, 192)
.AutoRedraw = True
.ScaleMode = vbPixels
.Top = 0
.Left = 0
End With
With Picture2
.Appearance = 0
.BorderStyle = 0
.Width = 3000
.Height = 900
.Left = 0
.BackColor = vbWhite
.AutoRedraw = True
.ScaleMode = vbPixels
.Visible = False
End With
'Dessin des images
'Pour rendre la démonstration autonome, on dessine quelques
'images dans Picture2.
'(cette étape n'est pas nécessaire si l'on dispose d'un
'fichier BMP chargé dans Picture2).
For I = 0 To 1
L = I * 20
H = 0
Do While H < 60
Picture2.Line (0 + L, 0 + H)-(19 + L, 19 + H), RGB(192, 192, 192), BF
If H = 20 Then
Picture2.Line (0 + L, 0 + H)-(19 + L, 19 + H), RGB(128, 128, 128), B
Picture2.Line (0 + L, 0 + H)-(19 + L, 0 + H), vbWhite
Picture2.Line (0 + L, 0 + H)-(0 + L, 19 + H), vbWhite
ElseIf H = 40 Then
Picture2.Line (0 + L, 0 + H)-(19 + L, 19 + H), RGB(160, 160, 160), BF
Picture2.Line (0 + L, 0 + H)-(19 + L, 19 + H), vbWhite, B
Picture2.Line (0 + L, 0 + H)-(19 + L, 0 + H), RGB(128, 128, 128)
Picture2.Line (0 + L, 0 + H)-(0 + L, 19 + H), RGB(128, 128, 128)
End If
If I = 0 Then
Picture2.Line (3 + L, 5 + H)-(16 + L, 14 + H), 0, B
Picture2.Line (4 + L, 6 + H)-(7 + L, 13 + H), vbBlue, BF
Picture2.Line (8 + L, 6 + H)-(11 + L, 13 + H), vbWhite, BF
Picture2.Line (12 + L, 6 + H)-(15 + L, 13 + H), vbRed, BF
ElseIf I = 1 Then
Picture2.Line (3 + L, 5 + H)-(16 + L, 14 + H), 0, B
Picture2.Line (4 + L, 6 + H)-(7 + L, 13 + H), 0, BF
Picture2.Line (8 + L, 6 + H)-(11 + L, 13 + H), vbYellow, BF
Picture2.Line (12 + L, 6 + H)-(15 + L, 13 + H), vbRed, BF
End If
H = H + 20
Loop
Next
L = 40
For I = 0 To 1
H = 0
Couleur = RGB(0, 0, 128)
Do While H < 60
Picture2.Line (0 + L, 0 + H)-(39 + L, 19 + H), RGB(192, 192, 192), BF
If H = 20 Then
Picture2.Line (0 + L, 0 + H)-(39 + L, 19 + H), RGB(128, 128, 128), B
Picture2.Line (0 + L, 0 + H)-(39 + L, 0 + H), vbWhite
Picture2.Line (0 + L, 0 + H)-(0 + L, 19 + H), vbWhite
Couleur = vbRed
ElseIf H = 40 Then
Picture2.Line (0 + L, 0 + H)-(39 + L, 19 + H), RGB(160, 160, 160), BF
Picture2.Line (0 + L, 0 + H)-(39 + L, 19 + H), vbWhite, B
Picture2.Line (0 + L, 0 + H)-(39 + L, 0 + H), RGB(128, 128, 128)
Picture2.Line (0 + L, 0 + H)-(0 + L, 19 + H), RGB(128, 128, 128)
Couleur = RGB(192, 0, 0)
End If
If I = 0 Then
Picture2.Line (5 + L, 8 + H)-(26 + L, 10 + H), Couleur, BF
Picture2.Line (27 + L, 6 + H)-(28 + L, 12 + H), Couleur, BF
Picture2.Line (29 + L, 7 + H)-(30 + L, 11 + H), Couleur, BF
Picture2.Line (31 + L, 8 + H)-(32 + L, 10 + H), Couleur, BF
Picture1.Line (33 + L, 9 + H)-(35 + L, 9 + H), Couleur
Else
StretchBlt Picture2.hdc, 117, 2 + H, -36, 16, Picture2.hdc, 42, 2 + H, 36, 16, vbSrcCopy
Picture2.Refresh
End If
H = H + 20
Loop
L = L + 40
Next
L = 120
H = 0
Do While H < 60
Picture2.Line (0 + L, 0 + H)-(29 + L, 19 + H), RGB(192, 192, 192), BF
If H = 20 Then
Picture2.Line (0 + L, 0 + H)-(29 + L, 19 + H), RGB(128, 128, 128), B
Picture2.Line (0 + L, 0 + H)-(29 + L, 0 + H), vbWhite
Picture2.Line (0 + L, 0 + H)-(0 + L, 19 + H), vbWhite
ElseIf H = 40 Then
Picture2.Line (0 + L, 0 + H)-(29 + L, 19 + H), RGB(160, 160, 160), BF
Picture2.Line (0 + L, 0 + H)-(29 + L, 19 + H), vbWhite, B
Picture2.Line (0 + L, 0 + H)-(29 + L, 0 + H), RGB(128, 128, 128)
Picture2.Line (0 + L, 0 + H)-(0 + L, 19 + H), RGB(128, 128, 128)
End If
Picture2.FillStyle = 0
Picture2.FillColor = vbYellow
Picture2.Circle (14 + L, 9 + H), 7, 0, , , 1
Picture2.FillStyle = 1
Picture2.Line (11 + L, 7 + H)-(12 + L, 8 + H), 0, BF
Picture2.Line (16 + L, 7 + H)-(17 + L, 8 + H), 0, BF
If H = 0 Then
Picture2.Circle (14 + L, 7 + H), 6, 0, 4, 5.5, 1
ElseIf H = 20 Then
Picture2.Line (12 + L, 12 + H)-(17 + L, 12 + H), 0
Else
Picture2.Circle (14 + L, 17 + H), 6, 0, 1, 2.5, 1
End If
H = H + 20
Loop
IndexImg = -1
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If IndexImg > -1 Then
BitBlt Picture1.hdc, Img(IndexImg).XD, 3, Img(IndexImg).W0, 20, Picture2.hdc, Img(IndexImg).X0, 0, vbSrcCopy
Picture1.Refresh
Picture1.ToolTipText = ""
IndexImg = -1
End If
End Sub
Private Sub Form_Resize()
If Me.WindowState = 1 Then Exit Sub
Dim Largeur As Long, Hauteur As Long
Dim TPX As Long, TPY As Long
TPX = Screen.TwipsPerPixelX
TPY = Screen.TwipsPerPixelY
Picture1.Width = Me.ScaleWidth
Picture1.Height = 25 * TPY
Picture1.Cls
Largeur = Me.ScaleWidth \ TPX
Hauteur = Picture1.ScaleHeight
'Dessin du cadre
Picture1.Line (Largeur - 1, 1)-(Largeur - 1, Hauteur - 1), RGB(128, 128, 128)
Picture1.Line (1, Hauteur - 1)-(Largeur - 1, Hauteur - 1), RGB(128, 128, 128)
Picture1.Line (1, 1)-(Largeur - 1, 1), vbWhite
Picture1.Line (1, 1)-(1, Hauteur - 1), vbWhite
'Affichage des images dans le cadre
For I = 0 To UBound(Img)
If Img(I).X1 < 0 Then
'alignement à droite si X1 négatif
Img(I).XD = Img(I).X1 + Largeur
ElseIf Img(I).X1 > 5000 Then
'Centrage
Img(I).XD = Img(I).X1 - 10000 + (Largeur \ 2)
Else
'alignement à gauche
Img(I).XD = Img(I).X1
End If
BitBlt Picture1.hdc, Img(I).XD, 3, Img(I).W0, 20, Picture2.hdc, Img(I).X0, 0, vbSrcCopy
Next
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim I As Integer, J As Integer
'Cherche à quelle image correspond la position du pointeur
J = -1
For I = 0 To UBound(Img)
If X > Img(I).XD And X < Img(I).XD + Img(I).W0 Then
J = I
Exit For
End If
Next
'Rétablit l'affichage neutre de l'image précédente
If IndexImg > -1 And IndexImg <> J Then
BitBlt Picture1.hdc, Img(IndexImg).XD, 3, Img(IndexImg).W0, 20, Picture2.hdc, Img(IndexImg).X0, 0, vbSrcCopy
Picture1.Refresh
IndexImg = -1
End If
'Affichage 3D de l'image cliquée
If J > -1 Then
BitBlt Picture1.hdc, Img(J).XD, 3, Img(J).W0, 20, Picture2.hdc, Img(J).X0, 40, vbSrcCopy
Picture1.Refresh
IndexImg = J
End If
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim I As Integer, J As Integer
'Cherche à quelle image correspond la position du pointeur
J = -1
For I = 0 To UBound(Img)
If X > Img(I).XD And X < Img(I).XD + Img(I).W0 Then
J = I
Exit For
End If
Next
'Rétablit l'affichage neutre de l'image précédente
If IndexImg > -1 And IndexImg <> J Then
BitBlt Picture1.hdc, Img(IndexImg).XD, 3, Img(IndexImg).W0, 20, Picture2.hdc, Img(IndexImg).X0, 0, vbSrcCopy
Picture1.Refresh
Picture1.ToolTipText = ""
IndexImg = -1
End If
'Affichage 3D de l'image survolée
If J > -1 And IndexImg <> J Then
BitBlt Picture1.hdc, Img(J).XD, 3, Img(J).W0, 20, Picture2.hdc, Img(J).X0, 20, vbSrcCopy
Picture1.Refresh
Picture1.ToolTipText = Img(J).TitreImg
IndexImg = J
Me.Cls
Me.CurrentX = (Me.ScaleWidth - Me.TextWidth(Img(J).Msg)) \ 2
Me.CurrentY = Me.ScaleHeight * 0.75
Me.Print Img(J).Msg
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If IndexImg > -1 Then
'Rétablit l'affichage 3D de l'image cliquée
BitBlt Picture1.hdc, Img(IndexImg).XD, 3, Img(IndexImg).W0, 20, Picture2.hdc, Img(IndexImg).X0, 20, vbSrcCopy
Picture1.Refresh
'Actions des commandes
Select Case Img(IndexImg).TitreImg
Case "Suivez le guide..."
Picture2.Top = Screen.TwipsPerPixelY * 40
Picture2.Left = (Me.ScaleWidth - (Screen.TwipsPerPixelX * 150)) \ 2
Picture2.Visible = True
Case "Barre à gauche, Toute !"
Picture2.Visible = False
Case "France"
Me.WindowState = 2
Case "Belgique"
Me.WindowState = 0
Case "Heureux ?"
Unload Me
End
Case Else
End Select
End If
End Sub |
Partager