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 / viewport.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  15.0 KB  |  375 lines

  1. VERSION 5.00
  2. Begin VB.Form frmViewport 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Viewport Example"
  5.    ClientHeight    =   4410
  6.    ClientLeft      =   1380
  7.    ClientTop       =   2040
  8.    ClientWidth     =   7920
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    PaletteMode     =   1  'UseZOrder
  12.    ScaleHeight     =   294
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   528
  15.    Begin VB.CommandButton cmdSettings 
  16.       Caption         =   "&Settings..."
  17.       Default         =   -1  'True
  18.       Height          =   495
  19.       Left            =   3840
  20.       TabIndex        =   4
  21.       Top             =   3840
  22.       Width           =   1095
  23.    End
  24.    Begin VB.TextBox txtRotate 
  25.       Alignment       =   1  'Right Justify
  26.       Height          =   285
  27.       Left            =   900
  28.       MaxLength       =   3
  29.       TabIndex        =   3
  30.       Text            =   "0"
  31.       Top             =   3960
  32.       Visible         =   0   'False
  33.       Width           =   435
  34.    End
  35.    Begin VB.CommandButton cmdExit 
  36.       Cancel          =   -1  'True
  37.       Caption         =   "E&xit"
  38.       Height          =   495
  39.       Left            =   6600
  40.       TabIndex        =   1
  41.       Top             =   3840
  42.       Width           =   1095
  43.    End
  44.    Begin VB.PictureBox pctScreen 
  45.       DrawMode        =   7  'Invert
  46.       DrawStyle       =   2  'Dot
  47.       FillColor       =   &H00C0C0C0&
  48.       ForeColor       =   &H000000FF&
  49.       Height          =   3615
  50.       Left            =   60
  51.       ScaleHeight     =   239
  52.       ScaleMode       =   3  'Pixel
  53.       ScaleWidth      =   510
  54.       TabIndex        =   0
  55.       TabStop         =   0   'False
  56.       Top             =   120
  57.       Width           =   7680
  58.       Begin VB.PictureBox Picture1 
  59.          Height          =   855
  60.          Left            =   6420
  61.          ScaleHeight     =   825
  62.          ScaleWidth      =   1125
  63.          TabIndex        =   5
  64.          Top             =   180
  65.          Width           =   1155
  66.          Begin VB.Line Line3 
  67.             BorderColor     =   &H00FF0000&
  68.             X1              =   240
  69.             X2              =   120
  70.             Y1              =   480
  71.             Y2              =   600
  72.          End
  73.          Begin VB.Line Line2 
  74.             BorderColor     =   &H00FF0000&
  75.             X1              =   120
  76.             X2              =   240
  77.             Y1              =   480
  78.             Y2              =   600
  79.          End
  80.          Begin VB.Label Label2 
  81.             Caption         =   "Window Origin"
  82.             Height          =   435
  83.             Left            =   420
  84.             TabIndex        =   7
  85.             Top             =   420
  86.             Width           =   615
  87.             WordWrap        =   -1  'True
  88.          End
  89.          Begin VB.Line Line1 
  90.             BorderColor     =   &H00FFFF00&
  91.             BorderWidth     =   2
  92.             X1              =   60
  93.             X2              =   300
  94.             Y1              =   240
  95.             Y2              =   240
  96.          End
  97.          Begin VB.Label Label1 
  98.             Caption         =   "Viewport"
  99.             Height          =   255
  100.             Left            =   420
  101.             TabIndex        =   6
  102.             Top             =   120
  103.             Width           =   675
  104.          End
  105.       End
  106.    End
  107.    Begin VB.Label lblRotate 
  108.       Caption         =   "Rotate:"
  109.       Height          =   255
  110.       Left            =   240
  111.       TabIndex        =   2
  112.       Top             =   3990
  113.       Visible         =   0   'False
  114.       Width           =   615
  115.    End
  116. Attribute VB_Name = "frmViewport"
  117. Attribute VB_GlobalNameSpace = False
  118. Attribute VB_Creatable = False
  119. Attribute VB_PredeclaredId = True
  120. Attribute VB_Exposed = False
  121. ' Viewport example
  122. ' Copyright 
  123.  1997 by Desaware Inc. All Rights Reserved
  124. Option Explicit
  125. Dim dummy&
  126. Public dviewX1!, dviewX2!, dviewY1!, dviewY2! ' anchors for the viewport drag
  127. Public savedDC&, RotAngle%
  128. Dim mySize As SIZE, myPoint As POINTAPI
  129. Public viewOrgX&, viewOrgY&, viewExtX&, viewExtY&
  130. Public WinOrgX&, WinOrgY&, WinExtX&, WinExtY&
  131. Public RectX1&, RectY1&, RectX2&, RectY2&
  132. '**********************************
  133. '**  Type Definitions:
  134. #If Win32 Then
  135. Private Type XFORM
  136.         eM11 As Single
  137.         eM12 As Single
  138.         eM21 As Single
  139.         eM22 As Single
  140.         eDx As Single
  141.         eDy As Single
  142. End Type
  143. Private Type SIZE
  144.     cx As Long
  145.     cy As Long
  146. End Type
  147. Private Type POINTAPI
  148.     X As Long
  149.     Y As Long
  150. End Type
  151. Private Type RECT
  152.     Left As Long
  153.     Top As Long
  154.     Right As Long
  155.     Bottom As Long
  156. End Type
  157. Const PS_SOLID& = 0
  158. '**********************************
  159. '**  Function Declarations:
  160. Private Declare Function LineTo& Lib "gdi32" (ByVal hdc As Long, ByVal X2 As Long, ByVal Y2 As Long)
  161. Private Declare Function MoveToEx& Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, oldPoint As POINTAPI)
  162. 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)
  163. Private Declare Function SetWorldTransform& Lib "gdi32" (ByVal hdc As Long, lpXform As XFORM)
  164. Private Declare Function ModifyWorldTransform& Lib "gdi32" (ByVal hdc As Long, lpXform As XFORM, ByVal iMode As Long)
  165. Private Declare Function GetWorldTransform& Lib "gdi32" (ByVal hdc As Long, lpXform As XFORM)
  166. Private Declare Function SetGraphicsMode& Lib "gdi32" (ByVal hdc As Long, ByVal iMode As Long)
  167. Private Declare Function SetMapMode& Lib "gdi32" (ByVal hdc As Long, ByVal nMapMode As Long)
  168. Private Declare Function LPtoDP& Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long)
  169. Private Declare Function DPtoLP& Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long)
  170. Private Declare Function GetViewportExtEx& Lib "gdi32" (ByVal hdc As Long, lpSize As SIZE)
  171. Private Declare Function GetViewportOrgEx& Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI)
  172. Private Declare Function SetViewportExtEx& Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpSize As SIZE)
  173. 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)
  174. Private Declare Function SetViewportOrgEx& Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpPoint As POINTAPI)
  175. 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)
  176. Private Declare Function SetWindowExtEx& Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpSize As SIZE)
  177. Private Declare Function SetWindowOrgEx& Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpPoint As POINTAPI)
  178. Private Declare Function GetWindowExtEx& Lib "gdi32" (ByVal hdc As Long, lpSize As SIZE)
  179. Private Declare Function GetWindowOrgEx& Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI)
  180. Private Declare Function CreatePen& Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long)
  181. Private Declare Function SelectObject& Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long)
  182. Private Declare Function DeleteObject& Lib "gdi32" (ByVal hObject As Long)
  183. Private Declare Function SaveDC& Lib "gdi32" (ByVal hdc As Long)
  184. Private Declare Function RestoreDC& Lib "gdi32" (ByVal hdc As Long, ByVal nSavedDC As Long)
  185. Private Declare Function ClipCursor& Lib "user32" (lpRect As RECT)
  186. Private Declare Function ClipCursorBynum& Lib "user32" Alias "ClipCursor" (ByVal lpRect As Long)
  187. Private Declare Function ClientToScreen& Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI)
  188. Private Declare Function SetROP2& Lib "gdi32" (ByVal hdc As Long, ByVal nDrawMode As Long)
  189. Const GM_ADVANCED& = 2
  190. Const MWT_IDENTITY& = 1
  191. Const MM_ANISOTROPIC& = 8
  192. Const MM_HIENGLISH& = 5
  193. Const MM_HIMETRIC& = 3
  194. Const MM_ISOTROPIC& = 7
  195. Const R2_COPYPEN& = 13
  196. Private Type OSVERSIONINFO
  197.         dwOSVersionInfoSize As Long
  198.         dwMajorVersion As Long
  199.         dwMinorVersion As Long
  200.         dwBuildNumber As Long
  201.         dwPlatformId As Long
  202.         szCSDVersion As String * 128
  203. End Type
  204. Const VER_PLATFORM_WIN32_NT& = 2
  205. Const VER_PLATFORM_WIN32_WINDOWS& = 1
  206. Private Declare Function GetVersionEx& Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO)
  207. #End If 'WIN32
  208. Private Sub cmdExit_Click()
  209.     Unload Me
  210. End Sub
  211. Private Sub cmdSettings_Click()
  212.     frmSettings.Show 1
  213. End Sub
  214. Private Sub Form_Load()
  215.     Dim myVer As OSVERSIONINFO
  216.     myVer.dwOSVersionInfoSize = 148
  217.     dummy& = GetVersionEx&(myVer) 'Get all the version info
  218.     If myVer.dwPlatformId = VER_PLATFORM_WIN32_NT Then
  219.         txtRotate.Visible = True
  220.         lblRotate.Visible = True
  221.     End If
  222. 'Set the default values for all the variables controlling the painting:
  223.     'Viewport
  224.     viewOrgX& = 200&
  225.     viewOrgY& = 75&
  226.     viewExtX& = 100&
  227.     viewExtY& = 100&
  228.     'Logical Window
  229.     WinOrgX& = 0
  230.     WinOrgY& = 0
  231.     WinExtX& = 100
  232.     WinExtY& = 100
  233.     'Rectangle & Ellipse Dimensions
  234.     RectX1& = 0
  235.     RectY1& = 0
  236.     RectX2& = 100
  237.     RectY2& = 100
  238. End Sub
  239. Private Sub pctScreen_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  240.     Dim clipR As RECT, clipP As POINTAPI
  241.     If Button And vbLeftButton Then
  242.         'The first corner of the rectangle is recorded in the viewX1
  243.         'and dviewY1 variables. The other corner is set to the same point.
  244.         dviewX1! = X
  245.         dviewY1! = Y
  246.         dviewX2! = dviewX1!
  247.         dviewY2! = dviewY1!
  248.         
  249. 'Clip the cursor to the picture control:
  250.         'Get the SCREEN coordinates of the form's origin
  251.         clipP.X = 0
  252.         clipP.Y = 0
  253.         dummy& = ClientToScreen(Me.hWnd, clipP)
  254.         
  255.         'Set the clip rectangle
  256.         clipR.Top = pctScreen.Top + clipP.Y ' Top of pctScreen + top of form
  257.         clipR.Left = pctScreen.Left + clipP.X ' left of pctScreen + left of form, etc.
  258.         clipR.Right = pctScreen.Left + pctScreen.Width + clipP.X
  259.         clipR.Bottom = pctScreen.Top + pctScreen.Height + clipP.Y
  260.         dummy& = ClipCursor&(clipR) ' Clip the cursor to the clipR rectangle
  261.     End If
  262. End Sub
  263. Private Sub pctScreen_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  264.     If Button And vbLeftButton Then ' If the button is down then
  265.         'Draw mode is XOR. We will draw a second box in the place of the
  266.         'previous one, erasing it.
  267.         pctScreen.Line (dviewX1!, dviewY1!)-(dviewX2!, dviewY2!), QBColor(10), B
  268.         ' Record where we are.
  269.         dviewX2! = X
  270.         dviewY2! = Y
  271.         
  272.         'Draw a dotted box to simulate a dragging rectangle
  273.         pctScreen.Line (dviewX1!, dviewY1!)-(dviewX2!, dviewY2!), QBColor(10), B
  274.     End If
  275. End Sub
  276. Private Sub pctScreen_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  277.     'Unclip the cursor
  278.     dummy& = ClipCursorBynum&(0&)
  279.     'If any of the box's dimensions are 0, then quit
  280.     If dviewX1 = dviewX2 Or dviewY1 = dviewY2 Then Exit Sub
  281.     'Erase the box from the screen by re-drawing it.
  282.     pctScreen.Line (dviewX1!, dviewY1!)-(dviewX2!, dviewY2!), QBColor(10), B
  283.     'Set the Viewport Variables to the box dimensions
  284.     viewOrgX& = CLng(dviewX1!)
  285.     viewOrgY& = CLng(dviewY1!)
  286.     viewExtX& = CLng(dviewX2!) - CLng(dviewX1!)
  287.     viewExtY& = CLng(dviewY2!) - CLng(dviewY1!)
  288.     'Redraw the screen.
  289.     pctScreen.Refresh
  290. End Sub
  291. Private Sub pctScreen_Paint()
  292.     Dim myXform As XFORM, PI As Double, color&
  293.     Dim cosvalue As Double, sinvalue As Double
  294.     ' Save the DC in order not to mess up VB drawing functions
  295.     savedDC& = SaveDC&(pctScreen.hdc)
  296.     'Make sure the drawing mode is MM_ANISOTROPIC ( no proportions)
  297.     dummy& = SetMapMode&(pctScreen.hdc, MM_ANISOTROPIC)
  298.     If txtRotate.Visible Then ' If we're in NT
  299.         ' Set the graphics mode to advanced (Enable transformations)
  300.         dummy& = SetGraphicsMode&(pctScreen.hdc, GM_ADVANCED)
  301.         If RotAngle% = 0 Then ' No rotate
  302.             dummy& = ModifyWorldTransform&(pctScreen.hdc, myXform, MWT_IDENTITY)
  303.         Else
  304.             PI = 3.14159265358979
  305.             ' No translation up or down
  306.             myXform.eDx = 0!
  307.             myXform.eDy = 0!
  308.             'Set the matrix to the correct values
  309.             cosvalue = Cos(CDbl(RotAngle%) * PI / CDbl(180))
  310.             sinvalue = Sin(CDbl(RotAngle%) * PI / CDbl(180))
  311.             myXform.eM11 = cosvalue
  312.             myXform.eM12 = sinvalue
  313.             myXform.eM21 = -sinvalue
  314.             myXform.eM22 = cosvalue
  315.             
  316.             ' Enable the transformation
  317.             dummy& = SetWorldTransform&(pctScreen.hdc, myXform)
  318.         End If
  319.     End If
  320.     'Set the Logical Window to the global variables
  321.     dummy& = SetWindowOrgEx&(pctScreen.hdc, WinOrgX&, WinOrgY&, myPoint)
  322.     dummy& = SetWindowExtEx&(pctScreen.hdc, WinExtX&, WinExtY&, mySize)
  323.     'Set the viewport
  324.     dummy& = SetViewportOrgEx&(pctScreen.hdc, viewOrgX&, viewOrgY&, myPoint)
  325.     dummy& = SetViewportExtEx&(pctScreen.hdc, viewExtX&, viewExtY&, mySize)
  326.     'Make sure drawing mode is NOT xor (see the other Ch. 7 example)
  327.     dummy& = SetROP2&(pctScreen.hdc, R2_COPYPEN)
  328.     'Make sure we don't draw dotted.
  329.     pctScreen.DrawStyle = 0 'Solid
  330.     'Draw the rectangle with dimensions specified in global variables
  331.     dummy& = Rectangle&(pctScreen.hdc, RectX1&, RectY1&, RectX2&, RectY2&)
  332.     dummy& = Ellipse&(pctScreen.hdc, RectX1&, RectY1&, RectX2&, RectY2&)
  333.     'Draw the Viewport Rectangle in a different color
  334.     color& = pctScreen.ForeColor
  335.     pctScreen.ForeColor = QBColor(11)
  336.     dummy& = Rectangle&(pctScreen.hdc, WinOrgX&, WinOrgY&, _
  337.             WinOrgX& + WinExtX&, WinOrgY& + WinExtY&)
  338.     pctScreen.ForeColor = color&
  339.     'Draw the Window Origin in a different color
  340.     color& = pctScreen.ForeColor
  341.     pctScreen.ForeColor = QBColor(1)
  342.     dummy& = MoveToEx&(pctScreen.hdc, -4&, -4&, myPoint)
  343.     dummy& = LineTo&(pctScreen.hdc, 4&, 4&)
  344.     dummy& = MoveToEx&(pctScreen.hdc, -4&, 4&, myPoint)
  345.     dummy& = LineTo&(pctScreen.hdc, 4&, -4&)
  346.     pctScreen.ForeColor = color&
  347.     'Restore all the changed settings.
  348.     pctScreen.DrawStyle = 2 'Dot
  349.     dummy& = RestoreDC(pctScreen.hdc, savedDC&)
  350. End Sub
  351. Private Sub txtRotate_Change()
  352.     If txtRotate = "" Then
  353.         txtRotate = "0"
  354.     End If
  355.     If CInt(txtRotate) > 360 Then
  356.         txtRotate = CStr(CInt(txtRotate Mod 360))
  357.     End If
  358.     RotAngle% = CInt(txtRotate)
  359.     pctScreen.Refresh
  360. End Sub
  361. Private Sub txtRotate_KeyPress(KeyAscii As Integer)
  362.     If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) And KeyAscii <> 8 Then
  363.         KeyAscii = 0
  364.     End If
  365. End Sub
  366. Private Sub txtRotate_LostFocus()
  367.     If txtRotate = "" Then
  368.         txtRotate = "0"
  369.     End If
  370.     If CInt(txtRotate) > 360 Then
  371.         txtRotate = CStr(CInt(txtRotate Mod 360))
  372.     End If
  373.     RotAngle% = CInt(txtRotate)
  374. End Sub
  375.