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

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