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 / quickdrw / quikdraw.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-04-23  |  25.5 KB  |  757 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       =   1875
  9.    ClientWidth     =   7365
  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          =   4995
  21.    Left            =   1020
  22.    LinkMode        =   1  'Source
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   4245
  25.    ScaleWidth      =   7365
  26.    Top             =   1215
  27.    Width           =   7545
  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.       _Version        =   65536
  186.       _ExtentX        =   847
  187.       _ExtentY        =   847
  188.       _StockProps     =   0
  189.       Filter          =   "Metafiles (*.wmf)|*.wmf"
  190.       Flags           =   4100
  191.    End
  192.    Begin VB.Label Label5 
  193.       Appearance      =   0  'Flat
  194.       BackColor       =   &H80000005&
  195.       Caption         =   "Width"
  196.       ForeColor       =   &H80000008&
  197.       Height          =   255
  198.       Left            =   6600
  199.       TabIndex        =   17
  200.       Top             =   3840
  201.       Width           =   615
  202.    End
  203.    Begin VB.Label Label4 
  204.       Appearance      =   0  'Flat
  205.       BackColor       =   &H80000005&
  206.       Caption         =   "Style"
  207.       ForeColor       =   &H80000008&
  208.       Height          =   255
  209.       Left            =   6600
  210.       TabIndex        =   15
  211.       Top             =   3480
  212.       Width           =   615
  213.    End
  214.    Begin VB.Label Label3 
  215.       Appearance      =   0  'Flat
  216.       BackColor       =   &H80000005&
  217.       Caption         =   "Color"
  218.       ForeColor       =   &H80000008&
  219.       Height          =   255
  220.       Left            =   6600
  221.       TabIndex        =   14
  222.       Top             =   3120
  223.       Width           =   615
  224.    End
  225.    Begin VB.Label Label2 
  226.       Appearance      =   0  'Flat
  227.       BackColor       =   &H80000005&
  228.       Caption         =   "Brush"
  229.       ForeColor       =   &H80000008&
  230.       Height          =   255
  231.       Left            =   6000
  232.       TabIndex        =   13
  233.       Top             =   2760
  234.       Width           =   615
  235.    End
  236.    Begin VB.Label Label1 
  237.       Appearance      =   0  'Flat
  238.       BackColor       =   &H80000005&
  239.       Caption         =   "Pen"
  240.       ForeColor       =   &H80000008&
  241.       Height          =   255
  242.       Left            =   5400
  243.       TabIndex        =   12
  244.       Top             =   2760
  245.       Width           =   495
  246.    End
  247.    Begin VB.Menu MenuDraw 
  248.       Caption         =   "Draw"
  249.       Begin VB.Menu MenuDrawType 
  250.          Caption         =   "Line"
  251.          Checked         =   -1  'True
  252.          Index           =   0
  253.       End
  254.       Begin VB.Menu MenuDrawType 
  255.          Caption         =   "Ellipse"
  256.          Index           =   1
  257.       End
  258.       Begin VB.Menu MenuDrawType 
  259.          Caption         =   "FocusRect"
  260.          Index           =   2
  261.       End
  262.       Begin VB.Menu MenuDrawType 
  263.          Caption         =   "Chord"
  264.          Index           =   3
  265.       End
  266.       Begin VB.Menu MenuDrawType 
  267.          Caption         =   "Pie"
  268.          Index           =   4
  269.       End
  270.       Begin VB.Menu MenuDrawType 
  271.          Caption         =   "Arc"
  272.          Index           =   5
  273.       End
  274.       Begin VB.Menu MenuDrawType 
  275.          Caption         =   "Polygon"
  276.          Index           =   6
  277.       End
  278.       Begin VB.Menu MenuDrawType 
  279.          Caption         =   "Polyline"
  280.          Index           =   7
  281.       End
  282.       Begin VB.Menu MenuDrawType 
  283.          Caption         =   "Rectangle"
  284.          Index           =   8
  285.       End
  286.       Begin VB.Menu NoOne 
  287.          Caption         =   "-"
  288.       End
  289.       Begin VB.Menu MenuExit 
  290.          Caption         =   "&Exit"
  291.          Index           =   100
  292.       End
  293.    End
  294.    Begin VB.Menu mnu_Metafile 
  295.       Caption         =   "Metafile"
  296.       Begin VB.Menu mnu_MetafileSave 
  297.          Caption         =   "Save"
  298.       End
  299.       Begin VB.Menu mnu_MetafileLoad 
  300.          Caption         =   "Load"
  301.       End
  302.       Begin VB.Menu mnu_MetafileCopy 
  303.          Caption         =   "Copy to Clipboard"
  304.       End
  305.    End
  306. Attribute VB_Name = "QuikDraw"
  307. Attribute VB_Creatable = False
  308. Attribute VB_Exposed = False
  309. DefStr A-Z
  310. Option Explicit
  311. ' Delete the current metafile
  312. Private Sub CmdDeleteMF_Click()
  313.     If MetaFile Is Nothing Then
  314.     Else
  315.         MetaFile.DeleteMetafile
  316.         Set MetaFile = Nothing
  317.     End If
  318. End Sub
  319. '   Draw the current object on the picture
  320. '   Index = 0 is the Execute button which draws the current
  321. '   object into the large Picture1 control
  322. '   Index = 1 is the SmallView button which draws the
  323. '   current object into the small Picture2 control
  324. '   Index = 2 is the AddToMF button which adds the current
  325. '   object into the current metafile.
  326. Private Sub CmdExecute_Click(Index As Integer)
  327.     Dim tmpDC As dwDeviceContext
  328.     Dim sys As New dwSystem
  329.     Dim rc As New dwRECT
  330.     Dim oldsize As New dwPoint
  331.     Dim oldpoint As New dwPoint
  332.     #If Win32 Then
  333.         Dim oldpolymode&
  334.         Dim saved&
  335.     #Else
  336.         Dim oldpolymode%
  337.         Dim saved%
  338.     #End If
  339.     Select Case Index
  340.         Case 0  ' Execute button - draw into Picture1 after
  341.                 ' clearing the control.
  342.             Set tmpDC = New dwDeviceContext
  343.             tmpDC.hDC = picture1.hDC
  344.             picture1.Cls
  345.         Case 1  ' SmallView button - draw into Picture2
  346.             picture2.Cls
  347.             Set tmpDC = New dwDeviceContext
  348.             tmpDC.hDC = picture2.hDC
  349.             ' We're going to be changing the scaling, better
  350.             ' save the current state of the DC or the VB
  351.             ' drawing routines will no longer draw correctly.
  352.             saved = tmpDC.SaveDC
  353.             ' The entire area of Picture1 is scaled to fit
  354.             ' Picture2 exactly - this requires a change of
  355.             ' the mapping mode.
  356.             tmpDC.SetMapMode MM_ANISOTROPIC
  357.             
  358.             ' The logical window is the size of Picture1.
  359.             ' Mapping this to the area of Picture2 is done
  360.             ' by making all of Picture2 the viewport.
  361.             tmpDC.SetWindowExtEx picture1.ScaleWidth, picture1.ScaleHeight, oldsize
  362.             tmpDC.SetViewportExtEx picture2.ScaleWidth, picture2.ScaleHeight, oldsize
  363.         Case 2  ' AddToMeta button - Add the current object
  364.                 ' to the global metafile.
  365.             ' First create a new metafile device context.
  366.             Set tmpDC = sys.CreateMetafile(vbNullString)
  367.             If MetaFile Is Nothing Then
  368.             Else
  369.                 ' If a global metafile already exists, first
  370.                 ' play the existing metafile into the new one.
  371.                 tmpDC.PlayMetaFile MetaFile
  372.                 ' Then delete the existing metafile.
  373.                 MetaFile.DeleteMetafile
  374.                 Set MetaFile = Nothing
  375.                 ' The drawing commands that follow will add
  376.                 ' the current object to the new metafile
  377.                 ' device context.
  378.             End If
  379.         End Select
  380.     ' Select in the private pen and brush if we're using them
  381.     If Pen Is Nothing And Brush Is Nothing Then
  382.     Else
  383.         tmpDC.SelectObjectPen Pen
  384.         tmpDC.SelectObjectBrush Brush
  385.     End If
  386.     ' Also change the polygon filling mode to winding if necessary
  387.     If ChkPoly.value = 1 Then oldpolymode = tmpDC.SetPolyFillMode(WINDING)
  388.     ' The object drawn depends on global LastDrawIndex which
  389.     ' was set by the Draw menu commands.
  390.     ' The PointsUsed global indicates how many points have
  391.     ' been drawn in Picture1.
  392.     Select Case LastDrawIndex
  393.         Case 0  ' Draw a line
  394.             If PointsUsed = 2 Then
  395.                 ' Set the current position of the pen
  396.                 tmpDC.MoveTo PointCollection(1)
  397.                 ' and draw to the specified point.
  398.                 tmpDC.LineTo PointCollection(2)
  399.             End If
  400.         Case 1  ' Draw an ellipse
  401.             If PointsUsed% = 2 Then
  402.                 rc.SetRect PointCollection(1).x, PointCollection(1).y, PointCollection(2).x, PointCollection(2).y
  403.                 tmpDC.Ellipse rc
  404.             End If
  405.         Case 2 ' Draw a focus rectangle
  406.             If PointsUsed% = 2 Then
  407.                 rc.SetRect PointCollection(1).x, PointCollection(1).y, PointCollection(2).x, PointCollection(2).y
  408.                 tmpDC.DrawFocusRect rc
  409.             End If
  410.         Case 3  ' Draw a chord
  411.             If PointsUsed% = 4 Then
  412.                 tmpDC.Chord PointCollection(1), PointCollection(2), PointCollection(3), PointCollection(4)
  413.             End If
  414.         Case 4  ' Draw a pie
  415.             If PointsUsed% = 4 Then
  416.                 tmpDC.Pie PointCollection(1), PointCollection(2), PointCollection(3), PointCollection(4)
  417.             End If
  418.         Case 5  ' Draw an arc
  419.             If PointsUsed% = 4 Then
  420.                 tmpDC.Arc PointCollection(1), PointCollection(2), PointCollection(3), PointCollection(4)
  421.             End If
  422.         Case 6 ' Draw a polygon
  423.             If PointsUsed% > 1 Then
  424.                 tmpDC.Polygon PointCollection
  425.             End If
  426.         Case 7 ' Draw a polyline
  427.             If PointsUsed% > 1 Then
  428.                 tmpDC.Polyline PointCollection
  429.             End If
  430.         Case 8 ' Draw a rectangle
  431.             If PointsUsed% = 2 Then
  432.                 rc.SetRect PointCollection(1).x, PointCollection(1).y, PointCollection(2).x, PointCollection(2).y
  433.                 tmpDC.Rectangle rc
  434.             End If
  435.         End Select
  436.     ' Be sure to restore the original GDI objects!
  437.     tmpDC.SelectObjectPen Nothing
  438.     tmpDC.SelectObjectBrush Nothing
  439.     If ChkPoly.value = 1 Then tmpDC.SetPolyFillMode oldpolymode
  440.     Select Case Index
  441.         Case 0
  442.             ' Notify the mouse down routine that the last
  443.             ' command was an execute
  444.             ' This informs the system that the next mouse
  445.             ' click in Picture1 is the start of a new object.
  446.             LastWasExecute% = -1
  447.         Case 1  ' Restore the previous state of the Picture2 DC
  448.             tmpDC.RestoreDC saved
  449.         Case 2  ' Close the metafile device context and
  450.                 ' objtain a metafile handle.
  451.             Set MetaFile = tmpDC.CloseMetafile
  452.             'tmpDC.hDC = picture1.hDC
  453.         End Select
  454.         
  455.     Set tmpDC = Nothing
  456.     Set sys = Nothing
  457.     Set rc = Nothing
  458.     Set oldsize = Nothing
  459.     Set oldpoint = Nothing
  460. End Sub
  461. ' Show the current global metafile if one exists. It will
  462. '   be shown in both Picture1 and Picture2
  463. Private Sub CmdShowMF_Click()
  464.     #If Win32 Then
  465.         Dim saved&
  466.     #Else
  467.         Dim saved%
  468.     #End If
  469.     Dim tmpDC As dwDeviceContext
  470.     Dim oldsize As New dwPoint
  471.     If MetaFile Is Nothing Then
  472.         Exit Sub
  473.     End If
  474.     ' Because the original drawing was into Picture1,
  475.     ' playing the metafile into Picture1 is trivial.
  476.     picture1.Cls
  477.     Set tmpDC = New dwDeviceContext
  478.     tmpDC.hDC = picture1.hDC
  479.     tmpDC.PlayMetaFile MetaFile
  480.     ' Picture 2 is trickier. First we clear it and save the
  481.     ' current DC state.
  482.     picture2.Cls
  483.     tmpDC.hDC = picture2.hDC
  484.     saved = tmpDC.SaveDC
  485.     ' Now set the new coordinate system. See the CmdExecute()_Click
  486.     ' command for further explanation
  487.     tmpDC.SetMapMode MM_ANISOTROPIC
  488.     tmpDC.SetWindowExtEx picture1.ScaleWidth, picture1.ScaleHeight, oldsize
  489.     tmpDC.SetViewportExtEx picture2.ScaleWidth, picture2.ScaleHeight, oldsize
  490.     ' All of the drawing objects that were used on the original
  491.     ' objects were saved with the metafile, thus the metafile
  492.     ' will automatically draw each object in the correct color
  493.     ' and style.
  494.     tmpDC.PlayMetaFile MetaFile
  495.     ' And restore the original DC state
  496.     tmpDC.RestoreDC saved
  497.     Set tmpDC = Nothing
  498.     Set oldsize = Nothing
  499. End Sub
  500. ' We default to the line mode with no points defined.
  501. Private Sub Form_Load()
  502.     MaxPoints% = 2
  503.     PointsUsed% = 0
  504.     ' Force the selection of a valid pen and brush
  505.     ScrollObject_Change 0
  506. End Sub
  507. '   It is important to delete these GDI objects (if they
  508. '   were created) before closing the application so that
  509. '   the Windows resources may be properly freed.
  510. Private Sub Form_Unload(Cancel As Integer)
  511.     Dim i%
  512.     If MetaFile Is Nothing Then
  513.     Else
  514.         MetaFile.DeleteMetafile
  515.         Set MetaFile = Nothing
  516.     End If
  517.     Set Pen = Nothing
  518.     Set Pen = Nothing
  519.     Set PointCollection = Nothing
  520. End Sub
  521. '   This function handles the menu commands. Each one defines
  522. '   a different object to draw when the Execute command button
  523. '   is selected.
  524. Private Sub MenuDrawType_Click(Index As Integer)
  525.     Dim x%, i%
  526.     ' Clear out the current object.
  527.     PointsUsed% = 0
  528.     picture1.Cls
  529.     ' LastDrawIndex is a global that shows which GDI drawing
  530.     ' function is being tested.
  531.     LastDrawIndex% = Index
  532.     ' Uncheck all of the menu entries
  533.     For x% = 0 To 8
  534.         MenuDrawType(x%).Checked = 0
  535.     Next x%
  536.     ' And check this one only.
  537.     MenuDrawType(Index).Checked = -1
  538.     ' Clear all the points in the collection
  539.     For i% = 1 To PointCollection.count
  540.         PointCollection.Remove 1
  541.     Next i%
  542.     ' Each GDI drawing tool has a maximum number of points
  543.     ' that it needs in order to perform the drawing.
  544.     Select Case Index
  545.         Case 0, 1, 2, 8
  546.             MaxPoints% = 2
  547.         Case 3, 4, 5
  548.             MaxPoints% = 4
  549.         ' Polygons are limited to some reasonable number, but
  550.         ' you can change the max number of points here without
  551.         ' changing anything else.
  552.         Case 6, 7
  553.             MaxPoints% = 32
  554.     End Select
  555. End Sub
  556. Private Sub MenuExit_Click(Index As Integer)
  557. Unload QuikDraw
  558. End Sub
  559. Private Sub mnu_MetafileCopy_Click()
  560. #If Win32 Then
  561.     Dim di&
  562. #Else
  563.     Dim di%
  564. #End If
  565.     Dim hdcMeta As dwDeviceContext
  566.     Dim newmf As dwMetaFile
  567.     Dim hgmem As New dwGlobalMemory
  568.     Dim mfp As METAFILEPICT
  569.     Dim GlblAddr&
  570.     Dim oldsize As New dwPoint
  571.     Dim sys As New dwSystem
  572.     If MetaFile Is Nothing Then
  573.         MsgBox "Metafile must be defined before saving"
  574.         Exit Sub
  575.     End If
  576.     Set hdcMeta = sys.CreateMetafile(vbNullString)
  577.     hdcMeta.SetWindowExtEx picture1.ScaleWidth, picture1.ScaleHeight, oldsize
  578.     hdcMeta.PlayMetaFile MetaFile
  579.     Set newmf = hdcMeta.CloseMetafile
  580.     mfp.mm = MM_ANISOTROPIC
  581.     mfp.xExt = picture1.ScaleWidth
  582.     mfp.yExt = picture1.ScaleHeight
  583.     mfp.hMF = newmf.hMetaFile
  584.     Set hgmem = sys.GlobalAlloc(GMEM_MOVEABLE, Len(mfp))
  585.     GlblAddr = hgmem.GlobalLock()
  586.     agCopyData mfp, ByVal GlblAddr&, Len(mfp)
  587.     hgmem.GlobalUnlock
  588.     ' Place the metafile into the clipboard
  589.     di = OpenClipboard(picture1.hwnd)
  590.     di = EmptyClipboard()
  591.     di = SetClipboardData(CF_METAFILEPICT, hgmem.hGlobal)
  592.     di = CloseClipboard()
  593.     Set hgmem = Nothing
  594.     Set sys = Nothing
  595.     Set oldsize = Nothing
  596.     Set newmf = Nothing
  597.     Set hdcMeta = Nothing
  598. End Sub
  599. Private Sub mnu_MetafileLoad_Click()
  600.     Dim usefile$
  601.     #If Win32 Then
  602.         Dim saved&
  603.         Dim di&, dl&
  604.     #Else
  605.         Dim saved%
  606.         Dim di%, dl&
  607.     #End If
  608.     Dim DC As New dwDeviceContext
  609.     Dim oldsize As New dwPoint
  610.     Dim usemf As dwMetaFile
  611.     CMDialogMF.DialogTitle = "Load a metafile"
  612.     CMDialogMF.Action = 1
  613.     usefile$ = CMDialogMF.filename
  614.     If usefile$ <> "" Then
  615.         Set usemf = LoadTheMetafile(usefile$)
  616.         If usemf Is Nothing Then
  617.         Else
  618.             ' Now draw the metafile
  619.             picture1.Cls
  620.             Set DC = New dwDeviceContext
  621.             DC.hDC = picture1.hDC
  622.             saved = DC.SaveDC
  623.             ' Now set the new coordinate system. See the CmdExecute()_Click
  624.             ' command for further explanation
  625.             ' Most metafiles will set their own extents, but we need
  626.             ' to set the viewport to match the scalemode of the
  627.             ' entire screen to fill the window
  628.             DC.SetMapMode MM_ANISOTROPIC
  629.             DC.SetViewportExtEx picture1.ScaleWidth, picture1.ScaleHeight, oldsize
  630.             ' All of the drawing objects that were used on the original
  631.             ' objects were saved with the metafile, thus the metafile
  632.             ' will automatically draw each object in the correct color
  633.             ' and style.
  634.             DC.PlayMetaFile usemf
  635.             ' And restore the original DC state
  636.             DC.RestoreDC saved
  637.             usemf.DeleteMetafile
  638.             Set usemf = Nothing
  639.             Set DC = Nothing
  640.         End If
  641.     End If
  642.     Set oldsize = Nothing
  643. End Sub
  644. Private Sub mnu_MetafileSave_Click()
  645.     Dim di&
  646.     Dim usefile$
  647.     If MetaFile Is Nothing Then
  648.         MsgBox "Metafile must be defined before saving"
  649.         Exit Sub
  650.     End If
  651.     CMDialogMF.DialogTitle = "Save a metafile"
  652.     CMDialogMF.Action = 2
  653.     usefile$ = CMDialogMF.filename
  654.     If usefile$ <> "" Then
  655.         di = SaveTheMetafile(usefile$, MetaFile, CInt(picture1.ScaleWidth), CInt(picture1.ScaleHeight))
  656.     End If
  657. End Sub
  658. '   Mouse clicks in Picture1
  659. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  660.     Dim pt%, px%, py%, i%
  661.     Dim newPoint As New dwPoint
  662.         
  663.     ' If last command was an execute, clear the points to
  664.     ' start a new image
  665.     If LastWasExecute% Then
  666.         PointsUsed% = 0
  667.         LastWasExecute% = 0
  668.         For i% = 1 To PointCollection.count
  669.             PointCollection.Remove 1
  670.         Next i%
  671.     End If
  672.     ' If the maximum number of points has been exceeded
  673.     ' Shift all of the points down
  674.     If PointsUsed% >= MaxPoints% Then
  675.         PointCollection.Remove 1
  676.         PointsUsed% = PointsUsed% - 1
  677.     End If
  678.     ' Add the current point to the list
  679.     newPoint.x = CInt(x)
  680.     newPoint.y = CInt(y)
  681.     PointCollection.Add Item:=newPoint
  682.     PointsUsed% = PointsUsed% + 1
  683.     picture1.Cls
  684.     ' Draw small + indicators to show where the points are. set
  685.     For pt% = 1 To PointsUsed%
  686.         px% = PointCollection(pt%).x
  687.         py% = PointCollection(pt%).y
  688.         picture1.Line (px% - 2, py%)-(px% + 3, py%)
  689.         picture1.Line (px%, py% - 2)-(px%, py% + 3)
  690.     Next pt%
  691.     Set newPoint = Nothing
  692. End Sub
  693. '   This picture control shows a rectangle drawn in the
  694. '   current pen and brush.
  695. Private Sub Picture3_Paint()
  696.     Dim rc As New dwRECT
  697.     Dim hwnd As New dwWindow
  698.     Dim tmpDC As dwDeviceContext
  699.     #If Win32 Then
  700.         Dim di&
  701.     #Else
  702.         Dim di%
  703.     #End If
  704.     ' Get the window handle for Picture2
  705.     hwnd.hwnd = Picture3.hwnd
  706.     ' Get a rectangle with the client area size...
  707.     Set rc = hwnd.GetClientRect()
  708.     '.. and shrink it by 10 pixels on a side.
  709.     rc.InflateRect -10, -10
  710.     Set tmpDC = New dwDeviceContext
  711.     tmpDC.hDC = Picture3.hDC
  712.     ' Select in our private pen and brush
  713.     If Pen Is Nothing And Brush Is Nothing Then
  714.     Else
  715.         tmpDC.SelectObjectPen Pen
  716.         tmpDC.SelectObjectBrush Brush
  717.     End If
  718.     ' Draw the rectangle
  719.     tmpDC.Rectangle rc
  720.     ' Be sure to restore the original GDI objects.
  721.     tmpDC.SelectObjectPen Nothing
  722.     tmpDC.SelectObjectBrush Nothing
  723.     Set rc = Nothing
  724.     Set hwnd = Nothing
  725.     Set tmpDC = Nothing
  726. End Sub
  727. '   These scroll bars are used to select colors, styles and
  728. '   pen widths.  The Min and Max properties are selected
  729. '   such that the Scrollbar value parameter may be used
  730. '   directly in the GDI object creation function.
  731. Private Sub ScrollObject_Change(Index As Integer)
  732.     Dim di%
  733.     ' Wrap around when increasing
  734.     If ScrollObject(Index).value = ScrollObject(Index).Max Then
  735.         ScrollObject(Index).value = ScrollObject(Index).Min + 1
  736.         Exit Sub
  737.     End If
  738.     ' Wrap around when decrementing
  739.     If ScrollObject(Index).value = ScrollObject(Index).Min Then
  740.         ScrollObject(Index).value = ScrollObject(Index).Max - 1
  741.         Exit Sub
  742.     End If
  743.     ' Now create the new pen
  744.     Pen.CreatePen ScrollObject(2).value, ScrollObject(4).value, QBColor(ScrollObject(0).value)
  745.     ' Now create the new brush
  746.     ' Value 6 indicates that we should create a solid brush
  747.     ' 0-5 indicate styles of hatched brushes.
  748.     If ScrollObject(3).value = 6 Then
  749.         Brush.CreateSolidBrush QBColor(ScrollObject(1).value)
  750.     Else
  751.         Brush.CreateHatchBrush ScrollObject(3).value, QBColor(ScrollObject(1).value)
  752.     End If
  753.     ' Draw a sample rectangle using the current pen&Brush
  754.     ' This forces the Paint event to be triggered.
  755.     Picture3.Refresh
  756. End Sub
  757.