home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / code / grafik / metapr / form1.frm (.txt) next >
Encoding:
Visual Basic Form  |  1993-09-26  |  27.8 KB  |  844 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    AutoRedraw      =   -1  'True
  4.    Caption         =   "Form1"
  5.    ClientHeight    =   4530
  6.    ClientLeft      =   255
  7.    ClientTop       =   1830
  8.    ClientWidth     =   7770
  9.    Height          =   5220
  10.    Icon            =   FORM1.FRX:0000
  11.    Left            =   195
  12.    LinkTopic       =   "Form1"
  13.    MaxButton       =   0   'False
  14.    MinButton       =   0   'False
  15.    ScaleHeight     =   4530
  16.    ScaleWidth      =   7770
  17.    Top             =   1200
  18.    Width           =   7890
  19.    Begin PictureBox Picture3 
  20.       Height          =   5500
  21.       Left            =   420
  22.       ScaleHeight     =   5475
  23.       ScaleWidth      =   5970
  24.       TabIndex        =   6
  25.       Top             =   -9000
  26.       Width           =   6000
  27.       Begin CommandButton Command2 
  28.          Caption         =   "&Ok"
  29.          Height          =   372
  30.          Left            =   4260
  31.          TabIndex        =   7
  32.          Top             =   3060
  33.          Width           =   972
  34.       End
  35.    End
  36.    Begin PictureBox Picture4 
  37.       BackColor       =   &H00FFFFFF&
  38.       Height          =   3132
  39.       Left            =   3852
  40.       ScaleHeight     =   3105
  41.       ScaleWidth      =   3465
  42.       TabIndex        =   8
  43.       Top             =   468
  44.       Visible         =   0   'False
  45.       Width           =   3492
  46.       Begin PictureBox Picture6 
  47.          Height          =   2292
  48.          Left            =   1260
  49.          ScaleHeight     =   151
  50.          ScaleMode       =   3  'Pixel
  51.          ScaleWidth      =   123
  52.          TabIndex        =   10
  53.          Top             =   360
  54.          Width           =   1872
  55.          Begin PictureBox Picture8 
  56.             BackColor       =   &H00000000&
  57.             Height          =   96
  58.             Left            =   900
  59.             MousePointer    =   8  'Size NW SE
  60.             ScaleHeight     =   4
  61.             ScaleMode       =   3  'Pixel
  62.             ScaleWidth      =   4
  63.             TabIndex        =   12
  64.             Top             =   1320
  65.             Visible         =   0   'False
  66.             Width           =   96
  67.          End
  68.          Begin PictureBox Picture7 
  69.             AutoRedraw      =   -1  'True
  70.             Height          =   552
  71.             Left            =   360
  72.             ScaleHeight     =   35
  73.             ScaleMode       =   3  'Pixel
  74.             ScaleWidth      =   55
  75.             TabIndex        =   11
  76.             TabStop         =   0   'False
  77.             Top             =   420
  78.             Width           =   852
  79.          End
  80.          Begin Shape Shape1 
  81.             BorderStyle     =   3  'Dot
  82.             Height          =   12
  83.             Left            =   1
  84.             Top             =   1
  85.             Visible         =   0   'False
  86.             Width           =   12
  87.          End
  88.       End
  89.       Begin PictureBox Picture5 
  90.          BackColor       =   &H00808080&
  91.          BorderStyle     =   0  'None
  92.          Height          =   1992
  93.          Left            =   480
  94.          ScaleHeight     =   1995
  95.          ScaleWidth      =   1635
  96.          TabIndex        =   9
  97.          Top             =   840
  98.          Width           =   1632
  99.       End
  100.    End
  101.    Begin PictureBox Picture2 
  102.       BackColor       =   &H00C0C0C0&
  103.       Height          =   2925
  104.       Left            =   0
  105.       ScaleHeight     =   2895
  106.       ScaleWidth      =   4605
  107.       TabIndex        =   1
  108.       Top             =   0
  109.       Width           =   4635
  110.       Begin CommandButton Command1 
  111.          Caption         =   "Ok"
  112.          Height          =   492
  113.          Left            =   180
  114.          TabIndex        =   5
  115.          Top             =   2160
  116.          Width           =   2052
  117.       End
  118.       Begin FileListBox File1 
  119.          Height          =   2430
  120.          Left            =   2340
  121.          Pattern         =   "*.WMF"
  122.          TabIndex        =   4
  123.          Top             =   120
  124.          Width           =   1935
  125.       End
  126.       Begin DirListBox Dir1 
  127.          Height          =   1536
  128.          Left            =   180
  129.          TabIndex        =   3
  130.          Top             =   540
  131.          Width           =   2052
  132.       End
  133.       Begin DriveListBox Drive1 
  134.          Height          =   300
  135.          Left            =   180
  136.          TabIndex        =   2
  137.          Top             =   120
  138.          Width           =   2052
  139.       End
  140.    End
  141.    Begin PictureBox Picture1 
  142.       AutoRedraw      =   -1  'True
  143.       AutoSize        =   -1  'True
  144.       DrawStyle       =   6  'Inside Solid
  145.       FillColor       =   &H000000FF&
  146.       FillStyle       =   0  'Solid
  147.       ForeColor       =   &H00000000&
  148.       Height          =   6132
  149.       Left            =   -60
  150.       ScaleHeight     =   407
  151.       ScaleMode       =   3  'Pixel
  152.       ScaleWidth      =   531
  153.       TabIndex        =   0
  154.       Top             =   120
  155.       Width           =   7992
  156.    End
  157.    Begin Menu mnuFile 
  158.       Caption         =   "&File"
  159.       Begin Menu mnuOpen 
  160.          Caption         =   "&Open"
  161.       End
  162.       Begin Menu mnuLine 
  163.          Caption         =   "-"
  164.          Index           =   0
  165.       End
  166.       Begin Menu mnuPrint 
  167.          Caption         =   "&Print"
  168.          Begin Menu mnuFull 
  169.             Caption         =   "&Full Page"
  170.          End
  171.          Begin Menu mnuSel 
  172.             Caption         =   "&Selected Area"
  173.          End
  174.       End
  175.       Begin Menu l1 
  176.          Caption         =   "-"
  177.       End
  178.       Begin Menu mnuExit 
  179.          Caption         =   "&Exit"
  180.       End
  181.    End
  182.    Begin Menu mnuDisplay 
  183.       Caption         =   "&Display"
  184.       Begin Menu mnuAnsi 
  185.          Caption         =   "&Ansiotropic"
  186.       End
  187.       Begin Menu mnuIso 
  188.          Caption         =   "&Isotropic"
  189.       End
  190.       Begin Menu l2 
  191.          Caption         =   "-"
  192.       End
  193.       Begin Menu mnuAbout 
  194.          Caption         =   "A&bout"
  195.       End
  196.    End
  197.    Begin Menu mnuPrintPar 
  198.       Caption         =   "&Print"
  199.       Visible         =   0   'False
  200.    End
  201.    Begin Menu mnuCancel 
  202.       Caption         =   "&Cancel"
  203.       Visible         =   0   'False
  204.    End
  205. Option Explicit
  206. 'Being restricted to using only one form for this project
  207. 'forced me into using this form as Windows uses its resources,
  208. 'turning objects on and off and resizing according to how the form
  209. 'was being used.
  210. 'It also shows that, using API calls, Visual Basic
  211. 'can compete on speed for most operations. Compare the time it takes
  212. 'to print a whole page using this program, to the time taken to print
  213. 'the timecard example provided with VB. Set your printer up to use
  214. 'Print Manager, this will help to differentiate between page processing
  215. 'and page printing.
  216. 'The main principle to get to grips with in using graphics in Windows,
  217. 'is how to convert a point in one object into the the same point but
  218. 'in another object. This program uses points and rectangles
  219. 'and has to use them as points on screen (for clipping) or points on another
  220. 'object (for sliding and moving that object around the object beneath it).
  221. 'No special VBX's are needed for this program Windows has it all built in.
  222. 'You just need to have a good guide around the Windows API calls.
  223. 'Visual Basic Progammer's Guide to the Windows API by D Appleman
  224. 'is highly recommended, and I have found it invaluable in this project.
  225. Dim OldMode%
  226. Dim Oldwidth%
  227. Dim OldHeight%
  228. Dim StartXPer
  229. Dim StartYPer
  230. Dim WidthPer
  231. Dim HeightPer
  232. Dim Startx
  233. Dim StartY
  234. Dim WinRect As RECT
  235. Dim DPoint As POINTAPI
  236. Dim UPoint As POINTAPI
  237. Dim TPoint As POINTAPI
  238. Sub ClearGrip ()
  239. 'hide grip (picture8)
  240.     Picture8.Visible = False
  241. End Sub
  242. Sub Command1_click ()
  243. Dim ret    'General return value
  244. 'If old Metafile exists then close it so that
  245. 'we do not eat memory. Failing to do this would
  246. 'mean that the last metafile would be floating
  247. 'around in Windows memory with no way  to get rid of it!!
  248.   If GhMF <> 0 Then
  249.     ret = DeleteMetaFile(GhMF)
  250.   End If
  251. 'Set the value of the global File name
  252.   If File1 = "" Then
  253.     Exit Sub
  254.   End If
  255.   file = Dir1 & "\" & File1
  256. ' Get a metafile Handle for the file
  257.   GhMF = GetDiskMetafile(file)
  258. ' If not a metafile then do nothing
  259.   If GhMF = 0 Then
  260.     Exit Sub
  261.   End If
  262. 'Turn the file box off and the picture box on
  263.   Picture2.Visible = False
  264.   RestoreWindow
  265.   Picture1.Visible = True
  266. 'Draw the Metafile
  267.   Picture1_paint
  268. 'remind through cation bar to try resizing
  269.   form1.Caption = form1.Caption & "  RESIZE ME"
  270. End Sub
  271. Sub Command2_Click ()
  272. 'turn off about box
  273.    Picture3.Visible = False
  274.    Form_resize
  275. End Sub
  276. Sub Dir1_Change ()
  277. 'display files in current directory
  278.   File1 = Dir1
  279. End Sub
  280. Sub Drive1_Change ()
  281. 'display  default directory for this drive
  282.   Dir1 = Drive1
  283. End Sub
  284. Sub File1_Click ()
  285. 'create full file name string
  286.   file = Dir1 & "\" & File1
  287. End Sub
  288. Sub File1_DblClick ()
  289. 'do the same as command1 (Ok)
  290.   Command1_click
  291. End Sub
  292. Sub Form_Activate ()
  293. mnuAbout_click
  294. End Sub
  295. Sub Form_Load ()
  296. 'set Caption on title bar
  297.   form1.Caption = "Metafile View & Print"
  298. 'Set the drawing mode for playing Metafiles to Ansiotropic
  299.   BoxScale = 8
  300.   mnuAnsi.Checked = True
  301. 'Make Picture1 fill the form
  302.   Picture1.Top = form1.ScaleTop
  303.   Picture1.Left = form1.ScaleLeft
  304.   Picture1.Width = form1.ScaleWidth
  305.   Picture1.Height = form1.ScaleHeight
  306.   Picture3.Top = 0
  307.   Picture3.Left = 0
  308.   mnuAbout_click
  309. 'display File Window
  310.   mnuOpen_click
  311. End Sub
  312. Sub Form_resize ()
  313. 'resize dependant on which window we are currently showing
  314.  If Picture3.Visible = True Then Exit Sub
  315. If Picture1.Visible = True Then
  316.   'resize Picture1
  317.     Picture1.Top = form1.ScaleTop
  318.     Picture1.Left = form1.ScaleLeft
  319.     Picture1.Width = form1.ScaleWidth
  320.     Picture1.Height = form1.ScaleHeight
  321.   'If there is a valid Metafile draw it
  322.     If GhMF = 0 Then
  323.      Else
  324.       Picture1_paint
  325.     End If
  326. End If
  327. If Picture2.Visible = True Then
  328.     'resize form
  329.     form1.Width = Picture2.Width
  330.     form1.Height = Picture2.Height + 750
  331. End If
  332. If Picture4.Visible = True Then
  333.     picture4_resize
  334. End If
  335. End Sub
  336. Sub Form_Unload (Cancel As Integer)
  337. Dim ret
  338. 'Close last Metafile before quiting
  339.   ret = DeleteMetaFile(GhMF)
  340. End Sub
  341. Function GetDiskMetafile (FileName$) As Integer
  342. ' This Function is Copyright 
  343.  P B Dyson 1993
  344. '                              48 Commercial Rd
  345. '                              Skelmanthorpe
  346. '                              Huddersfield
  347. '                              HD8 9DA
  348. '                              England
  349. ' Use of this Function is not restricted in any way
  350. ' Feel free to include or modify in any way
  351. ' If you pass on this Source please leave this Header in.
  352. 5 would be welcome if you use this code in one of your programs.
  353.   Dim ret As Long                ' general function return value
  354.   Dim Flen As Long               ' File length
  355.   Dim Address As Long            ' 32 bit memory address
  356.   Dim Head As MetaFileHeader     ' MetaFileHeader structure
  357.   Dim FileNum As Integer         ' VB file number
  358.   Dim hFile As Integer           ' DOS file handle
  359.   Dim hGlobmem As Integer        ' Global memory handle
  360.   Dim hMF As Integer             ' MetaFile handle
  361. ' no error handling for a bad filename or bad returns for clarity of method
  362. ' A valid filename will return a Metafile handle if it is a
  363. ' valid metafile,( either placeable or standard) or 0
  364. ' if it not a valid Metafile
  365. ' get next free file number
  366.     FileNum = FreeFile
  367. ' get file length
  368.     Flen = FileLen(FileName)
  369. ' open file
  370.     Open FileName For Binary As FileNum
  371. ' strip first 22 bytes to find out if it has a METAFILEHEADER
  372. ' structure at the start of the file
  373.      Get FileNum, 1, Head
  374. ' get dos file num for API calls that need to access the file
  375.     hFile = FileAttr(FileNum, 2)
  376. 'if no placeable header reset pointer to start of file
  377.     If Head.key <> &H9AC6CDD7 Then
  378.         ret = llseek(hFile, 0, 0)        ' could build in  an Exit Fun if failed
  379.     End If
  380. ' allocate some memory
  381.     hGlobmem = GlobalAlloc(gmem_moveable, Flen)      ' could build in  an Exit Fun if failed
  382. ' stop windows from moving it about while we use it
  383. ' and get the address of the memory
  384.     Address = GlobalLock(hGlobmem)               ' could build in  an Exit Fun if failed
  385. ' read rest of file into memory
  386.     ret = hread(hFile, Address, Flen)          ' could build in  an Exit Fun if failed
  387. ' finished with disk file now
  388.     Close FileNum
  389. ' unlock memory back to floating block
  390. ' keep Windows happy
  391.     ret = GlobalUnlock(hGlobmem)               ' fatal error if this fails
  392.                                                ' a block of memory is lost but unlikely
  393. ' create metafile handle from the memory block
  394.     hMF = SetMetafileBits(hGlobmem)
  395. ' If a MetaFile handle is not returned then free the global
  396. ' memory block
  397.     If hMF = 0 Then
  398.         ret = GlobalFree(hGlobmem)
  399.     End If
  400.     ' if we return a handle then
  401.     ' memory does not have to be released as it is now a normal
  402.     ' memory metafile and will be released when hMF is finished
  403.     ' with. This MUST be done with DeleteMetaFile() from the
  404.     ' calling program.
  405. ' return the handle to the Metafile or 0 if no MetaFile handle
  406. ' has been returned
  407.     GetDiskMetafile = hMF
  408. End Function
  409. Sub mnuAbout_click ()
  410. form1.Width = Picture3.Width
  411. form1.Height = Picture3.Height
  412. Picture3.Visible = True
  413.  Form_resize
  414. Picture3.Cls
  415. Picture3.Print "  Created for VB Primer to demonstrate how to speed"
  416. Picture3.Print "  up printing and positioning of graphics in"
  417. Picture3.Print "  Visual Basic using Metafiles. Logos, Graphics"
  418. Picture3.Print "  and converted plotfiles can be displayed on Forms"
  419. Picture3.Print "  and printed quickly at any size."
  420. Picture3.Print ""
  421. Picture3.Print "  If you find this program and source code useful"
  422. Picture3.Print "  please send 
  423. 5 to:"
  424. Picture3.Print ""
  425. Picture3.Print "      Peter Dyson,100273,656"
  426. Picture3.Print "      48 Commercial Rd,"
  427. Picture3.Print "      Skelmanthorpe,"
  428. Picture3.Print "      Huddersfield,"
  429. Picture3.Print "      HD8 9DA."
  430. Picture3.Print "      England."
  431. Picture3.Print ""
  432. Picture3.Print "  Or mail me some Metafile Handling code of "
  433. Picture3.Print "  similar speed and quality"
  434. End Sub
  435. Sub mnuAnsi_Click ()
  436. 'Set mapmode to Ansiotropic
  437.   BoxScale = 8
  438.   mnuAnsi.Checked = True
  439.   mnuIso.Checked = False
  440. 'Redraw
  441.   If Picture1.Visible = True Then
  442.      Picture1_paint
  443.    Else
  444.      Picture7_paint
  445.   End If
  446. End Sub
  447. Sub mnuCancel_click ()
  448. 'hide shape1
  449.    Shape1.Visible = False
  450. 'restore menus
  451.    mnuPrintPar.Visible = False
  452.    mnuCancel.Visible = False
  453.    mnuFile.Enabled = True
  454.    mnuDisplay.Enabled = True
  455.    form1.Caption = "Metafile View & Print"
  456. 'Restore picture1
  457.   Picture4.Visible = False
  458.   RestoreWindow
  459.   Picture1.Visible = True
  460.   Picture1_paint
  461. End Sub
  462. Sub mnuExit_Click ()
  463.   End      'Quit
  464. End Sub
  465. Sub mnuFull_Click ()
  466. 'Declarations
  467.   Dim hDC1 As Integer
  468.   Dim di%
  469.   Dim dl&
  470.   Dim savedDC As Integer
  471. 'turn on hourglass
  472.   Screen.MousePointer = 11
  473. 'prepare Printer for metafile
  474.    'Turn on printer object by printing space
  475.      Printer.ScaleMode = 1
  476.      Printer.Print ""
  477.    'Set Printer object scalemode to Pixel
  478.      Printer.ScaleMode = 3
  479.      hDC1 = Printer.hDC
  480.    'save dc attributes for Printer
  481.      savedDC = SaveDC(Printer.hDC)
  482.    ' more picture preparations which change the DC of the control
  483.      di% = SetMapMode(hDC1, BoxScale)
  484.      dl& = SetViewPortExt(hDC1, Printer.ScaleWidth, Printer.ScaleHeight)
  485.    ' and finally play the metafile into the Printer object
  486.      di% = PlayMetaFile(hDC1, GhMF)
  487.    'restore original DC attributes
  488.      di% = RestoreDC(hDC1, savedDC)
  489.    'Tell the Printer to print this page
  490.      Printer.EndDoc
  491. 'printing finished
  492. 'turn on normal cursor
  493.     Screen.MousePointer = 0
  494. End Sub
  495. Sub mnuIso_Click ()
  496. 'Set mapmode to Isotropic
  497.   BoxScale = 7
  498.   mnuIso.Checked = True
  499.   mnuAnsi.Checked = False
  500. 'Redraw
  501.   If Picture1.Visible = True Then
  502.      Picture1_paint
  503.    Else
  504.      Picture7_paint
  505.   End If
  506. End Sub
  507. Sub mnuOpen_click ()
  508. 'Make the File Box visible. (Not Windows standard
  509. 'but enables people with VB Primer to use this)
  510.   SaveWindow
  511.   'turn off picture1
  512.   Picture1.Visible = False
  513.   'resize form
  514.   form1.Width = Picture2.Width
  515.   form1.Height = Picture2.Height + 750
  516.   'display file box
  517.   Picture2.Visible = True
  518.   'show caption
  519.   form1.Caption = "Metafile View & Print"
  520. End Sub
  521. Sub mnuPrintPar_Click ()
  522. 'Declarations
  523.   Dim hDC1 As Integer
  524.   Dim di%
  525.   Dim dl&
  526.   Dim savedDC As Integer
  527. 'turn on hourglass
  528.     Screen.MousePointer = 11
  529.   'work out the start points in percentage terms of page x,y
  530.       StartXPer = Picture7.Left / Picture6.ScaleWidth
  531.       StartYPer = Picture7.Top / Picture6.ScaleHeight
  532.   'work out width and height
  533.       WidthPer = Picture7.Width / Picture6.ScaleWidth
  534.       HeightPer = Picture7.Height / Picture6.ScaleHeight
  535. 'prepare Printer for metafile
  536.    'Turn on printer object by printing space
  537.      Printer.ScaleMode = 1
  538.      Printer.Print ""
  539. 'Set Printer object scalemode to Pixel
  540.     Printer.ScaleMode = 3
  541.     hDC1 = Printer.hDC
  542. ' save dc attributes for Printer
  543.     savedDC = SaveDC(Printer.hDC)
  544. ' more picture preparations which change the DC of the control
  545.     di% = SetMapMode(hDC1, BoxScale)
  546.     dl& = SetViewPortExt(hDC1, Printer.ScaleWidth * WidthPer, Printer.ScaleHeight * HeightPer)
  547.     dl& = setviewportorg(hDC1, Printer.ScaleWidth * StartXPer, Printer.ScaleHeight * StartYPer)
  548. ' and finally play the metafile into the Printer object
  549.     di% = PlayMetaFile(hDC1, GhMF)
  550. 'restore original DC attributes
  551.     di% = RestoreDC(hDC1, savedDC)
  552. 'Tell the Printer to print this page
  553.    Printer.EndDoc
  554. 'Reset the form and pointer to what they were before
  555.    Screen.MousePointer = 0
  556.    mnuCancel_click
  557. End Sub
  558. Sub mnuSel_Click ()
  559.   SaveWindow
  560. 'clear everything from form
  561.   Picture1.Visible = False
  562.   Picture2.Visible = False
  563. 'change caption bar as prompt
  564.   form1.Caption = "Draw Box and Print"
  565.    picture4_resize
  566.   'turn page on
  567.   Picture4.Visible = True
  568.   'initialise boundary box
  569.   Shape1.Visible = True
  570.   Picture6_MouseDown 1, 0, 1, 1
  571.   Shape1.Visible = False
  572.   'do not display metafile picture or grip
  573.   Picture7.Visible = False
  574.   Picture8.Visible = False
  575. End Sub
  576. Sub Picture1_paint ()
  577. 'fill picture1 with the metafile
  578.   PlayMetafileFull Picture1, GhMF
  579. End Sub
  580. Sub picture4_resize ()
  581. 'resize form to look like portrait paper
  582.   form1.Width = form1.ScaleHeight * 8.5 / 12
  583. 'Enable Print Part and Cancel in menubar
  584.    mnuPrintPar.Visible = True
  585.    mnuCancel.Visible = True
  586.    mnuFile.Enabled = False
  587. 'Set sizes of picture boxes to current form size
  588.   'background
  589.   Picture4.Top = 0
  590.   Picture4.Left = 0
  591.   Picture4.Width = form1.ScaleWidth
  592.   Picture4.Height = form1.ScaleHeight
  593.   'shadow
  594.   Picture5.Top = Picture4.ScaleHeight * .05
  595.   Picture5.Left = Picture4.ScaleWidth * .05
  596.   Picture5.Width = Picture4.ScaleWidth * .9
  597.   Picture5.Height = Picture4.ScaleHeight * .9
  598.   'page
  599.   Picture6.Top = Picture5.Top - 60
  600.   Picture6.Left = Picture5.Left - 60
  601.   Picture6.Width = Picture5.Width
  602.   Picture6.Height = Picture5.Height
  603. End Sub
  604. Sub Picture6_MouseDown (Button As Integer, Shift As Integer, x As Single, y As Single)
  605. 'this is an example of how to confine the cursor to a specified
  606. 'control and how to create a rubberband box
  607.   If Picture7.Visible = True Then
  608.     Exit Sub
  609.   End If
  610.   'clear page
  611.      
  612.      Picture7.Visible = False
  613.      ClearGrip
  614.   'Set starting points
  615.      Shape1.Left = x
  616.      Shape1.Top = y
  617.      Shape1.Width = 1
  618.      Shape1.Height = 1
  619.      Shape1.Visible = True
  620.   'keep start points
  621.       
  622.       Startx = x
  623.       StartY = y
  624. ' Get boundarys of picture box in screen coords
  625. ' set the clipping rectangle for picture6 to hold the cursor.
  626. ' Just these two commands and the ClipCursorClear (type safe variant
  627. ' of ClipCursor) in mouse_up is all that is needed to create a bounding
  628. ' box from which the cursor cannot be moved out of.
  629.     GetWindowRect Picture6.hWnd, WinRect       'API
  630.     ClipCursor WinRect                         'API
  631. End Sub
  632. Sub Picture6_Mousemove (Button As Integer, Shift As Integer, x As Single, y As Single)
  633.   If Picture7.Visible = True Then
  634.     Exit Sub
  635.   End If
  636. 'only process if left mouse button is down
  637.   If Button = 1 Then
  638.      
  639.   'get width and height of box
  640.      Shape1.Width = (Abs(x - Startx)) + 1
  641.      Shape1.Height = (Abs(y - StartY)) + 1
  642.   ' if current x is to the left of start point change box orgin
  643.       If Startx > x Then
  644.         Shape1.Left = x
  645.        Else
  646.         Shape1.Left = Startx
  647.       End If
  648.    ' if current y is above the start point change box orgin
  649.       If StartY > y Then
  650.         Shape1.Top = y
  651.        Else
  652.         Shape1.Top = StartY
  653.       End If
  654.   End If
  655. End Sub
  656. Sub Picture6_MouseUp (Button As Integer, Shift As Integer, x As Single, y As Single)
  657.   If Picture7.Visible = True Then
  658.     Exit Sub
  659.   End If
  660. ' clear the cursor clipping rectangle to allow normal
  661. ' cursor actions to take place
  662.     ClipCursorClear 0              ' API
  663.   'resize picture to shape
  664.     Picture7.Visible = True
  665.     Picture7.Top = Shape1.Top
  666.     Picture7.Left = Shape1.Left
  667.     Picture7.Width = Shape1.Width
  668.     Picture7.Height = Shape1.Height
  669.   'prepare Picture7 for metafile
  670.      Shape1.Visible = False
  671.      PlayMetafileFull Picture7, GhMF
  672.      SetGrip
  673. End Sub
  674. Sub Picture7_MouseDown (Button As Integer, Shift As Integer, x As Single, y As Single)
  675. 'an example of how to ensure that a graghic object stays
  676. 'within the boundarys of another object. Also demonstrates
  677. 'how to convert a point selected from one coordinate system
  678. 'to the same point in a window underneath using a different coordinate
  679. 'system.
  680.  Dim page As RECT
  681.  Dim pic1 As RECT
  682.  Dim clip As RECT
  683.  Dim temp As POINTAPI
  684. MousePointer = 5
  685. 'turn off grip
  686.    ClearGrip
  687. 'convert points at POINTAPI struct
  688.    DPoint.x = x
  689.    DPoint.y = y
  690. 'find the pixel coords for down point for picture6
  691.    MapWindowPoints Picture7.hWnd, Picture6.hWnd, DPoint, 1
  692. 'keep original left and top of picture
  693.    TPoint.x = Picture7.Left
  694.    TPoint.y = Picture7.Top
  695. ' get screen area of page and picture (pixels)
  696.     GetWindowRect Picture6.hWnd, page  'screen coord of page
  697.     GetWindowRect Picture7.hWnd, pic1  'screen coord of picture
  698. 'get point of mousedown in screen coord
  699.    temp = DPoint                       'need dpoint in pic6 coords later
  700.    clienttoscreen Picture6.hWnd, temp  'now got mousedown in screen coords
  701. 'calculate clipping region
  702.    clip.Top = page.Top + (temp.y - pic1.Top)
  703.    clip.Left = page.Left + (temp.x - pic1.Left)
  704.    clip.Bottom = page.Bottom - (pic1.Bottom - temp.y)
  705.    clip.Right = page.Right - (pic1.Right - temp.x)
  706. 'set clipping action
  707.    ClipCursor clip
  708. End Sub
  709. Sub Picture7_MouseMove (Button As Integer, Shift As Integer, x As Single, y As Single)
  710. 'only process if leftmouse button is down
  711.   If Button = 1 Then
  712.     'put x,y in picture7 coord into POINAPI struct
  713.      UPoint.x = x
  714.      UPoint.y = y
  715.    'find the Picture6 coords for this down point
  716.      MapWindowPoints Picture7.hWnd, Picture6.hWnd, UPoint, 1
  717.    'calculate new picture position in picture 6 coords
  718.      Picture7.Top = TPoint.y + (UPoint.y - DPoint.y)
  719.      Picture7.Left = TPoint.x + (UPoint.x - DPoint.x)
  720.   End If
  721. End Sub
  722. Sub Picture7_MouseUp (Button As Integer, Shift As Integer, x As Single, y As Single)
  723. 'free up the cursor to move anywhere
  724.    ClipCursorClear 0
  725. 'put x,y in picture7 coord into POINAPI struct
  726.      UPoint.x = x
  727.      UPoint.y = y
  728. 'find the Picture6 coords for this  point
  729.      MapWindowPoints Picture7.hWnd, Picture6.hWnd, UPoint, 1
  730. 'calculate new picture position in picture 6 coords
  731.      Picture7.Top = TPoint.y + (UPoint.y - DPoint.y)
  732.      Picture7.Left = TPoint.x + (UPoint.x - DPoint.x)
  733. 'display the grip point
  734.    SetGrip
  735.    MousePointer = 0
  736. End Sub
  737. Sub Picture7_paint ()
  738. 'fill picture7 with the metafile
  739.    PlayMetafileFull Picture7, GhMF
  740. End Sub
  741. Sub Picture8_MouseDown (Button As Integer, Shift As Integer, x As Single, y As Single)
  742. 'a further example of how to use coordinate transformation
  743.  Dim page As RECT
  744.  Dim pic1 As RECT
  745.  Dim clip As RECT
  746. 'ensure picture6 scalemode is set to pixels
  747.    Picture6.ScaleMode = 3
  748.   'clear page
  749.      
  750.      Picture7.Visible = False
  751.      
  752.   'Set starting points
  753.      Shape1.Left = Picture7.Left
  754.      Shape1.Top = Picture7.Top
  755.      Shape1.Width = Picture8.Left - Picture7.Left + 3
  756.      Shape1.Height = Picture8.Top - Picture7.Top + 3
  757.      Shape1.Visible = True
  758.   'keep start points
  759.       
  760.       Startx = Picture7.Left
  761.       StartY = Picture7.Top
  762. ' Get boundarys of page box in screen coords
  763. ' Get boundarys of picture in screen coords
  764.     GetWindowRect Picture6.hWnd, page       'API
  765.     GetWindowRect Picture7.hWnd, pic1      'API
  766. ' set limits of cursor to picture top left
  767. '                         page bottom right
  768.     clip.Top = pic1.Top + 10          'not too small
  769.     clip.Left = pic1.Left + 10        '10 by 10 pixels min
  770.     clip.Bottom = page.Bottom
  771.     clip.Right = page.Right
  772.     ClipCursor clip                         'API
  773. End Sub
  774. Sub Picture8_MouseMove (Button As Integer, Shift As Integer, x As Single, y As Single)
  775. 'only process if left mouse button is down
  776.   If Button = 1 Then
  777. UPoint.x = x
  778. UPoint.y = y
  779. 'find the page coords for down point
  780.  MapWindowPoints Picture8.hWnd, Picture6.hWnd, UPoint, 1
  781.      Shape1.Width = (Abs(UPoint.x - Startx)) + 1
  782.      Shape1.Height = (Abs(UPoint.y - StartY)) + 1
  783. 'move the grip
  784.      Picture8.Top = Shape1.Height + StartY - 4
  785.      Picture8.Left = Shape1.Width + Startx - 4
  786.   End If
  787. End Sub
  788. Sub Picture8_MouseUp (Button As Integer, Shift As Integer, x As Single, y As Single)
  789. ' clear the cursor clipping rectangle to allow normal
  790. ' cursor actions to take place
  791.     ClipCursorClear 0              ' API
  792.   'resize picture to shape
  793.     Picture7.Top = Shape1.Top
  794.     Picture7.Left = Shape1.Left
  795.     Picture7.Width = Shape1.Width
  796.     Picture7.Height = Shape1.Height
  797. 'prepare Picture7 for metafile
  798.       
  799.       Shape1.Visible = False
  800.       Picture7.Visible = True
  801.       PlayMetafileFull Picture7, GhMF
  802.     SetGrip
  803. End Sub
  804. Sub PlayMetafileFull (Pict As Control, hMF As Integer)
  805.   Dim hDC1 As Integer
  806.   Dim di%
  807.   Dim dl&
  808.   Dim savedDC As Integer
  809.      
  810.   If Pict.Width > 3 Then
  811.      Pict.Cls
  812.      Pict.AutoRedraw = False
  813.      Pict.ScaleMode = 3
  814.      hDC1 = Pict.hDC
  815. ' save dc attributes for picture
  816.     savedDC = SaveDC(Pict.hDC)
  817. ' more picture preparations which change the DC of the control
  818.     di% = SetMapMode(hDC1, BoxScale)
  819.     dl& = SetViewPortExt(hDC1, Pict.ScaleWidth, Pict.ScaleHeight)
  820. ' and finally play the metafile into the picture
  821.     di% = PlayMetaFile(hDC1, hMF)
  822. 'restore original DC attributes
  823.     di% = RestoreDC(hDC1, savedDC)
  824.     Pict.AutoRedraw = True
  825. End If
  826. End Sub
  827.  Sub RestoreWindow ()
  828. 'Restore size
  829.   form1.ScaleMode = OldMode
  830.   form1.Width = Oldwidth
  831.   form1.Height = OldHeight
  832. End Sub
  833.  Sub SaveWindow ()
  834. 'Save current form properties
  835.   OldMode = form1.ScaleMode
  836.   Oldwidth = form1.Width
  837.   OldHeight = form1.Height
  838. End Sub
  839. Sub SetGrip ()
  840.   Picture8.Top = Picture7.Top + Picture7.Height - 3
  841.   Picture8.Left = Picture7.Left + Picture7.Width - 3
  842.   Picture8.Visible = True
  843. End Sub
  844.