home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / classlib / desaware / samplev4 / rectapi / rectplay.frm (.txt) < prev   
Encoding:
Visual Basic Form  |  1996-01-02  |  9.5 KB  |  253 lines

  1. VERSION 4.00
  2. Begin VB.Form RectPlay 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H80000005&
  5.    Caption         =   "RectPlay"
  6.    ClientHeight    =   3990
  7.    ClientLeft      =   1095
  8.    ClientTop       =   1770
  9.    ClientWidth     =   4950
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   0
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   4680
  21.    Left            =   1035
  22.    LinkMode        =   1  'Source
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   3990
  25.    ScaleWidth      =   4950
  26.    Top             =   1140
  27.    Width           =   5070
  28.    Begin VB.PictureBox Picture1 
  29.       Appearance      =   0  'Flat
  30.       BackColor       =   &H80000005&
  31.       ForeColor       =   &H00000000&
  32.       Height          =   3255
  33.       Left            =   180
  34.       ScaleHeight     =   215
  35.       ScaleMode       =   3  'Pixel
  36.       ScaleWidth      =   311
  37.       TabIndex        =   0
  38.       Top             =   540
  39.       Width           =   4695
  40.    End
  41.    Begin VB.Label Label1 
  42.       Appearance      =   0  'Flat
  43.       BackColor       =   &H80000005&
  44.       Caption         =   "Label1"
  45.       ForeColor       =   &H80000008&
  46.       Height          =   255
  47.       Left            =   120
  48.       TabIndex        =   1
  49.       Top             =   120
  50.       Width           =   3735
  51.    End
  52.    Begin VB.Menu MenuViewBar 
  53.       Caption         =   "View"
  54.       Begin VB.Menu MenuView 
  55.          Caption         =   "Rect&1"
  56.          Checked         =   -1  'True
  57.          Index           =   0
  58.       End
  59.       Begin VB.Menu MenuView 
  60.          Caption         =   "Rect&2"
  61.          Checked         =   -1  'True
  62.          Index           =   1
  63.       End
  64.       Begin VB.Menu MenuView 
  65.          Caption         =   "&Union"
  66.          Index           =   2
  67.       End
  68.       Begin VB.Menu MenuView 
  69.          Caption         =   "&Intersect"
  70.          Index           =   3
  71.       End
  72.       Begin VB.Menu MenuView 
  73.          Caption         =   "&Offset"
  74.          Index           =   4
  75.       End
  76.       Begin VB.Menu MenuView 
  77.          Caption         =   "&Subtract"
  78.          Index           =   5
  79.       End
  80.    End
  81.    Begin VB.Menu ModeViewBar 
  82.       Caption         =   "Mode"
  83.       Begin VB.Menu MenuMode 
  84.          Caption         =   "Point"
  85.          Index           =   0
  86.       End
  87.       Begin VB.Menu MenuMode 
  88.          Caption         =   "SetRect1"
  89.          Index           =   1
  90.       End
  91.       Begin VB.Menu MenuMode 
  92.          Caption         =   "SetRect2"
  93.          Index           =   2
  94.       End
  95.    End
  96. Attribute VB_Name = "RectPlay"
  97. Attribute VB_Creatable = False
  98. Attribute VB_Exposed = False
  99. ' One of the first changes from the original code
  100. ' was to add Option Explicit here.  The original
  101. ' program goes back to VB 1.0!
  102. Option Explicit
  103. '   Displays information about the point in the EndPoint
  104. '   global variable (which is set during the Picture1
  105. '   MouseUp event.
  106. Private Sub DoPointDisplay()
  107.     Dim outstring$, crlf$
  108.     Dim tlong&
  109.     ' Unfortunately, the order in which elements are
  110.     ' placed on the stack differs for Win16 and Win32,
  111.     ' So we have to do a swap as follows
  112.     ' Define a newline string
  113.     crlf$ = Chr$(13) + Chr$(10)
  114.     ' Here we changed the PtInRect to accept the two
  115.     ' POINTAPI fields individually instead of converting it first
  116.     ' into a long as was done in the original 16 bit example.
  117.     If Rect1.PtInRect(EndPoint) Then
  118.         outstring$ = "is in Rect1" + crlf$
  119.     End If
  120.     If Rect2.PtInRect(EndPoint) Then
  121.        outstring$ = outstring$ + "is in Rect2" + crlf$
  122.     End If
  123.     If RectUnion.PtInRect(EndPoint) Then
  124.         outstring$ = outstring$ + "is in RectUnion" + crlf$
  125.     End If
  126.     If RectIntersect.PtInRect(EndPoint) Then
  127.         outstring$ = outstring$ + "is in RectIntersect" + crlf$
  128.     End If
  129.     If RectSubtract.PtInRect(EndPoint) Then
  130.         outstring$ = outstring$ + "is in RectSubtract" + crlf$
  131.     End If
  132.     'If RectOffset.PtInRect(EndPoint) Then
  133.     '    outstring$ = outstring$ + "is in RectOffset" + crlf$
  134.     'End If
  135.     If outstring$ = "" Then outstring$ = "is not in any rectangle"
  136.     MsgBox outstring$, 0, "Selected Point"
  137. End Sub
  138. Private Sub Form_Load()
  139.     SettingState% = 1   ' Set the initial value
  140. End Sub
  141. '   Set the Label1 control based on the SettingState%
  142. '   global variable to indicate to the user what the
  143. '   operating mode is.
  144. Private Sub Form_Paint()
  145.     Select Case SettingState%
  146.         Case 0
  147.             Label1.Caption = "Point Detect"
  148.         Case 1
  149.             Label1.Caption = "Set Rect 1"
  150.         Case 2
  151.             Label1.Caption = "Set Rect 2"
  152.     End Select
  153. End Sub
  154. '   Set the SettingState% variable according to the
  155. '   mode command selected.
  156. Private Sub MenuMode_Click(Index As Integer)
  157.     SettingState% = Index
  158.     RectPlay.Refresh
  159. End Sub
  160. '   Check or uncheck the item to view
  161. '   Then redraw the picture box
  162. Private Sub MenuView_Click(Index As Integer)
  163.     If MenuView(Index).Checked Then
  164.         MenuView(Index).Checked = 0
  165.     Else: MenuView(Index).Checked = -1
  166.     End If
  167.     Picture1.Refresh
  168. End Sub
  169. '   Record the current mouse location in StartPoint, and
  170. '   set the drawing mode to exclusive or
  171. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  172.     ' This conversion is safe, as we are in pixels
  173.     ' Note: The original RectPlay program used explicit type
  174.     ' conversions such as "StartPoint.X = CInt(X)"
  175.     ' Because the point fields can be integers or longs depending
  176.     ' on platform, it's better to just let VB do its automatic
  177.     ' conversion than to coerce it to the wrong one or use conditional compilation.
  178.     StartPoint.SetPoint X, Y
  179.     EndPoint.SetPoint X, Y
  180.     ' Drawing will be exclusive Or
  181.     Picture1.DrawMode = vbNotXorPen
  182.     HasCapture% = -1
  183. End Sub
  184. '   If mouse tracking is in effect, and a rectangle
  185. '   is being drawn, erase the prior rectangle and draw
  186. '   one based on the new location.
  187. Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  188.     If SettingState% <> 0 And HasCapture% Then
  189.         Picture1.Line (StartPoint.X, StartPoint.Y)-(EndPoint.X, EndPoint.Y), , B
  190.         Picture1.Line (StartPoint.X, StartPoint.Y)-(X, Y), , B
  191.     End If
  192.     EndPoint.SetPoint X, Y
  193. End Sub
  194. '   Erase the prior rectangle and save the information
  195. '   in the appropriate global rectangle.
  196. Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  197.     ' If we're not mouse tracking, exit the subroutine
  198.     If Not HasCapture% Then Exit Sub
  199.     If SettingState% <> 0 Then
  200.         Picture1.Line (StartPoint.X, StartPoint.Y)-(EndPoint.X, EndPoint.Y), , B
  201.     End If
  202.     EndPoint.SetPoint X, Y
  203.     Select Case SettingState%
  204.         Case 0
  205.             DoPointDisplay  ' Show point information
  206.         Case 1
  207.             Rect1.SetRect StartPoint.X, StartPoint.Y, EndPoint.X, EndPoint.Y
  208.         Case 2
  209.             Rect2.SetRect StartPoint.X, StartPoint.Y, EndPoint.X, EndPoint.Y
  210.     End Select
  211.     HasCapture% = 0
  212.     ' Restore the original drawing mode
  213.     Picture1.DrawMode = vbCopyPen
  214.     Picture1.Refresh
  215. End Sub
  216. '   Draw each of the rectangles that are requested,
  217. '   each in a different color.
  218. Private Sub Picture1_Paint()
  219.     ' Find the union and intersection rectangles
  220.     ' Using API calls
  221.     Debug.Print "Offset Point" & EndPoint.X, EndPoint.Y
  222.     Debug.Print "rect1 " & Rect1.left, Rect1.top, Rect1.right, Rect1.bottom
  223.     Debug.Print "rect2 " & Rect2.left, Rect2.top, Rect2.right, Rect2.bottom
  224.     Call RectIntersect.IntersectRect(Rect1, Rect2)
  225.     Call RectUnion.UnionRect(Rect1, Rect2)
  226.     'Debug.Print "Offset 2a " & RectOffset.left, RectOffset.top, RectOffset.right, RectOffset.bottom
  227.     'RectOffset.OffsetRect 20, 20
  228.     'Debug.Print "Offset 2afb " & RectOffset.left, RectOffset.top, RectOffset.right, RectOffset.bottom
  229.     Call RectSubtract.SubtractRect(Rect1, Rect2)
  230.     Debug.Print " Subtract " & RectSubtract.left, RectSubtract.top, RectSubtract.right, RectSubtract.bottom
  231.     If MenuView(0).Checked Then ' Rect1
  232.         Picture1.Line (Rect1.left, Rect1.top)-(Rect1.right, Rect1.bottom), QBColor(1), B
  233.     End If
  234.     If MenuView(1).Checked Then ' Rect2
  235.         Picture1.Line (Rect2.left, Rect2.top)-(Rect2.right, Rect2.bottom), QBColor(2), B
  236.     End If
  237.     If MenuView(2).Checked Then ' Union
  238.         Picture1.Line (RectUnion.left, RectUnion.top)-(RectUnion.right, RectUnion.bottom), QBColor(8), B
  239.     End If
  240.     If MenuView(3).Checked Then
  241.         Picture1.Line (RectIntersect.left, RectIntersect.top)-(RectIntersect.right, RectIntersect.bottom), QBColor(4), B
  242.     End If
  243.     If MenuView(4).Checked Then
  244.       '  Picture1.Line (Rect1.left, Rect1.top)-(Rect1.right, Rect1.bottom), QBColor(11), B
  245.       '  Picture1.Line (RectOffset.left, RectOffset.top)-(RectOffset.right, RectOffset.bottom), QBColor(14), B
  246.       '  Picture1.Line (Rect1.left, Rect1.top)-(RectOffset.bottom, RectOffset.right), QBColor(4), B
  247.     End If
  248.     If MenuView(5).Checked Then
  249.         Picture1.Line (RectSubtract.left, RectSubtract.top)-(RectSubtract.right, RectSubtract.bottom), QBColor(14), B
  250.     End If
  251.          RectOffset.SetRectEmpty
  252. End Sub
  253.