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 / ch07 / spin.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  15.4 KB  |  481 lines

  1. VERSION 5.00
  2. Begin VB.Form frmSpin 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "Rotation Example"
  5.    ClientHeight    =   6030
  6.    ClientLeft      =   465
  7.    ClientTop       =   1590
  8.    ClientWidth     =   9570
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    PaletteMode     =   1  'UseZOrder
  13.    ScaleHeight     =   402
  14.    ScaleMode       =   3  'Pixel
  15.    ScaleWidth      =   638
  16.    ShowInTaskbar   =   0   'False
  17.    Begin VB.CheckBox chkFun 
  18.       Caption         =   "Fun Mode"
  19.       Height          =   195
  20.       Left            =   7020
  21.       TabIndex        =   20
  22.       Top             =   4800
  23.       Value           =   1  'Checked
  24.       Width           =   1395
  25.    End
  26.    Begin VB.CheckBox Check1 
  27.       Caption         =   "Timer On/OFF"
  28.       Height          =   255
  29.       Left            =   7020
  30.       TabIndex        =   19
  31.       Top             =   4440
  32.       Value           =   1  'Checked
  33.       Width           =   1335
  34.    End
  35.    Begin VB.TextBox txtEM21 
  36.       Height          =   285
  37.       Left            =   8580
  38.       TabIndex        =   17
  39.       Text            =   "0"
  40.       Top             =   4320
  41.       Width           =   735
  42.    End
  43.    Begin VB.TextBox txtEM12 
  44.       Height          =   285
  45.       Left            =   8220
  46.       TabIndex        =   15
  47.       Text            =   "0"
  48.       Top             =   2460
  49.       Width           =   735
  50.    End
  51.    Begin VB.HScrollBar scrM21 
  52.       Height          =   255
  53.       LargeChange     =   5
  54.       Left            =   7620
  55.       Max             =   200
  56.       Min             =   -200
  57.       TabIndex        =   14
  58.       Top             =   4020
  59.       Width           =   1695
  60.    End
  61.    Begin VB.VScrollBar scrM12 
  62.       Height          =   1575
  63.       LargeChange     =   5
  64.       Left            =   7380
  65.       Max             =   200
  66.       Min             =   -200
  67.       TabIndex        =   13
  68.       Top             =   2460
  69.       Width           =   255
  70.    End
  71.    Begin VB.TextBox txtEM22 
  72.       Height          =   285
  73.       Left            =   8220
  74.       TabIndex        =   11
  75.       Text            =   "0"
  76.       Top             =   120
  77.       Width           =   735
  78.    End
  79.    Begin VB.TextBox txtEM11 
  80.       Height          =   285
  81.       Left            =   8640
  82.       TabIndex        =   9
  83.       Text            =   "0"
  84.       Top             =   1980
  85.       Width           =   735
  86.    End
  87.    Begin VB.VScrollBar scrM22 
  88.       Height          =   1575
  89.       LargeChange     =   5
  90.       Left            =   7440
  91.       Max             =   200
  92.       Min             =   -200
  93.       TabIndex        =   8
  94.       Top             =   120
  95.       Width           =   255
  96.    End
  97.    Begin VB.HScrollBar scrM11 
  98.       Height          =   255
  99.       LargeChange     =   5
  100.       Left            =   7680
  101.       Max             =   200
  102.       Min             =   -200
  103.       TabIndex        =   7
  104.       Top             =   1680
  105.       Width           =   1695
  106.    End
  107.    Begin VB.TextBox txteDx 
  108.       Height          =   285
  109.       Left            =   7920
  110.       TabIndex        =   5
  111.       Text            =   "0"
  112.       Top             =   5700
  113.       Width           =   735
  114.    End
  115.    Begin VB.TextBox txteDy 
  116.       Height          =   285
  117.       Left            =   780
  118.       TabIndex        =   3
  119.       Text            =   "0"
  120.       Top             =   240
  121.       Width           =   735
  122.    End
  123.    Begin VB.HScrollBar scrDx 
  124.       Height          =   255
  125.       LargeChange     =   20
  126.       Left            =   360
  127.       Max             =   200
  128.       TabIndex        =   2
  129.       Top             =   5700
  130.       Width           =   7095
  131.    End
  132.    Begin VB.VScrollBar scrDy 
  133.       Height          =   5475
  134.       LargeChange     =   20
  135.       Left            =   120
  136.       Max             =   200
  137.       TabIndex        =   1
  138.       Top             =   240
  139.       Width           =   255
  140.    End
  141.    Begin VB.CommandButton cmdSet 
  142.       Caption         =   "&Set Values"
  143.       Default         =   -1  'True
  144.       Height          =   435
  145.       Left            =   7020
  146.       TabIndex        =   0
  147.       Top             =   5100
  148.       Width           =   1035
  149.    End
  150.    Begin VB.Timer Timer1 
  151.       Interval        =   10
  152.       Left            =   8880
  153.       Top             =   5340
  154.    End
  155.    Begin VB.Label Label1 
  156.       Caption         =   "eM21"
  157.       Height          =   255
  158.       Index           =   2
  159.       Left            =   8880
  160.       TabIndex        =   18
  161.       Top             =   3720
  162.       Width           =   435
  163.    End
  164.    Begin VB.Label Label1 
  165.       Caption         =   "eM12"
  166.       Height          =   255
  167.       Index           =   1
  168.       Left            =   7740
  169.       TabIndex        =   16
  170.       Top             =   2460
  171.       Width           =   495
  172.    End
  173.    Begin VB.Label Label1 
  174.       Caption         =   "eM22"
  175.       Height          =   255
  176.       Index           =   3
  177.       Left            =   7740
  178.       TabIndex        =   12
  179.       Top             =   120
  180.       Width           =   555
  181.    End
  182.    Begin VB.Label Label1 
  183.       Caption         =   "eM11"
  184.       Height          =   255
  185.       Index           =   0
  186.       Left            =   8940
  187.       TabIndex        =   10
  188.       Top             =   1380
  189.       Width           =   435
  190.    End
  191.    Begin VB.Label Label2 
  192.       Caption         =   "eDx"
  193.       Height          =   255
  194.       Index           =   0
  195.       Left            =   7500
  196.       TabIndex        =   6
  197.       Top             =   5700
  198.       Width           =   375
  199.    End
  200.    Begin VB.Label Label2 
  201.       Caption         =   "eDy"
  202.       Height          =   255
  203.       Index           =   1
  204.       Left            =   420
  205.       TabIndex        =   4
  206.       Top             =   240
  207.       Width           =   375
  208.    End
  209. Attribute VB_Name = "frmSpin"
  210. Attribute VB_GlobalNameSpace = False
  211. Attribute VB_Creatable = False
  212. Attribute VB_PredeclaredId = True
  213. Attribute VB_Exposed = False
  214. Option Explicit
  215. ' Copyright 
  216.  1997 by Desaware Inc. All Rights Reserved
  217. Dim RotAngle%, increment%
  218. Dim dummy&
  219. Public dviewX1!, dviewX2!, dviewY1!, dviewY2!
  220. Public hPen&, savedDC&
  221. Dim mySize As SIZE, myPoint As POINTAPI
  222. Public viewOrgX&, viewOrgY&, viewExtX&, viewExtY&
  223. Dim myXform As XFORM
  224. Public PI As Double
  225. Dim UpdatingScrollBars%
  226. '**********************************
  227. '**  Type Definitions:
  228. #If Win32 Then
  229. Private Type XFORM
  230.         eM11 As Single
  231.         eM12 As Single
  232.         eM21 As Single
  233.         eM22 As Single
  234.         eDx As Single
  235.         eDy As Single
  236. End Type
  237. Private Type SIZE
  238.     cx As Long
  239.     cy As Long
  240. End Type
  241. Private Type POINTAPI
  242.     X As Long
  243.     Y As Long
  244. End Type
  245. Private Type RECT
  246.     Left As Long
  247.     Top As Long
  248.     Right As Long
  249.     Bottom As Long
  250. End Type
  251. Const PS_SOLID& = 0
  252. '**********************************
  253. '**  Function Declarations:
  254. Private Declare Function Rectangle& Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long)
  255. Private Declare Function SetWorldTransform& Lib "gdi32" (ByVal hdc As Long, lpXform As XFORM)
  256. Private Declare Function ModifyWorldTransform& Lib "gdi32" (ByVal hdc As Long, lpXform As XFORM, ByVal iMode As Long)
  257. Private Declare Function GetWorldTransform& Lib "gdi32" (ByVal hdc As Long, lpXform As XFORM)
  258. Private Declare Function SetGraphicsMode& Lib "gdi32" (ByVal hdc As Long, ByVal iMode As Long)
  259. Private Declare Function SetMapMode& Lib "gdi32" (ByVal hdc As Long, ByVal nMapMode As Long)
  260. Private Declare Function GetViewportExtEx& Lib "gdi32" (ByVal hdc As Long, lpSize As SIZE)
  261. Private Declare Function GetViewportOrgEx& Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI)
  262. Private Declare Function SetViewportExtEx& Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpSize As SIZE)
  263. Private Declare Function ScaleViewportExtEx& Lib "gdi32" (ByVal hdc As Long, ByVal nXnum As Long, ByVal nXdenom As Long, ByVal nYnum As Long, ByVal nYdenom As Long, lpSize As SIZE)
  264. Private Declare Function SetViewportOrgEx& Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpPoint As POINTAPI)
  265. 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)
  266. Private Declare Function SetWindowExtEx& Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpSize As SIZE)
  267. Private Declare Function SetWindowOrgEx& Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpPoint As POINTAPI)
  268. Private Declare Function GetWindowExtEx& Lib "gdi32" (ByVal hdc As Long, lpSize As SIZE)
  269. Private Declare Function GetWindowOrgEx& Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI)
  270. Private Declare Function CreatePen& Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long)
  271. Private Declare Function SelectObject& Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long)
  272. Private Declare Function DeleteObject& Lib "gdi32" (ByVal hObject As Long)
  273. Private Declare Function SaveDC& Lib "gdi32" (ByVal hdc As Long)
  274. Private Declare Function RestoreDC& Lib "gdi32" (ByVal hdc As Long, ByVal nSavedDC As Long)
  275. Private Declare Function SetROP2& Lib "gdi32" (ByVal hdc As Long, ByVal nDrawMode As Long)
  276. Private Declare Function BitBlt& Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, _
  277.         ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
  278.         ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long)
  279.         
  280. Private Declare Function StretchBlt& Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, _
  281.         ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
  282.         ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
  283.         ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long)
  284.         
  285. Const GM_ADVANCED& = 2
  286. Const MWT_IDENTITY& = 1
  287. Const MM_ANISOTROPIC& = 8
  288. Const R2_COPYPEN& = 13
  289. Const SRCCOPY& = &HCC0020
  290. Private Sub Check1_Click()
  291.     Timer1.Enabled = Check1.Value
  292. End Sub
  293. Private Sub cmdSet_Click()
  294.     myXform.eDy = CSng(txteDy.Text)
  295.     myXform.eDx = CSng(txteDx.Text)
  296.     myXform.eM11 = CSng(txtEM11.Text)
  297.     myXform.eM12 = CSng(txtEM12.Text)
  298.     myXform.eM21 = CSng(txtEM21.Text)
  299.     myXform.eM22 = CSng(txtEM22.Text)
  300.     Me.Refresh
  301. End Sub
  302. #End If 'WIN32
  303. Private Sub Form_Paint()
  304.     Dim savedDC&
  305.     'Save the DC
  306.     savedDC& = SaveDC&(Me.hdc)
  307.     'Make sure we can rotate
  308.     dummy& = SetMapMode&(Me.hdc, MM_ANISOTROPIC)
  309.     dummy& = SetGraphicsMode&(Me.hdc, GM_ADVANCED)
  310.     PI = 3.14159265358979
  311.     dummy& = SetWorldTransform&(Me.hdc, myXform)
  312.     'Set the text boxes
  313.     txtEM11 = CStr(myXform.eM11)
  314.     txtEM12 = CStr(myXform.eM12)
  315.     txtEM21 = CStr(myXform.eM21)
  316.     txtEM22 = CStr(myXform.eM22)
  317.     txteDx = CStr(myXform.eDx)
  318.     txteDy = CStr(myXform.eDy)
  319.     UpdateScrollBars myXform
  320.     'Set the logical window and the viewport to reasonable values
  321.     dummy& = SetWindowOrgEx&(Me.hdc, 0&, 0&, myPoint)
  322.     dummy& = SetWindowExtEx&(Me.hdc, 100&, 100&, mySize)
  323.     dummy& = SetViewportOrgEx&(Me.hdc, 200&, 150&, myPoint)
  324.     dummy& = SetViewportExtEx&(Me.hdc, 100&, 100&, mySize)
  325.     'Draw the color rectangles and the circle
  326.     dummy& = Rectangle&(Me.hdc, 0, 0, 100, 100)
  327.     Me.ForeColor = &HFF
  328.     dummy& = Rectangle&(Me.hdc, 0, 0, 25, 25)
  329.     Me.ForeColor = QBColor(6)
  330.     dummy& = Rectangle&(Me.hdc, 75, 75, 100, 100)
  331.     Me.ForeColor = QBColor(1)
  332.     dummy& = Rectangle&(Me.hdc, 0, 75, 25, 100)
  333.     Me.ForeColor = QBColor(2)
  334.     dummy& = Rectangle&(Me.hdc, 75, 0, 100, 25)
  335.     Me.ForeColor = QBColor(0)
  336.     dummy& = Ellipse&(Me.hdc, 15, 15, 85, 85)
  337.     'Restore the saved DC
  338.     dummy& = RestoreDC(Me.hdc, savedDC&)
  339. End Sub
  340. Private Sub scrDx_Change()
  341.     If UpdatingScrollBars Then Exit Sub
  342.     myXform.eDx = scrDx
  343.     txteDx = "" & scrDx
  344.     Me.Refresh
  345. End Sub
  346. Private Sub scrDx_Scroll()
  347.     myXform.eDx = scrDx
  348.     txteDx = scrDx
  349.     Me.Refresh
  350. End Sub
  351. Private Sub scrDy_Change()
  352.     If UpdatingScrollBars Then Exit Sub
  353.     myXform.eDy = Abs(scrDy)
  354.     txteDy = "" & scrDy
  355.     Me.Refresh
  356. End Sub
  357. Private Sub scrDy_Scroll()
  358.     myXform.eDy = Abs(scrDy)
  359.     txteDy = "" & scrDy
  360.     Me.Refresh
  361. End Sub
  362. Private Sub scrM11_Change()
  363.     If UpdatingScrollBars Then Exit Sub
  364.     myXform.eM11 = CSng(scrM11) / 100!
  365.     txtEM11 = "" & CSng(scrM11) / 100!
  366.     Me.Refresh
  367. End Sub
  368. Private Sub scrM11_Scroll()
  369.     myXform.eM11 = CSng(scrM11) / 100!
  370.     txtEM11 = "" & CSng(scrM11) / 100!
  371.     Me.Refresh
  372. End Sub
  373. Private Sub scrM12_Change()
  374.     If UpdatingScrollBars Then Exit Sub
  375.     myXform.eM12 = CSng(scrM12) / 100!
  376.     txtEM12 = "" & CSng(scrM12) / 100!
  377.     Me.Refresh
  378. End Sub
  379. Private Sub scrM12_Scroll()
  380.     myXform.eM12 = CSng(scrM12) / 100!
  381.     txtEM12 = "" & CSng(scrM12) / 100!
  382.     Me.Refresh
  383. End Sub
  384. Private Sub scrM21_Change()
  385.     If UpdatingScrollBars Then Exit Sub
  386.     myXform.eM21 = CSng(scrM21) / 100!
  387.     txtEM21 = "" & CSng(scrM21) / 100!
  388.     Me.Refresh
  389. End Sub
  390. Private Sub scrM21_Scroll()
  391.     myXform.eM21 = CSng(scrM21) / 100!
  392.     txtEM21 = "" & CSng(scrM21) / 100!
  393.     Me.Refresh
  394. End Sub
  395. Private Sub scrM22_Change()
  396.     If UpdatingScrollBars Then Exit Sub
  397.     myXform.eM22 = CSng(scrM22) / 100!
  398.     txtEM22 = "" & CSng(scrM22) / 100!
  399.     Me.Refresh
  400. End Sub
  401. Private Sub scrM22_Scroll()
  402.     myXform.eM22 = CSng(scrM22) / 100!
  403.     txtEM22 = "" & CSng(scrM22) / 100!
  404.     Me.Refresh
  405. End Sub
  406. Private Sub Timer1_Timer()
  407.     'This functions provides automatic rotation.
  408.     Static increment%
  409.     Dim radAngle!
  410.     increment% = 5
  411.     'Increase the angle
  412.     RotAngle% = RotAngle% + increment%
  413.     'If we're over 360, go back
  414.     If RotAngle% > 360 Then
  415.         RotAngle% = 5
  416.     End If
  417.     'Convert to radians
  418.     radAngle! = RotAngle% / 180! * PI
  419.     'Set the structure
  420.     If chkFun.Value = 1 Then
  421.         ' Fun rotation
  422.         myXform.eDx = 0
  423.         myXform.eDy = 0
  424.         myXform.eM11 = Sin(radAngle!)
  425.         myXform.eM12 = -1 * Cos(radAngle!)
  426.         myXform.eM21 = Cos(radAngle!)
  427.         myXform.eM22 = 1
  428.     Else
  429.         ' Boring rotation
  430.         myXform.eDx = 0
  431.         myXform.eDy = 25
  432.         myXform.eM11 = Cos(radAngle!)
  433.         myXform.eM12 = Sin(radAngle!)
  434.         myXform.eM21 = -Sin(radAngle!)
  435.         myXform.eM22 = Cos(radAngle!)
  436.     End If
  437.         
  438.     'Repaint with the new values
  439.     Me.Refresh
  440. End Sub
  441. Private Sub txteDx_LostFocus()
  442.     If txteDx = "" Then
  443.         txteDx = "0"
  444.     End If
  445. End Sub
  446. Private Sub txteDy_LostFocus()
  447.     If txteDx = "" Then
  448.         txteDx = "0"
  449.     End If
  450. End Sub
  451. Private Sub txtEM11_LostFocus()
  452.     If txteDx = "" Then
  453.         txteDx = "0"
  454.     End If
  455. End Sub
  456. Private Sub txtEM12_LostFocus()
  457.     If txteDx = "" Then
  458.         txteDx = "0"
  459.     End If
  460. End Sub
  461. Private Sub txtEM21_LostFocus()
  462.     If txteDx = "" Then
  463.         txteDx = "0"
  464.     End If
  465. End Sub
  466. Private Sub txtEM22_LostFocus()
  467.     If txteDx = "" Then
  468.         txteDx = "0"
  469.     End If
  470. End Sub
  471. Private Sub UpdateScrollBars(xf As XFORM)
  472.     UpdatingScrollBars = True
  473.     scrDy.Value = xf.eDy
  474.     scrDx.Value = xf.eDx
  475.     scrM22.Value = CInt(xf.eM22 * 100)
  476.     scrM21.Value = CInt(xf.eM21 * 100)
  477.     scrM11.Value = CInt(xf.eM11 * 100)
  478.     scrM12.Value = CInt(xf.eM12 * 100)
  479.     UpdatingScrollBars = False
  480. End Sub
  481.