home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH7 / SRC / ASPECT.FRM (.txt) next >
Encoding:
Visual Basic Form  |  1996-05-02  |  3.6 KB  |  117 lines

  1. VERSION 4.00
  2. Begin VB.Form AspectForm 
  3.    Caption         =   "Aspect"
  4.    ClientHeight    =   4230
  5.    ClientLeft      =   1980
  6.    ClientTop       =   1695
  7.    ClientWidth     =   4710
  8.    Height          =   4920
  9.    Left            =   1920
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4230
  12.    ScaleWidth      =   4710
  13.    Top             =   1065
  14.    Width           =   4830
  15.    Begin VB.TextBox AspectText 
  16.       Height          =   285
  17.       Left            =   1080
  18.       TabIndex        =   1
  19.       Text            =   "1"
  20.       Top             =   120
  21.       Width           =   735
  22.    End
  23.    Begin VB.Label Label1 
  24.       Caption         =   "Aspect Ratio"
  25.       Height          =   255
  26.       Left            =   120
  27.       TabIndex        =   0
  28.       Top             =   120
  29.       Width           =   975
  30.    End
  31.    Begin VB.Menu mnuFile 
  32.       Caption         =   "&File"
  33.       Begin VB.Menu mnuFileExit 
  34.          Caption         =   "E&xit"
  35.       End
  36.    End
  37. Attribute VB_Name = "AspectForm"
  38. Attribute VB_Creatable = False
  39. Attribute VB_Exposed = False
  40. Option Explicit
  41. Dim SelectInProgress As Boolean
  42. Dim StartX As Single
  43. Dim StartY As Single
  44. Dim LastX As Single
  45. Dim LastY As Single
  46. Dim OldMode As Integer
  47. Dim ViewAspect As Single
  48. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  49.     SelectInProgress = True
  50.     ' For demonstration purposes, get the desired
  51.     ' aspect ratio from a TextBox.
  52.     ViewAspect = CSng(AspectText.Text)
  53.     ' Save the current drawing mode.
  54.     OldMode = DrawMode
  55.     ' Use invert mode for the rubberband box.
  56.     DrawMode = vbInvert
  57.     StartX = X
  58.     StartY = Y
  59.     LastX = X
  60.     LastY = Y
  61.     Line (StartX, StartY)-(LastX, LastY), , B
  62. End Sub
  63. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  64. Dim wid As Single
  65. Dim hgt As Single
  66.     If Not SelectInProgress Then Exit Sub
  67.     ' Erase the old box.
  68.     Line (StartX, StartY)-(LastX, LastY), , B
  69.     wid = X - StartX
  70.     hgt = Y - StartY
  71.     AdjustAspect wid, hgt, ViewAspect
  72.     LastX = StartX + wid
  73.     LastY = StartY + hgt
  74.     ' Draw the new box.
  75.     Line (StartX, StartY)-(LastX, LastY), , B
  76. End Sub
  77. Sub AdjustAspect(ww_wid As Single, ww_hgt As Single, view_aspect As Single)
  78. Dim ww_aspect As Single
  79. Dim sign As Integer
  80.     ' Don't divide by zero.
  81.     If ww_wid = 0 Or ww_hgt = 0 Or view_aspect = 0 Then Exit Sub
  82.     ww_aspect = ww_hgt / ww_wid
  83.     If ww_aspect < 0 Then
  84.         sign = -1
  85.     Else
  86.         sign = 1
  87.     End If
  88.     ww_aspect = Abs(ww_aspect)
  89.     If ww_aspect > view_aspect Then
  90.         ' The world window is too tall and thin. Make it wider.
  91.         ww_wid = sign * ww_hgt / view_aspect
  92.     Else
  93.         ' The world window is too short and squat. Make it taller.
  94.         ww_hgt = sign * view_aspect * ww_wid
  95.     End If
  96. End Sub
  97. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  98. Dim wid As Single
  99. Dim hgt As Single
  100.     If Not SelectInProgress Then Exit Sub
  101.     SelectInProgress = False
  102.     ' Erase the old box.
  103.     Line (StartX, StartY)-(LastX, LastY), , B
  104.     ' Restore the original drawing mode.
  105.     DrawMode = OldMode
  106.     wid = X - StartX
  107.     hgt = Y - StartY
  108.     AdjustAspect hgt, wid, ViewAspect
  109.     LastX = StartX + wid
  110.     LastY = StartY + hgt
  111.     ' Do something with the region
  112.     ' (StartX, StartY) - (LastX, LastY).
  113. End Sub
  114. Private Sub mnuFileExit_Click()
  115.     Unload Me
  116. End Sub
  117.