home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / getwin1r / zoom.frm (.txt) < prev   
Encoding:
Visual Basic Form  |  1999-09-07  |  11.9 KB  |  299 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.1#0"; "COMDLG32.OCX"
  3. Begin VB.Form FormView 
  4.    Appearance      =   0  'Flat
  5.    BackColor       =   &H80000005&
  6.    BorderStyle     =   1  'Fixed Single
  7.    Caption         =   "View Picture With ZOOM"
  8.    ClientHeight    =   3840
  9.    ClientLeft      =   45
  10.    ClientTop       =   615
  11.    ClientWidth     =   4455
  12.    LinkTopic       =   "Form1"
  13.    MaxButton       =   0   'False
  14.    ScaleHeight     =   256
  15.    ScaleMode       =   3  'Pixel
  16.    ScaleWidth      =   297
  17.    StartUpPosition =   2  'CenterScreen
  18.    Begin MSComDlg.CommonDialog CommonDialog1 
  19.       Left            =   2040
  20.       Top             =   1680
  21.       _ExtentX        =   847
  22.       _ExtentY        =   847
  23.       _Version        =   327681
  24.    End
  25.    Begin VB.PictureBox Picture1 
  26.       BackColor       =   &H80000010&
  27.       BorderStyle     =   0  'None
  28.       Height          =   3615
  29.       Left            =   0
  30.       ScaleHeight     =   241
  31.       ScaleMode       =   3  'Pixel
  32.       ScaleWidth      =   281
  33.       TabIndex        =   2
  34.       Top             =   0
  35.       Width           =   4215
  36.       Begin VB.Image Image1 
  37.          Enabled         =   0   'False
  38.          Height          =   1410
  39.          Left            =   0
  40.          Stretch         =   -1  'True
  41.          Top             =   0
  42.          Width           =   1260
  43.       End
  44.    End
  45.    Begin VB.VScrollBar VScroll1 
  46.       Height          =   3615
  47.       Left            =   4200
  48.       TabIndex        =   1
  49.       Top             =   0
  50.       Width           =   255
  51.    End
  52.    Begin VB.HScrollBar HScroll1 
  53.       Height          =   255
  54.       Left            =   0
  55.       TabIndex        =   0
  56.       Top             =   3600
  57.       Width           =   4215
  58.    End
  59.    Begin VB.Image Image2 
  60.       Height          =   615
  61.       Left            =   5040
  62.       Top             =   840
  63.       Visible         =   0   'False
  64.       Width           =   615
  65.    End
  66.    Begin VB.Menu mnu_file 
  67.       Caption         =   "&File"
  68.       Begin VB.Menu mnu_picture 
  69.          Caption         =   "Open Picture"
  70.       End
  71.       Begin VB.Menu mnu_spacer 
  72.          Caption         =   "-"
  73.       End
  74.       Begin VB.Menu mnu_exit 
  75.          Caption         =   "E&xit"
  76.       End
  77.    End
  78. Attribute VB_Name = "FormView"
  79. Attribute VB_GlobalNameSpace = False
  80. Attribute VB_Creatable = False
  81. Attribute VB_PredeclaredId = True
  82. Attribute VB_Exposed = False
  83.    Dim TX As Long
  84.    Dim TY As Long
  85.    Dim ZoomDepth As Long
  86.    Private Sub Form_Load()
  87.        'All computer screens (monitors) are NOT the same so
  88.        'we must account for that, and ensure that our
  89.        'software will work properly for every user.
  90.        'Below we set the TX and TY as the first piece of
  91.        'code to be executed.
  92.        'Our Form and PICTURE scalemodes are set at "3" or
  93.        'PIXEL, but when calculating measurements in VB
  94.        'we need to use their dimensions in PIXELS for
  95.        'easier calculation. Our scroll bars work better
  96.        'and quicker in pixels as opposed to TWIPS.
  97.        ' My screen is 15 TWIPS per pixel, so
  98.        'TX and TY will actually equal 15 throughout the
  99.        'entire program. Your screen may be different.
  100.        TX = Screen.TwipsPerPixelX
  101.        TY = Screen.TwipsPerPixelY
  102.    End Sub
  103.    Private Sub HScroll1_Change()
  104.        HScroll1_Scroll
  105.    End Sub
  106.    Private Sub HScroll1_Scroll()
  107.        Image1.Left = -HScroll1.Value
  108.    End Sub
  109.    Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  110.        On Error GoTo BadZoom
  111.        'Here (button 1 / left mouse button), is where we zoom in
  112.        'on the picture
  113.        If Button = 1 Then
  114.            'I choose 10 as enough times for zooming in
  115.            'and you can change this to a higher or
  116.            'lower number if you want
  117.            If ZoomDepth >= 10 Then Beep: Exit Sub
  118.            'Notice the "Image1.Width / 4" that is used here. This merely
  119.            'increases the image by 25%. You may use a different number
  120.            'than "4" to change your zoom ratio, but make sure you use
  121.            'the same number through your code.
  122.            Image1.Width = Image1.Width + (Image1.Width / 4)
  123.            Image1.Height = Image1.Height + (Image1.Height / 4)
  124.            If Image1.Width < Picture1.Width Then
  125.                Image1.Left = 0
  126.            Else
  127.                'Else, everything seems to be good
  128.                'so we will zoom in as calculated below.
  129.                'NOTICE that this is where we maintain
  130.                'our "point of view". What I mean is,
  131.                'our mouse cursor is pointed at a specific
  132.                'area of the image, so when we zoom in, we
  133.                'want to see that same area at a closer view.
  134.                'The "X" in the code, directly below, is part
  135.                'of the calculation of the horizontal mouse
  136.                'positio, which in turn sets the scroll bar
  137.                'properly. Thus the image has shifted the
  138.                'correct amount.
  139.                Set_Scrolls
  140.                
  141.                If HScroll1.Value + ((X / TX) / 4) > HScroll1.Max Then
  142.                    'This "IF" statement makes sure that our scroll value
  143.                    'does not exceed our Scroll MAX when zooming
  144.                    'in near the far right of the image. If it does
  145.                    'exceed, we will use the maximum scroll value
  146.                    HScroll1.Value = HScroll1.Max
  147.                Else
  148.                    HScroll1.Value = HScroll1.Value + ((X / TX) / 4)
  149.                End If
  150.            End If
  151.            'The "IF" statement below is the same
  152.            'as the one above, but it will now refer to the
  153.            'image height instead of the width
  154.            If Image1.Height < Picture1.Height Then
  155.            Else
  156.                Set_Scrolls
  157.                If VScroll1.Value + ((Y / TY) / 4) > VScroll1.Max Then
  158.                    VScroll1.Value = VScroll1.Max
  159.                Else
  160.                    VScroll1.Value = VScroll1.Value + ((Y / TY) / 4)
  161.                End If
  162.            End If
  163.            ZoomDepth = ZoomDepth + 1 'To keep track of how many times we soomed in
  164.            
  165.        ElseIf Button = 2 Then 'Else if button 2 is clicked (right mouse).
  166.            'We will zoom out. The code below is
  167.            'very similar to the code above with
  168.            'some minor changes.
  169.            If Image1.Width <= 10 Then Beep: Exit Sub
  170.            If Image1.Height <= 10 Then Beep: Exit Sub
  171.            Image1.Width = Image1.Width - (Image1.Width / 4)
  172.            Image1.Height = Image1.Height - (Image1.Height / 4)
  173.            If Image1.Width < Picture1.Width Then
  174.                'Do nothing
  175.            Else
  176.                If HScroll1.Value - ((X / TX) / 4) > HScroll1.Max Then
  177.                    HScroll1.Value = HScroll1.Max
  178.                ElseIf HScroll1.Value - ((X / TX) / 4) < 1 Then
  179.                    HScroll1.Value = 1
  180.                Else
  181.                    HScroll1.Value = HScroll1.Value - ((X / TX) / 4)
  182.                End If
  183.            End If
  184.            If Image1.Height < Picture1.Height Then
  185.                Image1.Top = 0
  186.            Else
  187.                If VScroll1.Value - ((Y / TY) / 4) > VScroll1.Max Then
  188.                    VScroll1.Value = VScroll1.Max
  189.                ElseIf VScroll1.Value - ((Y / TY) / 4) < 1 Then
  190.                    VScroll1.Value = 1
  191.                Else
  192.                    VScroll1.Value = VScroll1.Value - ((Y / TY) / 4)
  193.                End If
  194.            End If
  195.            ZoomDepth = ZoomDepth - 1 'Deduct each time we zoom out
  196.        End If
  197.        Set_Scrolls 'Jump to the "Set_Scrolls Sub" here
  198.        'which will determine when to enable
  199.        'or disable a scroll bar.
  200.        Exit Sub
  201. BadZoom:
  202.        Resume Next
  203.    End Sub
  204.    Private Sub mnu_2_Click()
  205.    End Sub
  206.    Private Sub mnu_exit_Click()
  207.        End
  208.    End Sub
  209.    Private Sub mnu_picture_Click()
  210.        'Incase the user clicks the CANCEL button
  211.        'we will exit nicely and do nothing, therefore
  212.        'we set the error that the CANCEL's button
  213.        'produces to TRUE
  214.        CommonDialog1.CancelError = True
  215.        'On any ERROR (mainly the CANCEL ERROR) go to the
  216.        'specified error handler immediately and exit sub.
  217.        On Error GoTo Err_Handler
  218.        CommonDialog1.Filter = "All Image Files (*.bmp;*.jpg;*.gif;*.ico;*.dib;*.wmf)|*.bmp;*.jpg;*.gif;*.ico;*.dib;*.wmf|Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|GIF (*.gif)|*.gif|Icon (*.ico)|*.ico|DIB (*.dib)|*.dib|Windows Meta File (*.wmf)|*.wmf"
  219.        CommonDialog1.ShowOpen
  220.        'Incase of a faulty image file, go to
  221.        'the BadPic error handler and exit nicely
  222.        On Error GoTo BadPic
  223.        'Sometimes an image file can not be loaded due to
  224.        'corruption. So we must test it first by placing
  225.        'it into an image box first (Image2 in our sample here).
  226.        'We don't want to chance losing the image in Image1.
  227.        Image2.Picture = LoadPicture(CommonDialog1.filename)
  228.        Image2.Picture = LoadPicture() 'Clear picture to save memory
  229.        'At this point, no error should have occured so we will
  230.        'continue. If an error occured, we would be sent to
  231.        'the BadPic error handler and exited the sub safely.
  232.        'Set the STRETCH propertry to FALSE to ensure proper
  233.        'and proportional size of original picture.
  234.        Image1.Stretch = False
  235.        'Load picture into view here...
  236.        Image1.Picture = LoadPicture(CommonDialog1.filename)
  237.        'Now set the STRETCH to TRUE to get the ZOOM effect.
  238.        Image1.Stretch = True
  239.        'Enable the Image control only because when you first
  240.        'run this program, you have no picture to work with,
  241.        'we don't want the user to ZOOM a blank image and look
  242.        'un-professional.
  243.        Image1.Enabled = True
  244.        Set_Scrolls
  245.        'We reset our scroll bars
  246.        'to 1 so the next picture loaded will
  247.        'not be viewed according to our last
  248.        'picture viewed.
  249.        HScroll1.Value = 1
  250.        VScroll1.Value = 1
  251.        ZoomDepth = 0
  252.        Exit Sub
  253. Err_Handler:
  254.        Exit Sub
  255. BadPic:
  256.        Image2.Picture = LoadPicture() 'Clear picture to save memory
  257.        MsgBox "Cannot use the image file " & CommonDialog1.filename, vbInformation, "Err number " & Err
  258.    End Sub
  259.    Private Sub VScroll1_Change()
  260.        VScroll1_Scroll
  261.    End Sub
  262.    Private Sub VScroll1_Scroll()
  263.        Image1.Top = -VScroll1.Value
  264.    End Sub
  265.    Public Sub Set_Scrolls()
  266.        If Image1.Width > Picture1.Width Then
  267.            'If the Image is wider than the picture box
  268.            'then we want to enable the horizontal
  269.            'scroll bar
  270.            HScroll1.Enabled = True
  271.            'We can set the maximum vale for this scroll bar
  272.            'based on the difference in the width of
  273.            'the picture box and the image.
  274.            'Note: since our scale mode is PIXEL and not TWIP
  275.            'we have a scroll bar that is efficient. TWIPs could
  276.            'very easily make the scroll bar MAX value
  277.            'into the thousands.
  278.            HScroll1.Max = Image1.Width - Picture1.Width
  279.            HScroll1.Min = 1
  280.            HScroll1.SmallChange = 1
  281.            HScroll1.LargeChange = 1
  282.        Else
  283.            'Else, our image is not wider than the picture box and
  284.            'we just dissable the scroll bar.
  285.            HScroll1.Enabled = False
  286.        End If
  287.        'The code below is the same as the above code, but
  288.        'deals with the height.
  289.        If Image1.Height > Picture1.Height Then
  290.            VScroll1.Enabled = True
  291.            VScroll1.Max = Image1.Height - Picture1.Height
  292.            VScroll1.Min = 1
  293.            VScroll1.SmallChange = 1
  294.            VScroll1.LargeChange = 1
  295.        Else
  296.            VScroll1.Enabled = False
  297.        End If
  298.    End Sub
  299.