home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / AALIAS2.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-08  |  12.2 KB  |  402 lines

  1. VERSION 4.00
  2. Begin VB.Form AntiAliasForm 
  3.    Caption         =   "Anti-Aliasing"
  4.    ClientHeight    =   4485
  5.    ClientLeft      =   1905
  6.    ClientTop       =   1275
  7.    ClientWidth     =   5835
  8.    DrawMode        =   14  'Copy Pen
  9.    Height          =   5175
  10.    Left            =   1845
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   299
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   389
  15.    Top             =   645
  16.    Width           =   5955
  17.    Begin VB.CheckBox ColorCheck 
  18.       Caption         =   "Color"
  19.       Height          =   255
  20.       Left            =   3120
  21.       TabIndex        =   9
  22.       Top             =   45
  23.       Value           =   1  'Checked
  24.       Width           =   735
  25.    End
  26.    Begin VB.CommandButton CmdGo 
  27.       Caption         =   "Go"
  28.       Default         =   -1  'True
  29.       Height          =   375
  30.       Left            =   4080
  31.       TabIndex        =   8
  32.       Top             =   0
  33.       Width           =   615
  34.    End
  35.    Begin VB.TextBox ScaleText 
  36.       Height          =   285
  37.       Left            =   2520
  38.       TabIndex        =   6
  39.       Text            =   "2"
  40.       Top             =   30
  41.       Width           =   375
  42.    End
  43.    Begin VB.PictureBox EnlargedPic 
  44.       AutoRedraw      =   -1  'True
  45.       BackColor       =   &H00C0C0C0&
  46.       ForeColor       =   &H00000000&
  47.       Height          =   3870
  48.       Left            =   1965
  49.       Picture         =   "AALIAS2.frx":0000
  50.       ScaleHeight     =   254
  51.       ScaleMode       =   3  'Pixel
  52.       ScaleWidth      =   254
  53.       TabIndex        =   4
  54.       Top             =   600
  55.       Width           =   3870
  56.    End
  57.    Begin VB.PictureBox AntiAliasedPic 
  58.       AutoRedraw      =   -1  'True
  59.       BackColor       =   &H00C0C0C0&
  60.       ForeColor       =   &H00000000&
  61.       Height          =   1935
  62.       Left            =   0
  63.       Picture         =   "AALIAS2.frx":0446
  64.       ScaleHeight     =   125
  65.       ScaleMode       =   3  'Pixel
  66.       ScaleWidth      =   125
  67.       TabIndex        =   2
  68.       Top             =   2520
  69.       Width           =   1935
  70.    End
  71.    Begin VB.PictureBox AliasedPic 
  72.       AutoRedraw      =   -1  'True
  73.       BackColor       =   &H00C0C0C0&
  74.       BeginProperty Font 
  75.          name            =   "Times New Roman"
  76.          charset         =   0
  77.          weight          =   700
  78.          size            =   15.75
  79.          underline       =   0   'False
  80.          italic          =   -1  'True
  81.          strikethrough   =   0   'False
  82.       EndProperty
  83.       ForeColor       =   &H00000000&
  84.       Height          =   1935
  85.       Left            =   0
  86.       Picture         =   "AALIAS2.frx":088C
  87.       ScaleHeight     =   125
  88.       ScaleMode       =   3  'Pixel
  89.       ScaleWidth      =   125
  90.       TabIndex        =   0
  91.       Top             =   240
  92.       Width           =   1935
  93.    End
  94.    Begin VB.Label Label1 
  95.       Caption         =   "Scale"
  96.       Height          =   255
  97.       Index           =   3
  98.       Left            =   2040
  99.       TabIndex        =   7
  100.       Top             =   45
  101.       Width           =   495
  102.    End
  103.    Begin VB.Label Label1 
  104.       Caption         =   "Enlarged"
  105.       Height          =   255
  106.       Index           =   2
  107.       Left            =   1965
  108.       TabIndex        =   5
  109.       Top             =   360
  110.       Width           =   735
  111.    End
  112.    Begin VB.Label Label1 
  113.       Caption         =   "Anti-Aliased"
  114.       Height          =   255
  115.       Index           =   1
  116.       Left            =   0
  117.       TabIndex        =   3
  118.       Top             =   2280
  119.       Width           =   975
  120.    End
  121.    Begin VB.Label Label1 
  122.       Caption         =   "Aliased"
  123.       Height          =   255
  124.       Index           =   0
  125.       Left            =   0
  126.       TabIndex        =   1
  127.       Top             =   0
  128.       Width           =   615
  129.    End
  130.    Begin VB.Menu mnuFile 
  131.       Caption         =   "&File"
  132.       Begin VB.Menu mnuFileExit 
  133.          Caption         =   "E&xit"
  134.       End
  135.    End
  136. Attribute VB_Name = "AntiAliasForm"
  137. Attribute VB_Creatable = False
  138. Attribute VB_Exposed = False
  139. Option Explicit
  140. ' ************************************************
  141. ' Redraw the original stuff.
  142. ' ************************************************
  143. Private Sub ColorCheck_Click()
  144.     DrawIt AliasedPic
  145. End Sub
  146. ' ************************************************
  147. ' Anti-alias.
  148. ' ************************************************
  149. Sub CmdGo_Click()
  150. Dim S As Integer
  151.     MousePointer = vbHourglass
  152.     ' Make EnlargedPic the correct size.
  153.     If Not IsNumeric(ScaleText.Text) Then _
  154.         ScaleText.Text = "2"
  155.     S = CInt(ScaleText.Text)
  156.     If S < 1 Then
  157.         ScaleText.Text = "2"
  158.         S = 2
  159.     End If
  160.     EnlargedPic.Width = _
  161.         EnlargedPic.Width - _
  162.         EnlargedPic.ScaleWidth + _
  163.         S * AliasedPic.ScaleWidth + S
  164.     EnlargedPic.Height = _
  165.         EnlargedPic.Height - _
  166.         EnlargedPic.ScaleHeight + _
  167.         S * AliasedPic.ScaleHeight + S
  168.     ' Make EnlargedPic use the right thicknesses.
  169.     EnlargedPic.DrawWidth = S * AliasedPic.DrawWidth
  170.     EnlargedPic.Font.Size = S * AliasedPic.Font.Size
  171.     ' Draw the enlarged picture.
  172.     AntiAliasedPic.Cls
  173.     DrawIt EnlargedPic
  174.     DoEvents
  175.     ' Shrink the enlarged picture.
  176.     ShrinkPicture EnlargedPic, AntiAliasedPic, S
  177.     MousePointer = vbDefault
  178. End Sub
  179. ' ************************************************
  180. ' Draw some stuff to work with.
  181. ' ************************************************
  182. Sub BWDrawStuff(pic As PictureBox)
  183. Const PI = 3.14159
  184. Const MSG = "Smile!"
  185. Dim x1 As Single
  186. Dim x2 As Single
  187. Dim x3 As Single
  188. Dim x4 As Single
  189. Dim x5 As Single
  190. Dim x6 As Single
  191. Dim x7 As Single
  192. Dim y1 As Single
  193. Dim y2 As Single
  194. Dim dy As Single
  195. Dim r1 As Single
  196. Dim r2 As Single
  197. Dim r3 As Single
  198. Dim r4 As Single
  199.     x1 = pic.ScaleWidth * 0.4
  200.     x2 = pic.ScaleWidth * 0.27
  201.     x3 = pic.ScaleWidth * 0.53
  202.     x4 = pic.ScaleWidth * 0.29
  203.     x5 = pic.ScaleWidth * 0.55
  204.     x6 = pic.ScaleWidth * 0.8
  205.     x7 = pic.ScaleWidth * 1
  206.     y1 = pic.ScaleHeight * 0.4
  207.     y2 = pic.ScaleHeight * 0.25
  208.     r1 = pic.ScaleHeight * 0.35
  209.     r2 = pic.ScaleHeight * 0.25
  210.     r3 = pic.ScaleHeight * 0.05
  211.     r4 = pic.ScaleHeight * 0.0375
  212.     pic.Cls
  213.     pic.Circle (x1, y1), r1
  214.     pic.Circle (x1, y1), r2, , PI, 2 * PI
  215.     pic.Circle (x1, y1), r3
  216.     pic.Circle (x2, y2), r3
  217.     pic.Circle (x3, y2), r3
  218.     pic.FillStyle = vbFSSolid
  219.     pic.Circle (x4, y2), r4, , , , 1.5
  220.     pic.Circle (x5, y2), r4, , , , 1.5
  221.     pic.FillStyle = vbFSTransparent
  222.     pic.CurrentX = x1 - pic.TextWidth(MSG) / 2
  223.     pic.CurrentY = (pic.ScaleHeight + y1 + r1 _
  224.         - pic.TextHeight(MSG)) / 2
  225.     pic.Print MSG
  226.     dy = pic.ScaleHeight / 15
  227.     For y1 = dy / 2 To pic.ScaleHeight Step dy
  228.         pic.Line (x6, y1)-(x7, y1 * 2)
  229.     Next y1
  230. End Sub
  231. ' ************************************************
  232. ' Draw stuff in color or black and white.
  233. ' ************************************************
  234. Sub DrawIt(pic As PictureBox)
  235.     If ColorCheck.Value = vbChecked Then
  236.         ColorDrawStuff pic
  237.     Else
  238.         BWDrawStuff pic
  239.     End If
  240. End Sub
  241. ' ************************************************
  242. ' Draw some stuff to work with.
  243. ' ************************************************
  244. Sub ColorDrawStuff(pic As PictureBox)
  245. Const PI = 3.14159
  246. Const MSG = "Smile!"
  247. Dim x1 As Single
  248. Dim x2 As Single
  249. Dim x3 As Single
  250. Dim x4 As Single
  251. Dim x5 As Single
  252. Dim x6 As Single
  253. Dim x7 As Single
  254. Dim y1 As Single
  255. Dim y2 As Single
  256. Dim dy As Single
  257. Dim r1 As Single
  258. Dim r2 As Single
  259. Dim r3 As Single
  260. Dim r4 As Single
  261.     x1 = pic.ScaleWidth * 0.4
  262.     x2 = pic.ScaleWidth * 0.27
  263.     x3 = pic.ScaleWidth * 0.53
  264.     x4 = pic.ScaleWidth * 0.29
  265.     x5 = pic.ScaleWidth * 0.55
  266.     x6 = pic.ScaleWidth * 0.8
  267.     x7 = pic.ScaleWidth * 1
  268.     y1 = pic.ScaleHeight * 0.4
  269.     y2 = pic.ScaleHeight * 0.25
  270.     r1 = pic.ScaleHeight * 0.35
  271.     r2 = pic.ScaleHeight * 0.25
  272.     r3 = pic.ScaleHeight * 0.05
  273.     r4 = pic.ScaleHeight * 0.0375
  274.     pic.Cls
  275.     pic.FillStyle = vbFSSolid
  276.     pic.FillColor = vbYellow
  277.     pic.ForeColor = pic.FillColor
  278.     pic.Circle (x1, y1), r1
  279.     pic.FillColor = RGB(255, 153, 51)
  280.     pic.ForeColor = pic.FillColor
  281.     pic.Circle (x1, y1), r3
  282.     pic.FillColor = vbWhite
  283.     pic.ForeColor = vbBlack
  284.     pic.Circle (x2, y2), r3
  285.     pic.Circle (x3, y2), r3
  286.     pic.FillColor = vbBlack
  287.     pic.Circle (x4, y2), r4, , , , 1.5
  288.     pic.Circle (x5, y2), r4, , , , 1.5
  289.     pic.FillStyle = vbFSTransparent
  290.     pic.ForeColor = vbRed
  291.     pic.Circle (x1, y1), r2, , PI, 2 * PI
  292.     pic.ForeColor = vbBlue
  293.     pic.CurrentX = x1 - pic.TextWidth(MSG) / 2
  294.     pic.CurrentY = (pic.ScaleHeight + y1 + r1 _
  295.         - pic.TextHeight(MSG)) / 2
  296.     pic.Print MSG
  297.     pic.ForeColor = RGB(&H80, 0, &H80)
  298.     dy = pic.ScaleHeight / 15
  299.     For y1 = dy / 2 To pic.ScaleHeight Step dy
  300.         pic.Line (x6, y1)-(x7, y1 * 2)
  301.     Next y1
  302.     pic.ForeColor = vbBlack
  303. End Sub
  304. ' ************************************************
  305. ' Shrink fpic into tpic, reducing by a factor of
  306. ' 1/s.
  307. ' ************************************************
  308. Sub ShrinkPicture(fpic As PictureBox, tpic As PictureBox, S As Integer)
  309. Dim SysPal(0 To 255) As PALETTEENTRY
  310. Dim x As Integer
  311. Dim y As Integer
  312. Dim i As Integer
  313. Dim j As Integer
  314. Dim r As Long
  315. Dim g As Long
  316. Dim b As Long
  317. Dim status As Long
  318. Dim bm As BITMAP
  319. Dim hbm As Integer
  320. Dim wid As Long
  321. Dim hgt As Long
  322. Dim fbytes() As Byte
  323. Dim tbytes() As Byte
  324. Dim pos As Integer
  325.     ' Make sure fpic has the foreground palette.
  326.     fpic.ZOrder
  327.     status = RealizePalette(fpic.hdc)
  328.     DoEvents
  329.     ' Get the system palette entries.
  330.     status = GetSystemPaletteEntries(fpic.hdc, 0, 256, SysPal(0))
  331.         
  332.     ' Get the input pixels.
  333.     hbm = fpic.Image
  334.     status = GetObject(hbm, BITMAP_SIZE, bm)
  335.     wid = bm.bmWidthBytes
  336.     hgt = bm.bmHeight
  337.     ReDim fbytes(0 To wid - 1, 0 To hgt - 1)
  338.     status = GetBitmapBits(hbm, wid * hgt, fbytes(0, 0))
  339.     ' Dimension the output pixel array.
  340.     hbm = tpic.Image
  341.     status = GetObject(hbm, BITMAP_SIZE, bm)
  342.     wid = bm.bmWidthBytes
  343.     hgt = bm.bmHeight
  344.     ReDim tbytes(0 To wid - 1, 0 To hgt - 1)
  345.     ' Shrink the image.
  346.     For y = 0 To hgt - 1
  347.         For x = 0 To wid - 1
  348.             ' Compute the value of pixel (x, y).
  349.             r = 0
  350.             g = 0
  351.             b = 0
  352.             For i = 0 To S - 1
  353.                 For j = 0 To S - 1
  354.                     pos = fbytes(S * x + j, S * y + i)
  355.                     r = r + SysPal(pos).peRed
  356.                     g = g + SysPal(pos).peGreen
  357.                     b = b + SysPal(pos).peBlue
  358.                 Next j
  359.             Next i
  360.             ' Set the output pixel's value.
  361.             r = r / S / S
  362.             g = g / S / S
  363.             b = b / S / S
  364.             tpic.PSet (x, y), RGB(r, g, b)
  365.         Next x
  366.         DoEvents
  367.     Next y
  368. End Sub
  369. Private Sub Form_Load()
  370.     ' Make sure the screen supports palettes.
  371.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  372.         Beep
  373.         MsgBox "This monitor does not support palettes.", _
  374.             vbCritical
  375.         End
  376.     End If
  377.     ' Make everyone use the same font.
  378.     AntiAliasedPic.Font.Name = AliasedPic.Font.Name
  379.     AntiAliasedPic.Font.Bold = AliasedPic.Font.Bold
  380.     AntiAliasedPic.Font.Italic = AliasedPic.Font.Italic
  381.     AntiAliasedPic.Font.Strikethrough = AliasedPic.Font.Strikethrough
  382.     AntiAliasedPic.Font.Underline = AliasedPic.Font.Underline
  383.     EnlargedPic.Font.Name = AliasedPic.Font.Name
  384.     EnlargedPic.Font.Bold = AliasedPic.Font.Bold
  385.     EnlargedPic.Font.Italic = AliasedPic.Font.Italic
  386.     EnlargedPic.Font.Strikethrough = AliasedPic.Font.Strikethrough
  387.     EnlargedPic.Font.Underline = AliasedPic.Font.Underline
  388.         
  389.     ' Make AntiAliasedPic use the right thicknesses.
  390.     AntiAliasedPic.DrawWidth = AliasedPic.DrawWidth
  391.     AntiAliasedPic.Font.Size = AliasedPic.Font.Size
  392.         
  393.     ' Draw original stuff.
  394.     DrawIt AliasedPic
  395. End Sub
  396. Private Sub Form_Unload(Cancel As Integer)
  397.     End
  398. End Sub
  399. Private Sub mnuFileExit_Click()
  400.     Unload Me
  401. End Sub
  402.