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
|
'PICTUREBOX AVEC SCROLLBARS
'AJOUTER:
'-UN PICTUREBOX
'-UN BOUTON POUR CHARGE UNE IMAGE
'-RAJOUTER 2 SCROLLBARS H ET V DE LA BOITE À OUTILS
Public Class frmPicAvecScrollBar
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'ajoute une sub ScrollBars_Scroll handler pour les 2 scrollbars(vertical et horizontal) pris de la boite à outils
AddHandler HScrollBar1.Scroll, AddressOf ScrollBars_Scroll
AddHandler VScrollBar1.Scroll, AddressOf ScrollBars_Scroll
End Sub
'ic notre Sub ScrollBars_Scroll() handler quand on deplace les
'scrollbars
Public Sub ScrollBars_Scroll(ByVal sender As Object, _
ByVal se As ScrollEventArgs)
Dim graphics As Graphics = PictureBox1.CreateGraphics()
'rectangle destination sert à cadrer la zone à redessiner de l'image
'quand on scrolle vert ou horiz.
'dim. zone à redessiner = dimension picturbox - valeur de scroll
Dim monRect As Rectangle = New Rectangle(0, 0, _
PictureBox1.Width - HScrollBar1.Height, _
PictureBox1.Height - VScrollBar1.Width)
'DrawImage recopie partie d'image
'dans rect destination(monrect) à partir de partie rect source (PictureBox1.Image),unites(pixel)
graphics.DrawImage(PictureBox1.Image, monRect, _
New Rectangle(HScrollBar1.Value, VScrollBar1.Value, _
PictureBox1.Width - HScrollBar1.Height, _
PictureBox1.Height - VScrollBar1.Width), GraphicsUnit.Pixel)
End Sub
Private Sub btnChargeImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChargeImage.Click
If OpenFileDialog1.ShowDialog() <> DialogResult.Cancel Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
'la valeur maxi atteinte par les 2 scrollbars ne doit pas
'depasser la difference =dimensions picturebox - dimensions image
HScrollBar1.Maximum = PictureBox1.Image.Width - _
PictureBox1.Width
VScrollBar1.Maximum = PictureBox1.Image.Height - _
PictureBox1.Height
ShowScrollBars()
End If
End Sub
'cette sub montre les scrollbars si les dimensions de l'image charge
'sont plus grandes que les dimension du picturebox
Private Sub ShowScrollBars()
VScrollBar1.Visible = True
HScrollBar1.Visible = True
If PictureBox1.Height > PictureBox1.Image.Height Then
VScrollBar1.Visible = False
End If
If PictureBox1.Width > PictureBox1.Image.Width Then
HScrollBar1.Visible = False
End If
End Sub
End Class |
Partager