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 / ch08 / path.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  10.3 KB  |  267 lines

  1. VERSION 4.00
  2. Begin VB.Form frmPaths 
  3.    Caption         =   "Paths Example"
  4.    ClientHeight    =   5355
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1515
  7.    ClientWidth     =   6465
  8.    ClipControls    =   0   'False
  9.    Height          =   5760
  10.    Left            =   1035
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   5355
  13.    ScaleWidth      =   6465
  14.    Top             =   1170
  15.    Width           =   6585
  16.    Begin VB.CommandButton cmdWhich 
  17.       Caption         =   "Stroke"
  18.       Height          =   435
  19.       Index           =   3
  20.       Left            =   180
  21.       TabIndex        =   6
  22.       Top             =   4740
  23.       Width           =   1155
  24.    End
  25.    Begin VB.CommandButton cmdExit 
  26.       Caption         =   "E&xit"
  27.       Height          =   435
  28.       Left            =   5100
  29.       TabIndex        =   5
  30.       Top             =   4200
  31.       Width           =   1155
  32.    End
  33.    Begin VB.CommandButton cmdWhich 
  34.       Caption         =   "Bitmap"
  35.       Height          =   435
  36.       Index           =   2
  37.       Left            =   2940
  38.       TabIndex        =   4
  39.       Top             =   4200
  40.       Width           =   1155
  41.    End
  42.    Begin VB.CommandButton cmdWhich 
  43.       Caption         =   "Color Circles"
  44.       Height          =   435
  45.       Index           =   1
  46.       Left            =   1560
  47.       TabIndex        =   3
  48.       Top             =   4200
  49.       Width           =   1155
  50.    End
  51.    Begin VB.CommandButton cmdWhich 
  52.       Caption         =   "Radial Lines"
  53.       Height          =   435
  54.       Index           =   0
  55.       Left            =   180
  56.       TabIndex        =   2
  57.       Top             =   4200
  58.       Width           =   1155
  59.    End
  60.    Begin VB.PictureBox Out 
  61.       ClipControls    =   0   'False
  62.       FillColor       =   &H000000FF&
  63.       BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  64.          Name            =   "Times New Roman"
  65.          Size            =   63.75
  66.          Charset         =   0
  67.          Weight          =   700
  68.          Underline       =   0   'False
  69.          Italic          =   0   'False
  70.          Strikethrough   =   0   'False
  71.       EndProperty
  72.       FontTransparent =   0   'False
  73.       ForeColor       =   &H00FF0000&
  74.       Height          =   3915
  75.       Left            =   180
  76.       ScaleHeight     =   259
  77.       ScaleMode       =   3  'Pixel
  78.       ScaleWidth      =   403
  79.       TabIndex        =   0
  80.       Top             =   120
  81.       Width           =   6075
  82.    End
  83.    Begin VB.PictureBox pctBmp 
  84.       AutoRedraw      =   -1  'True
  85.       AutoSize        =   -1  'True
  86.       Height          =   1830
  87.       Left            =   60
  88.       Picture         =   "path.frx":0000
  89.       ScaleHeight     =   120
  90.       ScaleMode       =   3  'Pixel
  91.       ScaleWidth      =   160
  92.       TabIndex        =   1
  93.       Top             =   60
  94.       Visible         =   0   'False
  95.       Width           =   2430
  96.    End
  97. Attribute VB_Name = "frmPaths"
  98. Attribute VB_Creatable = False
  99. Attribute VB_Exposed = False
  100. Option Explicit
  101. ' Copyright 
  102.  1997 by Desaware Inc. All Rights Reserved
  103. Public dl&, savedDC&, WhichDrawing%
  104. Private allCos!(90), allSin!(90)
  105. '**********************************
  106. '**  Type Definitions:
  107. #If Win32 Then
  108. Private Type RECT
  109.     left As Long
  110.     top As Long
  111.     right As Long
  112.     bottom As Long
  113. End Type
  114. Private Type POINTAPI
  115.     x As Long
  116.     y As Long
  117. End Type
  118. #End If 'WIN32 Types
  119. #If Win32 Then
  120. Private Const SRCPAINT& = &HEE0086
  121. Private Const SRCCOPY& = &HCC0020
  122. Private Const SRCAND& = &H8800C6
  123. Private Const SRCERASE& = &H440328
  124. Private Const SRCINVERT& = &H660046
  125. Private Const TRANSPARENT& = 1
  126. Private Const RGN_COPY& = 5
  127. Private Const RGN_AND& = 1
  128. Private Const RGN_DIFF& = 4
  129. Private Const RGN_XOR& = 3
  130. Private Const RGN_OR& = 2
  131. Private Const BLACK_BRUSH = 4
  132. Private Const BLACK_PEN = 7
  133. '  Pen Styles
  134. Private Const PS_SOLID = 0
  135. Private Const PS_DASH = 1                    '  -------
  136. Private Const PS_DOT = 2                     '  .......
  137. Private Const PS_DASHDOT = 3                 '  _._._._
  138. Private Const PS_DASHDOTDOT = 4              '  _.._.._
  139. Private Const PS_NULL = 5
  140. Private Const PS_INSIDEFRAME = 6
  141. Private Const PS_USERSTYLE = 7
  142. Private Const PS_ALTERNATE = 8
  143. Private Const PS_STYLE_MASK = &HF
  144. #End If 'WIN32
  145. '**********************************
  146. '**  Function Declarations:
  147. #If Win32 Then
  148. Private Declare Function SelectObject& Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long)
  149. Private Declare Function DeleteObject& Lib "gdi32" (ByVal hObject As Long)
  150. Private Declare Function CreatePen& Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long)
  151. Private Declare Function GetStockObject& Lib "gdi32" (ByVal nIndex As Long)
  152. Private Declare Function TextOut& Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long)
  153. Private Declare Function Ellipse& Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long)
  154. Private Declare Function MoveToEx& Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, Prev As POINTAPI)
  155. Private Declare Function LineTo& Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long)
  156. Private Declare Function GetLastError& Lib "kernel32" ()
  157. Private Declare Function GetRgnBox& Lib "gdi32" (ByVal hdc As Long, bounds As RECT)
  158. Private Declare Function GetClipRgn& Lib "gdi32" (ByVal hdc As Long, ByVal hRegion As Long)
  159. Private Declare Function SaveDC& Lib "gdi32" (ByVal hdc As Long)
  160. Private Declare Function RestoreDC& Lib "gdi32" (ByVal hdc As Long, ByVal nSavedDC As Long)
  161. Private Declare Function DrawText& Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long)
  162. Private Declare Function BeginPath& Lib "gdi32" (ByVal hdc As Long)
  163. Private Declare Function EndPath& Lib "gdi32" (ByVal hdc As Long)
  164. Private Declare Function AbortPath& Lib "gdi32" (ByVal hdc As Long)
  165. Private Declare Function CloseFigure& Lib "gdi32" (ByVal hdc As Long)
  166. Private Declare Function StrokeAndFillPath& Lib "gdi32" (ByVal hdc As Long)
  167. Private Declare Function StrokePath& Lib "gdi32" (ByVal hdc As Long)
  168. Private Declare Function GetPath& Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, lpTypes As Byte, ByVal nSize As Long)
  169. Private Declare Function FlattenPath& Lib "gdi32" (ByVal hdc As Long)
  170. Private Declare Function SelectClipPath& Lib "gdi32" (ByVal hdc As Long, ByVal iMode As Long)
  171. Private Declare Function PathToRegion& Lib "gdi32" (ByVal hdc As Long)
  172. Private Declare Function CreateRectRgn& Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long)
  173. Private Declare Function SelectClipRgn& Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long)
  174. Private Declare Function FillRgn& Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long)
  175. Private Declare Function BitBlt& Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, _
  176.     ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, _
  177.     ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long)
  178. #End If 'WIN32
  179. Private Sub cmdExit_Click()
  180.     Unload Me
  181. End Sub
  182. Private Sub cmdWhich_Click(Index As Integer)
  183.     WhichDrawing% = Index
  184.     out.Refresh
  185. End Sub
  186. Private Sub Form_Load()
  187.     Dim i%
  188.     Screen.MousePointer = 11
  189.     For i% = 1 To 90
  190.         allCos!(i%) = Cos(CSng(i%) / 180! * 3.14159)
  191.         allSin!(i%) = Sin(CSng(i%) / 180! * 3.14159)
  192.     Next i%
  193.     Screen.MousePointer = 0
  194. End Sub
  195. Private Sub out_Paint()
  196.     Dim usepen&, oldpen&
  197.     Dim dl&
  198.     Dim myRect As RECT, radius&, i%, j%, myPoint As POINTAPI
  199.     savedDC& = SaveDC&(out.hdc)
  200.     Select Case WhichDrawing%
  201.     Case 0 ' Lines ( black on gray)
  202.         dl& = BeginPath&(out.hdc)
  203.            dl& = TextOut&(out.hdc, 50&, 30&, "Radial", 6)
  204.            dl& = TextOut&(out.hdc, 80&, 120&, "Lines", 5)
  205.         dl& = EndPath&(out.hdc)
  206.         dl& = SelectClipPath&(out.hdc, RGN_COPY)
  207.         radius& = 600
  208.         
  209.         ' Don't delete stock pens
  210.         usepen = GetStockObject(BLACK_PEN)
  211.         oldpen = SelectObject(out.hdc, usepen)
  212.         For i% = 1 To 90
  213.              dl& = MoveToEx&(out.hdc, out.ScaleWidth, 0, myPoint)
  214.              dl& = LineTo&(out.hdc, out.ScaleWidth - allCos!(i%) * CSng(radius&), 0 + allSin!(i%) * CSng(radius&))
  215.         Next i%
  216.     Case 1 ' Color Circles:
  217.         dl& = BeginPath&(out.hdc)
  218.             dl& = TextOut&(out.hdc, 80&, 30&, "Color", 5)
  219.             dl& = TextOut&(out.hdc, 50&, 120&, "Circles", 7)
  220.         dl& = EndPath&(out.hdc)
  221.         dl& = SelectClipPath&(out.hdc, RGN_COPY)
  222.         usepen = CreatePen(PS_SOLID, 1, QBColor(12))
  223.         oldpen = SelectObject(out.hdc, usepen)
  224.         For i% = 0 To 40
  225.             dl& = Ellipse&(out.hdc, i% * out.ScaleWidth / 80, i% * out.ScaleHeight / 80, out.ScaleWidth - (i% * out.ScaleWidth / 80), out.ScaleHeight - (i% * out.ScaleHeight / 80))
  226.         Next i%
  227.         ' Select out the pen before deleting
  228.         dl = SelectObject(out.hdc, oldpen)
  229.         dl = DeleteObject(usepen)
  230.         
  231.     Case 2 ' Bitmap:
  232.         dl& = BeginPath&(out.hdc)
  233.         dl& = TextOut&(out.hdc, 80&, 30&, "Color", 5)
  234.         dl& = TextOut&(out.hdc, 50&, 120&, "Bitmap", 6)
  235.         dl& = EndPath&(out.hdc)
  236.         dl& = SelectClipPath&(out.hdc, RGN_COPY)
  237.         TileBitmaps
  238.     Case 3
  239.         dl& = BeginPath&(out.hdc)
  240.         dl& = TextOut&(out.hdc, 50&, 30&, "Stroke", 6)
  241.         dl& = TextOut&(out.hdc, 80&, 120&, "Path", 4)
  242.         dl& = EndPath&(out.hdc)
  243.         StrokeThePath
  244.     End Select
  245.         
  246.     dl& = RestoreDC&(out.hdc, savedDC&)
  247. End Sub
  248. ' Tile the bitmap onto the output picture
  249. Public Sub TileBitmaps()
  250.     Dim dl&, i%, j%
  251.     For i% = 0 To 5
  252.         For j% = 0 To 5
  253.             dl& = BitBlt&(out.hdc, 0 + i% * 160, 0 + j% * 120, 160, 120, pctBmp.hdc, 0, 0, SRCCOPY)
  254.         Next j%
  255.     Next i%
  256. End Sub
  257. Public Sub StrokeThePath()
  258.     Dim dl&, usepen&, oldpen&
  259.     ' Create a green pen
  260.     usepen = CreatePen(PS_SOLID, 3, RGB(0, 255, 0))
  261.     oldpen = SelectObject(out.hdc, usepen)
  262.     dl = StrokePath(out.hdc)
  263.     ' Select out the pen before deleting
  264.     dl = SelectObject(out.hdc, oldpen)
  265.     dl = DeleteObject(usepen)
  266. End Sub
  267.