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 / vbpg32 / samples4 / ch08 / quikdraw.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  25.1 KB  |  710 lines

  1. VERSION 4.00
  2. Begin VB.Form QuikDraw 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H80000005&
  5.    Caption         =   "QuikDraw"
  6.    ClientHeight    =   4245
  7.    ClientLeft      =   1110
  8.    ClientTop       =   1770
  9.    ClientWidth     =   7365
  10.    BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  11.       Name            =   "MS Sans Serif"
  12.       Size            =   8.25
  13.       Charset         =   0
  14.       Weight          =   700
  15.       Underline       =   0   'False
  16.       Italic          =   0   'False
  17.       Strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   4935
  21.    Left            =   1050
  22.    LinkMode        =   1  'Source
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   4245
  25.    ScaleWidth      =   7365
  26.    Top             =   1140
  27.    Width           =   7485
  28.    Begin VB.HScrollBar ScrollObject 
  29.       Height          =   255
  30.       Index           =   4
  31.       Left            =   5400
  32.       Max             =   9
  33.       TabIndex        =   16
  34.       Top             =   3840
  35.       Value           =   1
  36.       Width           =   495
  37.    End
  38.    Begin VB.CheckBox ChkPoly 
  39.       Appearance      =   0  'Flat
  40.       BackColor       =   &H80000005&
  41.       Caption         =   "PolyMode WINDING"
  42.       ForeColor       =   &H80000008&
  43.       Height          =   255
  44.       Left            =   1800
  45.       TabIndex        =   18
  46.       Top             =   3840
  47.       Width           =   2175
  48.    End
  49.    Begin VB.CommandButton CmdShowMF 
  50.       Appearance      =   0  'Flat
  51.       BackColor       =   &H80000005&
  52.       Caption         =   "ShowMF"
  53.       Height          =   495
  54.       Left            =   120
  55.       TabIndex        =   6
  56.       Top             =   3720
  57.       Width           =   1095
  58.    End
  59.    Begin VB.HScrollBar ScrollObject 
  60.       Height          =   255
  61.       Index           =   3
  62.       Left            =   6000
  63.       Max             =   7
  64.       Min             =   -1
  65.       TabIndex        =   11
  66.       Top             =   3480
  67.       Value           =   6
  68.       Width           =   495
  69.    End
  70.    Begin VB.HScrollBar ScrollObject 
  71.       Height          =   255
  72.       Index           =   2
  73.       Left            =   5400
  74.       Max             =   5
  75.       Min             =   -1
  76.       TabIndex        =   10
  77.       Top             =   3480
  78.       Width           =   495
  79.    End
  80.    Begin VB.HScrollBar ScrollObject 
  81.       Height          =   255
  82.       Index           =   1
  83.       Left            =   6000
  84.       Max             =   16
  85.       Min             =   -1
  86.       TabIndex        =   9
  87.       Top             =   3120
  88.       Value           =   15
  89.       Width           =   495
  90.    End
  91.    Begin VB.HScrollBar ScrollObject 
  92.       Height          =   255
  93.       Index           =   0
  94.       Left            =   5400
  95.       Max             =   16
  96.       Min             =   -1
  97.       TabIndex        =   8
  98.       Top             =   3120
  99.       Width           =   495
  100.    End
  101.    Begin VB.PictureBox Picture3 
  102.       Appearance      =   0  'Flat
  103.       BackColor       =   &H80000005&
  104.       ForeColor       =   &H80000008&
  105.       Height          =   975
  106.       Left            =   4080
  107.       ScaleHeight     =   945
  108.       ScaleWidth      =   1185
  109.       TabIndex        =   7
  110.       Top             =   3120
  111.       Width           =   1215
  112.    End
  113.    Begin VB.CommandButton CmdDeleteMF 
  114.       Appearance      =   0  'Flat
  115.       BackColor       =   &H80000005&
  116.       Caption         =   "DeleteMF"
  117.       Height          =   495
  118.       Left            =   1320
  119.       TabIndex        =   5
  120.       Top             =   3120
  121.       Width           =   975
  122.    End
  123.    Begin VB.CommandButton CmdExecute 
  124.       Appearance      =   0  'Flat
  125.       BackColor       =   &H80000005&
  126.       Caption         =   "AddToMF"
  127.       Height          =   495
  128.       Index           =   2
  129.       Left            =   120
  130.       TabIndex        =   4
  131.       Top             =   3120
  132.       Width           =   1095
  133.    End
  134.    Begin VB.PictureBox Picture2 
  135.       Appearance      =   0  'Flat
  136.       BackColor       =   &H80000005&
  137.       ForeColor       =   &H80000008&
  138.       Height          =   1095
  139.       Left            =   5760
  140.       ScaleHeight     =   71
  141.       ScaleMode       =   3  'Pixel
  142.       ScaleWidth      =   71
  143.       TabIndex        =   3
  144.       Top             =   1440
  145.       Width           =   1095
  146.    End
  147.    Begin VB.CommandButton CmdExecute 
  148.       Appearance      =   0  'Flat
  149.       BackColor       =   &H80000005&
  150.       Caption         =   "SmallView"
  151.       Height          =   495
  152.       Index           =   1
  153.       Left            =   5760
  154.       TabIndex        =   2
  155.       Top             =   840
  156.       Width           =   1095
  157.    End
  158.    Begin VB.CommandButton CmdExecute 
  159.       Appearance      =   0  'Flat
  160.       BackColor       =   &H80000005&
  161.       Caption         =   "Execute"
  162.       Height          =   495
  163.       Index           =   0
  164.       Left            =   5760
  165.       TabIndex        =   1
  166.       Top             =   240
  167.       Width           =   1095
  168.    End
  169.    Begin VB.PictureBox Picture1 
  170.       Appearance      =   0  'Flat
  171.       BackColor       =   &H80000005&
  172.       ForeColor       =   &H80000008&
  173.       Height          =   2895
  174.       Left            =   120
  175.       ScaleHeight     =   191
  176.       ScaleMode       =   3  'Pixel
  177.       ScaleWidth      =   319
  178.       TabIndex        =   0
  179.       Top             =   120
  180.       Width           =   4815
  181.    End
  182.    Begin MSComDlg.CommonDialog CMDialogMF 
  183.       Left            =   3360
  184.       Top             =   3120
  185.       _ExtentX        =   847
  186.       _ExtentY        =   847
  187.       _Version        =   327680
  188.       Filter          =   "Metafiles (*.wmf)|*.wmf"
  189.       Flags           =   4100
  190.    End
  191.    Begin VB.Label Label5 
  192.       Appearance      =   0  'Flat
  193.       BackColor       =   &H80000005&
  194.       Caption         =   "Width"
  195.       ForeColor       =   &H80000008&
  196.       Height          =   255
  197.       Left            =   6600
  198.       TabIndex        =   17
  199.       Top             =   3840
  200.       Width           =   615
  201.    End
  202.    Begin VB.Label Label4 
  203.       Appearance      =   0  'Flat
  204.       BackColor       =   &H80000005&
  205.       Caption         =   "Style"
  206.       ForeColor       =   &H80000008&
  207.       Height          =   255
  208.       Left            =   6600
  209.       TabIndex        =   15
  210.       Top             =   3480
  211.       Width           =   615
  212.    End
  213.    Begin VB.Label Label3 
  214.       Appearance      =   0  'Flat
  215.       BackColor       =   &H80000005&
  216.       Caption         =   "Color"
  217.       ForeColor       =   &H80000008&
  218.       Height          =   255
  219.       Left            =   6600
  220.       TabIndex        =   14
  221.       Top             =   3120
  222.       Width           =   615
  223.    End
  224.    Begin VB.Label Label2 
  225.       Appearance      =   0  'Flat
  226.       BackColor       =   &H80000005&
  227.       Caption         =   "Brush"
  228.       ForeColor       =   &H80000008&
  229.       Height          =   255
  230.       Left            =   6000
  231.       TabIndex        =   13
  232.       Top             =   2760
  233.       Width           =   615
  234.    End
  235.    Begin VB.Label Label1 
  236.       Appearance      =   0  'Flat
  237.       BackColor       =   &H80000005&
  238.       Caption         =   "Pen"
  239.       ForeColor       =   &H80000008&
  240.       Height          =   255
  241.       Left            =   5400
  242.       TabIndex        =   12
  243.       Top             =   2760
  244.       Width           =   495
  245.    End
  246.    Begin VB.Menu MenuDraw 
  247.       Caption         =   "Draw"
  248.       Begin VB.Menu MenuDrawType 
  249.          Caption         =   "Line"
  250.          Checked         =   -1  'True
  251.          Index           =   0
  252.       End
  253.       Begin VB.Menu MenuDrawType 
  254.          Caption         =   "Ellipse"
  255.          Index           =   1
  256.       End
  257.       Begin VB.Menu MenuDrawType 
  258.          Caption         =   "FocusRect"
  259.          Index           =   2
  260.       End
  261.       Begin VB.Menu MenuDrawType 
  262.          Caption         =   "Chord"
  263.          Index           =   3
  264.       End
  265.       Begin VB.Menu MenuDrawType 
  266.          Caption         =   "Pie"
  267.          Index           =   4
  268.       End
  269.       Begin VB.Menu MenuDrawType 
  270.          Caption         =   "Arc"
  271.          Index           =   5
  272.       End
  273.       Begin VB.Menu MenuDrawType 
  274.          Caption         =   "Polygon"
  275.          Index           =   6
  276.       End
  277.       Begin VB.Menu MenuDrawType 
  278.          Caption         =   "Polyline"
  279.          Index           =   7
  280.       End
  281.       Begin VB.Menu MenuDrawType 
  282.          Caption         =   "Rectangle"
  283.          Index           =   8
  284.       End
  285.    End
  286.    Begin VB.Menu mnu_Metafile 
  287.       Caption         =   "Metafile"
  288.       Begin VB.Menu mnu_MetafileSave 
  289.          Caption         =   "Save"
  290.       End
  291.       Begin VB.Menu mnu_MetafileLoad 
  292.          Caption         =   "Load"
  293.       End
  294.       Begin VB.Menu mnu_MetafileCopy 
  295.          Caption         =   "Copy to Clipboard"
  296.       End
  297.    End
  298. Attribute VB_Name = "QuikDraw"
  299. Attribute VB_Creatable = False
  300. Attribute VB_Exposed = False
  301. DefStr A-Z
  302. Option Explicit
  303. ' Copyright 
  304.  1997 by Desaware Inc. All Rights Reserved
  305. ' Delete the current metafile
  306. Private Sub CmdDeleteMF_Click()
  307.     Dim di& ' Change to long - will work in Win16 too.
  308.     If hndMetaFile Then
  309.         di = DeleteMetaFile(hndMetaFile)
  310.         hndMetaFile = 0
  311.     End If
  312. End Sub
  313. '   Draw the current object on the picture
  314. '   Index = 0 is the Execute button which draws the current
  315. '   object into the large Picture1 control
  316. '   Index = 1 is the SmallView button which draws the
  317. '   current object into the small Picture2 control
  318. '   Index = 2 is the AddToMF button which adds the current
  319. '   object into the current metafile.
  320. Private Sub CmdExecute_Click(Index As Integer)
  321.     #If Win32 Then
  322.         Dim dc&, saved&, di&, dl&
  323.         Dim oldpen&, oldbrush&, oldpolymode&
  324.     #Else
  325.         Dim dc%, saved%, di%, dl&
  326.         Dim oldpen%, oldbrush%, oldpolymode%
  327.     #End If
  328.     Dim rc As RECT
  329.     Dim oldsize As SIZE
  330.     Dim oldpoint As POINTAPI
  331.     Select Case Index
  332.         Case 0  ' Execute button - draw into Picture1 after
  333.                 ' clearing the control.
  334.             dc = picture1.hDC
  335.             picture1.Cls
  336.         Case 1  ' SmallView button - draw into Picture2
  337.             picture2.Cls
  338.             dc = picture2.hDC
  339.             ' We're going to be changing the scaling, better
  340.             ' save the current state of the DC or the VB
  341.             ' drawing routines will no longer draw correctly.
  342.             saved = SaveDC(dc)
  343.             ' The entire area of Picture1 is scaled to fit
  344.             ' Picture2 exactly - this requires a change of
  345.             ' the mapping mode.
  346.             di = SetMapMode(dc, MM_ANISOTROPIC)
  347.             
  348.             ' The logical window is the size of Picture1.
  349.             ' Mapping this to the area of Picture2 is done
  350.             ' by making all of Picture2 the viewport.
  351.             dl& = SetWindowExtEx(dc, picture1.ScaleWidth, picture1.ScaleHeight, oldsize)
  352.             dl& = SetViewportExtEx(dc, picture2.ScaleWidth, picture2.ScaleHeight, oldsize)
  353.         Case 2  ' AddToMeta button - Add the current object
  354.                 ' to the global metafile.
  355.             ' First create a new metafile device context.
  356.             dc = CreateMetaFile(vbNullString)
  357.             If hndMetaFile <> 0 Then
  358.                 ' If a global metafile already exists,
  359.                 ' first the existing metafile into the new one.
  360.                 di = PlayMetaFile(dc, hndMetaFile)
  361.                 ' Then delete the existing metafile.
  362.                 di = DeleteMetaFile(hndMetaFile)
  363.                 hndMetaFile = 0
  364.                 ' The drawing commands that follow will add
  365.                 ' the current object to the new metafile
  366.                 ' device context.
  367.                 End If
  368.         End Select
  369.     ' Select in the private pen and brush if we're using them
  370.     If hndPen <> 0 And hndBrush <> 0 Then
  371.         oldpen = SelectObject(dc, hndPen)
  372.         oldbrush = SelectObject(dc, hndBrush)
  373.         End If
  374.     ' Also change the polygon filling mode to winding if necessary
  375.     If ChkPoly.value = 1 Then oldpolymode = SetPolyFillMode(dc, WINDING)
  376.     ' The object drawn depends on global LastDrawIndex which
  377.     ' was set by the Draw menu commands.
  378.     ' The PointsUsed global indicates how many points have
  379.     ' been drawn in Picture1.
  380.     Select Case LastDrawIndex
  381.         Case 0  ' Draw a line
  382.             If PointsUsed = 2 Then
  383.                 ' Set the current position of the pen
  384.                 dl = MoveToEx(dc, PointArray(0).X, PointArray(0).Y, oldpoint)
  385.                 ' and draw to the specified point.
  386.                 di = LineTo(dc, PointArray(1).X, PointArray(1).Y)
  387.                 End If
  388.         Case 1  ' Draw an ellipse
  389.             If PointsUsed% = 2 Then
  390.                 di = Ellipse(dc, PointArray(0).X, PointArray(0).Y, PointArray(1).X, PointArray(1).Y)
  391.                 End If
  392.         Case 2 ' Draw a focus rectangle
  393.             If PointsUsed% = 2 Then
  394.                 SetRect rc, PointArray(0).X, PointArray(0).Y, PointArray(1).X, PointArray(1).Y
  395.                 DrawFocusRect dc, rc
  396.                 End If
  397.         Case 3  ' Draw a chord
  398.             If PointsUsed% = 4 Then
  399.                 di = Chord(dc, PointArray(0).X, PointArray(0).Y, PointArray(1).X, PointArray(1).Y, PointArray(2).X, PointArray(2).Y, PointArray(3).X, PointArray(3).Y)
  400.                 End If
  401.         Case 4  ' Draw a pie
  402.             If PointsUsed% = 4 Then
  403.                 di = Pie(dc, PointArray(0).X, PointArray(0).Y, PointArray(1).X, PointArray(1).Y, PointArray(2).X, PointArray(2).Y, PointArray(3).X, PointArray(3).Y)
  404.                 End If
  405.         Case 5  ' Draw an arc
  406.             If PointsUsed% = 4 Then
  407.                 di = Arc(dc, PointArray(0).X, PointArray(0).Y, PointArray(1).X, PointArray(1).Y, PointArray(2).X, PointArray(2).Y, PointArray(3).X, PointArray(3).Y)
  408.                 End If
  409.         Case 6 ' Draw a polygon
  410.             If PointsUsed% > 1 Then
  411.                 di = Polygon(dc, PointArray(0), PointsUsed%)
  412.                 End If
  413.         Case 7 ' Draw a polyline
  414.             If PointsUsed% > 1 Then
  415.                 di = Polyline(dc, PointArray(0), PointsUsed%)
  416.                 End If
  417.         Case 8 ' Draw a rectangle
  418.             If PointsUsed% = 2 Then
  419.                 di = Rectangle(dc, PointArray(0).X, PointArray(0).Y, PointArray(1).X, PointArray(1).Y)
  420.                 End If
  421.         End Select
  422.     ' Be sure to restore the original GDI objects!
  423.     If oldpen <> 0 Then di = SelectObject(dc, oldpen)
  424.     If oldbrush <> 0 Then di = SelectObject(dc, oldbrush)
  425.     If ChkPoly.value = 1 Then di = SetPolyFillMode(dc, oldpolymode)
  426.     Select Case Index
  427.         Case 0
  428.             ' Notify the mouse down routine that the last
  429.             ' command was an execute
  430.             ' This informs the system that the next mouse
  431.             ' click in Picture1 is the start of a new object.
  432.             LastWasExecute% = -1
  433.         Case 1  ' Restore the previous state of the Picture2 DC
  434.             di = RestoreDC(dc, saved)
  435.         Case 2  ' Close the metafile device context and
  436.                 ' objtain a metafile handle.
  437.             hndMetaFile = CloseMetaFile(dc)
  438.             dc = picture1.hDC
  439.         End Select
  440. End Sub
  441. ' Show the current global metafile if one exists. It will
  442. '   be shown in both Picture1 and Picture2
  443. ' Porting notes:
  444. '   Conditionally define the variabe types.
  445. '   Removed type characters % and & from variable usage to avoid
  446. '       conflicts between 16 and 32 bits environments
  447. '   Changed SetViewportEx and SetWindowExt to SetViewportExtEx and
  448. '       SetWindowExtEx for Win32 compatibility.
  449. Private Sub CmdShowMF_Click()
  450.     #If Win32 Then
  451.         Dim saved&, dc&, di&, dl&
  452.     #Else
  453.         Dim saved%, dc%, di%, dl&
  454.     #End If
  455.     Dim oldsize As SIZE
  456.     ' Because the original drawing was into Picture1,
  457.     ' playing the metafile into Picture1 is trivial.
  458.     picture1.Cls
  459.     di = PlayMetaFile(picture1.hDC, hndMetaFile)
  460.     ' Picture 2 is trickier. First we clear it and save the
  461.     ' current DC state.
  462.     picture2.Cls
  463.     dc = picture2.hDC
  464.     saved = SaveDC(dc)
  465.     ' Now set the new coordinate system. See the CmdExecute()_Click
  466.     ' command for further explanation
  467.     di = SetMapMode(dc, MM_ANISOTROPIC)
  468.     dl = SetWindowExtEx(dc, picture1.ScaleWidth, picture1.ScaleHeight, oldsize)
  469.     dl = SetViewportExtEx(dc, picture2.ScaleWidth, picture2.ScaleHeight, oldsize)
  470.     ' All of the drawing objects that were used on the original
  471.     ' objects were saved with the metafile, thus the metafile
  472.     ' will automatically draw each object in the correct color
  473.     ' and style.
  474.     di = PlayMetaFile(dc, hndMetaFile)
  475.     ' And restore the original DC state
  476.     di = RestoreDC(dc, saved)
  477. End Sub
  478. ' We default to the line mode with no points defined.
  479. Private Sub Form_Load()
  480.     MaxPoints% = 2
  481.     PointsUsed% = 0
  482.     ' Force the selection of a valid pen and brush
  483.     ScrollObject_Change 0
  484. End Sub
  485. '   It is important to delete these GDI objects (if they
  486. '   were created) before closing the application so that
  487. '   the Windows resources may be properly freed.
  488. Private Sub Form_Unload(Cancel As Integer)
  489.     Dim di&
  490.     If hndMetaFile <> 0 Then di = DeleteMetaFile(hndMetaFile)
  491.     If hndPen <> 0 Then di = DeleteObject(hndPen)
  492.     If hndBrush <> 0 Then di = DeleteObject(hndBrush)
  493. End Sub
  494. '   This function handles the menu commands. Each one defines
  495. '   a different object to draw when the Execute command button
  496. '   is selected.
  497. Private Sub MenuDrawType_Click(Index As Integer)
  498.     Dim X%
  499.     ' Clear out the current object.
  500.     PointsUsed% = 0
  501.     picture1.Cls
  502.     ' LastDrawIndex is a global that shows which GDI drawing
  503.     ' function is being tested.
  504.     LastDrawIndex% = Index
  505.     ' Uncheck all of the menu entries
  506.     For X% = 0 To 8
  507.         MenuDrawType(X%).Checked = 0
  508.         Next X%
  509.     ' And check this one only.
  510.     MenuDrawType(Index).Checked = -1
  511.     ' Each GDI drawing tool has a maximum number of points
  512.     ' that it needs in order to perform the drawing.
  513.     Select Case Index
  514.         Case 0, 1, 2, 8
  515.             MaxPoints% = 2
  516.         Case 3, 4, 5
  517.             MaxPoints% = 4
  518.         ' Polygons are limited to the size of the point data array
  519.         Case 6, 7
  520.             MaxPoints% = 32
  521.         End Select
  522. End Sub
  523. Private Sub mnu_MetafileCopy_Click()
  524.     #If Win32 Then
  525.         Dim hdcMeta&
  526.         Dim dl&, di&
  527.         Dim newmf&
  528.         Dim hgmem&
  529.     #Else
  530.         Dim hdcMeta%
  531.         Dim dl&, di%
  532.         Dim newmf%
  533.         Dim hgmem%
  534.     #End If
  535.     Dim mfp As METAFILEPICT
  536.     Dim GlblAddr&
  537.     Dim oldsize As SIZE
  538.     If hndMetaFile = 0 Then
  539.         MsgBox "Metafile must be defined before saving"
  540.         Exit Sub
  541.     End If
  542.     hdcMeta = CreateMetaFile(vbNullString)
  543.     dl& = SetWindowExtEx(hdcMeta, picture1.ScaleWidth, picture1.ScaleHeight, oldsize)
  544.     di = PlayMetaFile(hdcMeta, hndMetaFile)
  545.     newmf = CloseMetaFile(hdcMeta)
  546.     mfp.mm = MM_ANISOTROPIC
  547.     mfp.xExt = picture1.ScaleWidth
  548.     mfp.yExt = picture1.ScaleHeight
  549.     mfp.hMF = newmf
  550.     ' Take out hardcoded sizes - used to be 8 instead of len(mfp)
  551.     hgmem = GlobalAlloc(GMEM_MOVEABLE, Len(mfp))
  552.     GlblAddr = GlobalLock(hgmem)
  553.     agCopyData mfp, ByVal GlblAddr&, Len(mfp)
  554.     di = GlobalUnlock(hgmem)
  555.     ' Place the metafile into the clipboard
  556.     di = OpenClipboard(picture1.hWnd)
  557.     di = EmptyClipboard()
  558.     di = SetClipboardData(CF_METAFILEPICT, hgmem)
  559.     di = CloseClipboard()
  560. End Sub
  561. Private Sub mnu_MetafileLoad_Click()
  562.     Dim usefile$
  563.     #If Win32 Then
  564.         Dim saved&
  565.         Dim dc&
  566.         Dim usemf&
  567.         Dim di&, dl&
  568.     #Else
  569.         Dim saved%
  570.         Dim dc%
  571.         Dim usemf%
  572.         Dim di%, dl&
  573.     #End If
  574.     Dim oldsize As SIZE
  575.     CMDialogMF.DialogTitle = "Load a metafile"
  576.     CMDialogMF.Action = 1
  577.     usefile$ = CMDialogMF.FileName
  578.     If usefile$ <> "" Then
  579.         usemf = LoadTheMetafile(usefile$)
  580.         If usemf <> 0 Then
  581.             ' Now draw the metafile
  582.             picture1.Cls
  583.             dc = picture1.hDC
  584.             saved = SaveDC(dc)
  585.             ' Now set the new coordinate system. See the CmdExecute()_Click
  586.             ' command for further explanation
  587.             ' Most metafiles will set their own extents, but we need
  588.             ' to set the viewport to match the scalemode of the
  589.             ' entire screen to fill the window
  590.             di = SetMapMode(dc, MM_ANISOTROPIC)
  591.             dl = SetViewportExtEx(dc, picture1.ScaleWidth, picture1.ScaleHeight, oldsize)
  592.             ' All of the drawing objects that were used on the original
  593.             ' objects were saved with the metafile, thus the metafile
  594.             ' will automatically draw each object in the correct color
  595.             ' and style.
  596.             di = PlayMetaFile(dc, usemf)
  597.             ' And restore the original DC state
  598.             di = RestoreDC(dc, saved)
  599.             di = DeleteMetaFile(usemf)
  600.         End If
  601.     End If
  602. End Sub
  603. Private Sub mnu_MetafileSave_Click()
  604.     Dim di&
  605.     Dim usefile$
  606.     If hndMetaFile = 0 Then
  607.         MsgBox "Metafile must be defined before saving"
  608.         Exit Sub
  609.     End If
  610.     CMDialogMF.DialogTitle = "Save a metafile"
  611.     CMDialogMF.Action = 2
  612.     usefile$ = CMDialogMF.FileName
  613.     If usefile$ <> "" Then
  614.         di = SaveTheMetafile(usefile$, hndMetaFile, CInt(picture1.ScaleWidth), CInt(picture1.ScaleHeight))
  615.     End If
  616. End Sub
  617. '   Mouse clicks in Picture1
  618. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  619.     Dim pt%, px%, py%
  620.     ' If last command was an execute, clear the points to
  621.     ' start a new image
  622.     If LastWasExecute% Then
  623.         PointsUsed% = 0
  624.         LastWasExecute% = 0
  625.         End If
  626.     ' If the maximum number of points has been exceeded
  627.     ' Shift all of the points down
  628.     If PointsUsed% >= MaxPoints% Then
  629.         For pt% = 1 To MaxPoints%
  630.             PointArray(pt% - 1) = PointArray(pt%)
  631.             Next pt%
  632.         PointsUsed% = PointsUsed% - 1
  633.         End If
  634.     ' Add the current point to the list
  635.     PointArray(PointsUsed%).X = CInt(X)
  636.     PointArray(PointsUsed%).Y = CInt(Y)
  637.     PointsUsed% = PointsUsed% + 1
  638.     picture1.Cls
  639.     ' Draw small + indicators to show where the points are.
  640.     For pt% = 0 To PointsUsed% - 1
  641.         px% = PointArray(pt%).X
  642.         py% = PointArray(pt%).Y
  643.         picture1.Line (px% - 2, py%)-(px% + 3, py%)
  644.         picture1.Line (px%, py% - 2)-(px%, py% + 3)
  645.     Next pt%
  646. End Sub
  647. '   This picture control shows a rectangle drawn in the
  648. '   current pen and brush.
  649. Private Sub Picture3_Paint()
  650.     Dim rc As RECT
  651.     #If Win32 Then
  652.         Dim hWnd&
  653.         Dim oldpen&, oldbrush&
  654.         Dim di&
  655.     #Else
  656.         Dim hWnd%
  657.         Dim oldpen%, oldbrush%
  658.         Dim di%
  659.     #End If
  660.     ' Get the window handle for Picture2
  661.     hWnd = Picture3.hWnd
  662.     ' Get a rectangle with the client area size...
  663.     GetClientRect hWnd, rc
  664.     '.. and shrink it by 10 pixels on a side.
  665.     InflateRect rc, -10, -10
  666.     ' Select in our private pen and brush
  667.     If hndPen <> 0 And hndBrush <> 0 Then
  668.         oldpen = SelectObject(Picture3.hDC, hndPen)
  669.         oldbrush = SelectObject(Picture3.hDC, hndBrush)
  670.         End If
  671.     ' Draw the rectangle
  672.     di = Rectangle(Picture3.hDC, rc.Left, rc.Top, rc.Right, rc.Bottom)
  673.     ' Be sure to restore the original GDI objects!
  674.     If oldpen <> 0 Then di = SelectObject(Picture3.hDC, oldpen)
  675.     If oldbrush <> 0 Then di = SelectObject(Picture3.hDC, oldbrush)
  676. End Sub
  677. '   These scroll bars are used to select colors, styles and
  678. '   pen widths.  The Min and Max properties are selected
  679. '   such that the Scrollbar value parameter may be used
  680. '   directly in the GDI object creation function.
  681. Private Sub ScrollObject_Change(Index As Integer)
  682.     Dim di%
  683.     ' Wrap around when increasing
  684.     If ScrollObject(Index).value = ScrollObject(Index).Max Then
  685.         ScrollObject(Index).value = ScrollObject(Index).Min + 1
  686.         Exit Sub
  687.         End If
  688.     ' Wrap around when decrementing
  689.     If ScrollObject(Index).value = ScrollObject(Index).Min Then
  690.         ScrollObject(Index).value = ScrollObject(Index).Max - 1
  691.         Exit Sub
  692.         End If
  693.     ' Delete the current objects
  694.     If hndPen Then di = DeleteObject(hndPen)
  695.     If hndBrush Then di = DeleteObject(hndBrush)
  696.     ' Now create the new pen
  697.     hndPen = CreatePen(ScrollObject(2).value, ScrollObject(4).value, QBColor(ScrollObject(0).value))
  698.     ' Now create the new brush
  699.     ' Value 6 indicates that we should create a solid brush
  700.     ' 0-5 indicate styles of hatched brushes.
  701.     If ScrollObject(3).value = 6 Then
  702.         hndBrush = CreateSolidBrush(QBColor(ScrollObject(1).value))
  703.     Else
  704.         hndBrush = CreateHatchBrush(ScrollObject(3).value, QBColor(ScrollObject(1).value))
  705.     End If
  706.     ' Draw a sample rectangle using the current pen&Brush
  707.     ' This forces the Paint event to be triggered.
  708.     Picture3.Refresh
  709. End Sub
  710.