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 / samplev5 / quickdrw / quikdraw.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-17  |  25.5 KB  |  756 lines

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