home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / vbpg32 / samples5 / ch09 / combine.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  12.0 KB  |  325 lines

  1. VERSION 5.00
  2. Begin VB.Form frmCombine 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H80000005&
  5.    Caption         =   "Combining Images"
  6.    ClientHeight    =   4200
  7.    ClientLeft      =   1095
  8.    ClientTop       =   1485
  9.    ClientWidth     =   5790
  10.    BeginProperty Font 
  11.       Name            =   "MS Sans Serif"
  12.       Size            =   8.25
  13.       Charset         =   0
  14.       Weight          =   700
  15.       Underline       =   0   'False
  16.       Italic          =   0   'False
  17.       Strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    LinkTopic       =   "Form3"
  21.    PaletteMode     =   1  'UseZOrder
  22.    ScaleHeight     =   4200
  23.    ScaleWidth      =   5790
  24.    Begin VB.PictureBox picSaved 
  25.       Appearance      =   0  'Flat
  26.       BackColor       =   &H80000005&
  27.       ForeColor       =   &H80000008&
  28.       Height          =   510
  29.       Left            =   420
  30.       ScaleHeight     =   480
  31.       ScaleWidth      =   480
  32.       TabIndex        =   13
  33.       Top             =   2760
  34.       Width           =   510
  35.    End
  36.    Begin VB.PictureBox picWork3 
  37.       Appearance      =   0  'Flat
  38.       BackColor       =   &H80000005&
  39.       ForeColor       =   &H80000008&
  40.       Height          =   510
  41.       Left            =   3600
  42.       ScaleHeight     =   480
  43.       ScaleWidth      =   480
  44.       TabIndex        =   11
  45.       Top             =   3420
  46.       Width           =   510
  47.    End
  48.    Begin VB.PictureBox picWork2 
  49.       Appearance      =   0  'Flat
  50.       BackColor       =   &H80000005&
  51.       ForeColor       =   &H80000008&
  52.       Height          =   510
  53.       Left            =   3600
  54.       ScaleHeight     =   480
  55.       ScaleWidth      =   480
  56.       TabIndex        =   9
  57.       Top             =   2820
  58.       Width           =   510
  59.    End
  60.    Begin VB.PictureBox picWork 
  61.       Appearance      =   0  'Flat
  62.       BackColor       =   &H80000005&
  63.       ForeColor       =   &H80000008&
  64.       Height          =   510
  65.       Left            =   3600
  66.       ScaleHeight     =   480
  67.       ScaleWidth      =   480
  68.       TabIndex        =   8
  69.       Top             =   2220
  70.       Width           =   510
  71.    End
  72.    Begin VB.PictureBox picCarMaskI 
  73.       Appearance      =   0  'Flat
  74.       AutoRedraw      =   -1  'True
  75.       BackColor       =   &H80000005&
  76.       ForeColor       =   &H80000008&
  77.       Height          =   510
  78.       Left            =   3600
  79.       ScaleHeight     =   480
  80.       ScaleWidth      =   480
  81.       TabIndex        =   3
  82.       Top             =   1560
  83.       Width           =   510
  84.    End
  85.    Begin VB.PictureBox picCarMask 
  86.       Appearance      =   0  'Flat
  87.       AutoRedraw      =   -1  'True
  88.       AutoSize        =   -1  'True
  89.       BackColor       =   &H80000005&
  90.       ForeColor       =   &H80000008&
  91.       Height          =   510
  92.       Left            =   3600
  93.       Picture         =   "COMBINE.frx":0000
  94.       ScaleHeight     =   480
  95.       ScaleWidth      =   480
  96.       TabIndex        =   2
  97.       Top             =   960
  98.       Width           =   510
  99.    End
  100.    Begin VB.PictureBox picCar 
  101.       Appearance      =   0  'Flat
  102.       AutoSize        =   -1  'True
  103.       BackColor       =   &H80000005&
  104.       ForeColor       =   &H80000008&
  105.       Height          =   510
  106.       Left            =   3600
  107.       Picture         =   "COMBINE.frx":0282
  108.       ScaleHeight     =   480
  109.       ScaleWidth      =   480
  110.       TabIndex        =   1
  111.       Top             =   360
  112.       Width           =   510
  113.    End
  114.    Begin VB.PictureBox picLeafs 
  115.       Appearance      =   0  'Flat
  116.       AutoSize        =   -1  'True
  117.       BackColor       =   &H80000005&
  118.       ForeColor       =   &H80000008&
  119.       Height          =   2280
  120.       Left            =   420
  121.       MousePointer    =   2  'Cross
  122.       Picture         =   "COMBINE.frx":0504
  123.       ScaleHeight     =   150
  124.       ScaleMode       =   3  'Pixel
  125.       ScaleWidth      =   200
  126.       TabIndex        =   0
  127.       Top             =   360
  128.       Width           =   3030
  129.    End
  130.    Begin VB.Label Label7 
  131.       Appearance      =   0  'Flat
  132.       BackColor       =   &H80000005&
  133.       Caption         =   "Hold the original image segment to restore later."
  134.       ForeColor       =   &H80000008&
  135.       Height          =   675
  136.       Left            =   1080
  137.       TabIndex        =   14
  138.       Top             =   2760
  139.       Width           =   1935
  140.    End
  141.    Begin VB.Label Label6 
  142.       Appearance      =   0  'Flat
  143.       BackColor       =   &H80000005&
  144.       Caption         =   "Work area - step 3"
  145.       ForeColor       =   &H80000008&
  146.       Height          =   435
  147.       Left            =   4200
  148.       TabIndex        =   12
  149.       Top             =   3420
  150.       Width           =   1275
  151.    End
  152.    Begin VB.Label Label5 
  153.       Appearance      =   0  'Flat
  154.       BackColor       =   &H80000005&
  155.       Caption         =   "Work area - step 2"
  156.       ForeColor       =   &H80000008&
  157.       Height          =   435
  158.       Left            =   4200
  159.       TabIndex        =   10
  160.       Top             =   2820
  161.       Width           =   1275
  162.    End
  163.    Begin VB.Label Label4 
  164.       Appearance      =   0  'Flat
  165.       BackColor       =   &H80000005&
  166.       Caption         =   "Work area - step 1"
  167.       ForeColor       =   &H80000008&
  168.       Height          =   375
  169.       Left            =   4200
  170.       TabIndex        =   7
  171.       Top             =   2280
  172.       Width           =   1395
  173.    End
  174.    Begin VB.Label Label3 
  175.       Appearance      =   0  'Flat
  176.       BackColor       =   &H80000005&
  177.       Caption         =   "Inverted mask - created at runtime."
  178.       ForeColor       =   &H80000008&
  179.       Height          =   615
  180.       Left            =   4200
  181.       TabIndex        =   6
  182.       Top             =   1560
  183.       Width           =   1395
  184.    End
  185.    Begin VB.Label Label2 
  186.       Appearance      =   0  'Flat
  187.       BackColor       =   &H80000005&
  188.       Caption         =   "Mask to use."
  189.       ForeColor       =   &H80000008&
  190.       Height          =   495
  191.       Left            =   4200
  192.       TabIndex        =   5
  193.       Top             =   960
  194.       Width           =   1395
  195.    End
  196.    Begin VB.Label Label1 
  197.       Appearance      =   0  'Flat
  198.       BackColor       =   &H80000005&
  199.       Caption         =   "Picture to combine."
  200.       ForeColor       =   &H80000008&
  201.       Height          =   495
  202.       Left            =   4200
  203.       TabIndex        =   4
  204.       Top             =   360
  205.       Width           =   1395
  206.    End
  207. Attribute VB_Name = "frmCombine"
  208. Attribute VB_GlobalNameSpace = False
  209. Attribute VB_Creatable = False
  210. Attribute VB_PredeclaredId = True
  211. Attribute VB_Exposed = False
  212. ' ------------------------------------------------------------------------
  213. '     COMBINE.FRM
  214. '                       Copyright (C) 1995-1997 Desaware
  215. '  You have a royalty-free right to use, modify, reproduce and distribute
  216. '  this file (and/or any modified version) in any way you find useful,
  217. '  provided that you agree that Desaware has no
  218. '  warranty, obligation or liability for its contents.
  219. ' ------------------------------------------------------------------------
  220. Option Explicit
  221. ' Copyright 
  222.  1997 by Desaware Inc. All Rights Reserved
  223. ' We use these variables to keep track of whether the
  224. ' car is currently merged, and if so, where.
  225. Dim CarIsMerged%
  226. Dim CarMergeLocX%
  227. Dim CarMergeLocY%
  228. Private Type RECT
  229.     left As Long
  230.     top As Long
  231.     right As Long
  232.     bottom As Long
  233. End Type
  234. Private Type POINTAPI
  235.     x As Long
  236.     y As Long
  237. End Type
  238. Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
  239. Private Const SRCPAINT = &HEE0086    ' (DWORD) dest = source OR dest
  240. Private Const SRCAND = &H8800C6  ' (DWORD) dest = source AND dest
  241. Private Const SRCINVERT = &H660046   ' (DWORD) dest = source XOR dest
  242. Private Const SRCERASE = &H440328    ' (DWORD) dest = source AND (NOT dest )
  243. Private Const NOTSRCCOPY = &H330008  ' (DWORD) dest = (NOT source)
  244. Private Const NOTSRCERASE = &H1100A6 ' (DWORD) dest = (NOT src) AND (NOT dest)
  245. Private Const MERGECOPY = &HC000CA   ' (DWORD) dest = (source AND pattern)
  246. Private Const MERGEPAINT = &HBB0226  ' (DWORD) dest = (NOT source) OR dest
  247. Private Const PATCOPY = &HF00021 ' (DWORD) dest = pattern
  248. Private Const PATPAINT = &HFB0A09    ' (DWORD) dest = DPSnoo
  249. Private Const PATINVERT = &H5A0049   ' (DWORD) dest = pattern XOR dest
  250. Private Const DSTINVERT = &H550009   ' (DWORD) dest = (NOT dest)
  251. Private Const BLACKNESS = &H42&  ' (DWORD) dest = BLACK
  252. Private Const WHITENESS = &HFF0062   ' (DWORD) dest = WHITE
  253. Private Declare Function BitBlt& Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long)
  254. ' This routine combines the car into the leaf spread
  255. ' x and y are the start position for the merge in the leaf
  256. ' picture
  257. ' We'll use x = -1000 to indicate that the existing car
  258. ' location should be cleared.
  259. Private Sub CombineImage(x%, y%)
  260.     Dim di%
  261.     If CarIsMerged% Then
  262.         ' Car is already merged - we had better remove
  263.         ' it before merging it into the new location
  264.         ' This is accomplished by simply restoring
  265.         ' the saved area.
  266.         di% = BitBlt(picLeafs.hDC, CarMergeLocX%, CarMergeLocY%, 32, 32, picSaved.hDC, 0, 0, SRCCOPY)
  267.         CarIsMerged% = False
  268.     End If
  269.     If x% = -1000 Then Exit Sub
  270.     ' Lets start by bringing in a portion of the leaf
  271.     ' spread into the work picture.
  272.     di% = BitBlt(picWork.hDC, 0, 0, 32, 32, picLeafs.hDC, x%, y%, SRCCOPY)
  273.     ' We'll also hold the image in the saved area
  274.     di% = BitBlt(picSaved.hDC, 0, 0, 32, 32, picLeafs.hDC, x%, y%, SRCCOPY)
  275.     ' In a real application, we would perform all of the
  276.     ' following operations on the same work picture.
  277.     ' For illustrative purposes, we're using multiple
  278.     ' pictures.
  279.     ' Now lets Force to white the car area
  280.     ' First bring over the current work area.
  281.     di% = BitBlt(picWork2.hDC, 0, 0, 32, 32, picWork.hDC, 0, 0, SRCCOPY)
  282.     ' Now OR in the inverted mask
  283.     di% = BitBlt(picWork2.hDC, 0, 0, 32, 32, picCarMaskI.hDC, 0, 0, SRCPAINT)
  284.     ' Now lets merge in the car
  285.     ' First, bring over the current work area.
  286.     di% = BitBlt(picWork3.hDC, 0, 0, 32, 32, picWork2.hDC, 0, 0, SRCCOPY)
  287.     ' Now AND in the car picture
  288.     di% = BitBlt(picWork3.hDC, 0, 0, 32, 32, picCar.hDC, 0, 0, SRCAND)
  289.     ' And finally,  Copy the bitmap back to the original spread
  290.     di% = BitBlt(picLeafs.hDC, x%, y%, 32, 32, picWork3.hDC, 0, 0, SRCCOPY)
  291.     ' Save the current status
  292.     CarIsMerged% = True
  293.     CarMergeLocX% = x%
  294.     CarMergeLocY% = y%
  295. End Sub
  296. Private Sub Form_Load()
  297.     Dim di%
  298.     ' Note that in order for this BitBlt to work during
  299.     ' form load, both of these picture controls must have
  300.     ' autoredraw set to True - this assures that the
  301.     ' device context refers to a real bitmap at all times.
  302.     di% = BitBlt(picCarMaskI.hDC, 0, 0, 32, 32, picCarMask.hDC, 0, 0, SRCINVERT)
  303. End Sub
  304. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  305.     CombineImage -1000, -1000
  306. End Sub
  307. Private Sub picLeafs_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  308.     Dim usex%, usey%
  309.     ' It's safe to use integers here, as we've set the
  310.     ' scalemode to pixels.
  311.     usex% = x
  312.     usey% = y
  313.     ' Rather than go through the hassle of partial bitmaps,
  314.     ' we'll simply restrict the merge area to valid parts
  315.     ' of the leaf picture control
  316.     ' Important:  We know that we're using a 32x32 pixel
  317.     ' merge field.  A robust version of this program would
  318.     ' have to calculate the actual size of the merged image
  319.     If usex% < 0 Then usex% = 0
  320.     If usey% < 0 Then usey% = 0
  321.     If usex% + 32 > picLeafs.ScaleWidth Then usex% = picLeafs.ScaleWidth - 32
  322.     If usey% + 32 > picLeafs.ScaleHeight Then usey% = picLeafs.ScaleHeight - 32
  323.     CombineImage usex%, usey%
  324. End Sub
  325.