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

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