home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / directinput / scrawl / frmcanvas.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2000-10-02  |  8.7 KB  |  250 lines

  1. VERSION 5.00
  2. Begin VB.Form frmCanvas 
  3.    AutoRedraw      =   -1  'True
  4.    BackColor       =   &H80000005&
  5.    Caption         =   "Visual Basic Scrawl Sample"
  6.    ClientHeight    =   6150
  7.    ClientLeft      =   165
  8.    ClientTop       =   450
  9.    ClientWidth     =   9990
  10.    Icon            =   "frmCanvas.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   410
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   666
  15.    StartUpPosition =   2  'CenterScreen
  16.    Begin VB.Image imgPencil 
  17.       Appearance      =   0  'Flat
  18.       Height          =   480
  19.       Left            =   0
  20.       Picture         =   "frmCanvas.frx":0442
  21.       Top             =   0
  22.       Width           =   480
  23.    End
  24.    Begin VB.Menu mnuContext 
  25.       Caption         =   "none"
  26.       Visible         =   0   'False
  27.       Begin VB.Menu mnuAbout 
  28.          Caption         =   "About..."
  29.       End
  30.       Begin VB.Menu Sep1 
  31.          Caption         =   "-"
  32.       End
  33.       Begin VB.Menu mnuSpeed1 
  34.          Caption         =   "Speed 1"
  35.       End
  36.       Begin VB.Menu mnuSpeed2 
  37.          Caption         =   "Speed 2"
  38.       End
  39.       Begin VB.Menu mnuSpeed3 
  40.          Caption         =   "Speed 3"
  41.       End
  42.       Begin VB.Menu Sep2 
  43.          Caption         =   "-"
  44.       End
  45.       Begin VB.Menu mnuClear 
  46.          Caption         =   "Clear"
  47.       End
  48.       Begin VB.Menu Sep3 
  49.          Caption         =   "-"
  50.       End
  51.       Begin VB.Menu mnuSuspend 
  52.          Caption         =   "Release Mouse"
  53.       End
  54.    End
  55. Attribute VB_Name = "frmCanvas"
  56. Attribute VB_GlobalNameSpace = False
  57. Attribute VB_Creatable = False
  58. Attribute VB_PredeclaredId = True
  59. Attribute VB_Exposed = False
  60. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  61. '  Copyright (C) 1998-2000 Microsoft Corporation.  All Rights Reserved.
  62. '  File:       frmCanvas.frm
  63. '  Content:    This Form holds the DirectInput callback for mouse messages
  64. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  65. Option Explicit
  66. Implements DirectXEvent8
  67. Private Sub DirectXEvent8_DXCallback(ByVal eventid As Long)
  68. ' This is where we respond to any change in mouse state. Usually this will be an axis movement
  69. ' or button press or release, but it could also mean we've lost acquisition.
  70. ' Note: no event is signalled if we voluntarily Unacquire. Normally loss of acquisition will
  71. ' mean that the app window has lost the focus.
  72.   Dim diDeviceData(1 To BufferSize) As DIDEVICEOBJECTDATA
  73.   Dim NumItems As Integer
  74.   Dim i As Integer
  75.   Static OldSequence As Long
  76.   ' Get data
  77.   On Error GoTo INPUTLOST
  78.   NumItems = objDIDev.getDeviceData(diDeviceData, 0)
  79.   On Error GoTo 0
  80.   ' Process data
  81.   For i = 1 To NumItems
  82.     Select Case diDeviceData(i).lOfs
  83.       Case DIMOFS_X
  84.         g_cursorx = g_cursorx + diDeviceData(i).lData * g_Sensitivity
  85.            
  86.         ' We don't want to update the cursor or draw a line is response to
  87.         ' separate axis movements, or we will get a staircase instead of diagonal lines.
  88.         ' A diagonal movement of the mouse results in two events with the same sequence number.
  89.         ' In order to avoid postponing the last event till the mouse moves again, we always
  90.         ' reset OldSequence after it's been tested once.
  91.           
  92.         If OldSequence <> diDeviceData(i).lSequence Then
  93.           UpdateCursor
  94.           OldSequence = diDeviceData(i).lSequence
  95.         Else
  96.           OldSequence = 0
  97.         End If
  98.          
  99.       Case DIMOFS_Y
  100.         g_cursory = g_cursory + diDeviceData(i).lData * g_Sensitivity
  101.         If OldSequence <> diDeviceData(i).lSequence Then
  102.           UpdateCursor
  103.           OldSequence = diDeviceData(i).lSequence
  104.         Else
  105.           OldSequence = 0
  106.         End If
  107.         
  108.       Case DIMOFS_BUTTON0
  109.         If diDeviceData(i).lData And &H80 Then
  110.           Drawing = True
  111.            
  112.           ' Keep record for Line function
  113.           CurrentX = g_cursorx
  114.           CurrentY = g_cursory
  115.            
  116.           ' Draw a point in case button-up follows immediately
  117.           PSet (g_cursorx, g_cursory)
  118.         Else
  119.           Drawing = False
  120.         End If
  121.            
  122.       Case DIMOFS_BUTTON1
  123.         If diDeviceData(i).lData = 0 Then  ' button up
  124.           Popup
  125.         End If
  126.         
  127.     End Select
  128.   Next i
  129.   Exit Sub
  130. INPUTLOST:
  131.   ' Windows stole the mouse from us. DIERR_INPUTLOST is raised if the user switched to
  132.   ' another app, but DIERR_NOTACQUIRED is raised if the Windows key was pressed.
  133.   If (Err.Number = DIERR_INPUTLOST) Or (Err.Number = DIERR_NOTACQUIRED) Then
  134.     SetSystemCursor
  135.     Exit Sub
  136.   End If
  137. End Sub
  138. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  139.   Select Case KeyCode
  140.     Case 93         ' AppMenu key
  141.       Popup
  142.       
  143.     End Select
  144. End Sub
  145. Private Sub Form_Unload(Cancel As Integer)
  146.   ' Restore the default window procedure
  147.   If procOld <> 0 Then
  148.     Call SetWindowLong(hWnd, GWL_WNDPROC, procOld)
  149.   End If
  150.   If EventHandle <> 0 Then objDX.destroyEvent EventHandle
  151. End Sub
  152. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  153.   Dim didevstate As DIMOUSESTATE
  154.   ' We want to force acquisition of the mouse whenever the context menu is closed,
  155.   ' whenever we switch back to the application, or in any other circumstance where
  156.   ' Windows is finished with the cursor. If a MouseMove event happens,
  157.   ' we know the cursor is in our app window and Windows is generating mouse messages, therefore
  158.   ' it's time to reacquire.
  159.   ' Note: this event is triggered whenever the window gets the mouse, even when there's no mouse
  160.   ' activity -- for example, when we have just Alt+Tabbed back, or cancelled out of the context
  161.   ' menu with the Esc key.
  162.    If Suspended Then Exit Sub    ' Allow continued use of Windows cursor
  163.   ' This event gets called again after we acquire the mouse. In order to prevent the cursor
  164.   ' position being set to the middle of the window, we check to see if we've already acquired,
  165.   ' and if so, we don't reposition our private cursor. The only way to find out if the mouse
  166.   ' is acquired is to try to retrieve data.
  167.   On Error GoTo NOTYETACQUIRED
  168.   Call objDIDev.getDeviceStateMouse(didevstate)
  169.   On Error GoTo 0
  170.   Exit Sub
  171. NOTYETACQUIRED:
  172.   Call AcquireMouse
  173. End Sub
  174. Sub AcquireMouse()
  175.   Dim CursorPoint As POINTAPI
  176.   ' Move private cursor to system cursor.
  177.   Call GetCursorPos(CursorPoint)  ' Get position before Windows loses cursor
  178.   Call ScreenToClient(hWnd, CursorPoint)
  179.   On Error GoTo CANNOTACQUIRE
  180.   objDIDev.acquire
  181.   g_cursorx = CursorPoint.x
  182.   g_cursory = CursorPoint.y
  183.   UpdateCursor
  184.   frmCanvas.imgPencil.Visible = True
  185.   On Error GoTo 0
  186.   Exit Sub
  187. CANNOTACQUIRE:
  188.   Exit Sub
  189. End Sub
  190. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  191.   ' Allows user to resume by clicking on the canvas.
  192.   If Button = 1 Then Suspended = False
  193. End Sub
  194. Private Sub mnuAbout_Click()
  195.   Call frmAbout.Show(vbModal, Me)
  196. End Sub
  197. Private Sub mnuClear_Click()
  198.    Cls
  199. End Sub
  200. Private Sub mnuSpeed1_Click()
  201.   g_Sensitivity = 1
  202.   mnuSpeed1.Checked = True
  203.   mnuSpeed2.Checked = False
  204.   mnuSpeed3.Checked = False
  205. End Sub
  206. Private Sub mnuSpeed2_Click()
  207.   g_Sensitivity = 2
  208.   mnuSpeed2.Checked = True
  209.   mnuSpeed1.Checked = False
  210.   mnuSpeed3.Checked = False
  211. End Sub
  212. Private Sub mnuSpeed3_Click()
  213.   g_Sensitivity = 3
  214.   mnuSpeed3.Checked = True
  215.   mnuSpeed1.Checked = False
  216.   mnuSpeed2.Checked = False
  217. End Sub
  218. Private Sub mnuSuspend_Click()
  219.   Suspended = Not Suspended
  220.   imgPencil.Visible = Not Suspended
  221. End Sub
  222. Public Sub UpdateCursor()
  223.   ' Update the position of our private cursor
  224.   If g_cursorx < 0 Then g_cursorx = 0
  225.   If g_cursorx >= frmCanvas.ScaleWidth Then g_cursorx = frmCanvas.ScaleWidth - 1
  226.   If g_cursory < 0 Then g_cursory = 0
  227.   If g_cursory >= frmCanvas.ScaleHeight Then g_cursory = frmCanvas.ScaleHeight - 1
  228.   frmCanvas.imgPencil.Left = g_cursorx
  229.   frmCanvas.imgPencil.Top = g_cursory
  230.   If Drawing Then
  231.     Line -(g_cursorx, g_cursory)
  232.   End If
  233. End Sub
  234. Public Sub Popup()
  235.   objDIDev.unacquire
  236.   SetSystemCursor
  237.   Call PopupMenu(mnuContext)
  238. End Sub
  239. Public Sub SetSystemCursor()
  240.  ' Get the system cursor into the same position as the private cursor,
  241.  ' and stop drawing
  242.   Dim point As POINTAPI
  243.   imgPencil.Visible = False
  244.   Drawing = False
  245.   point.x = g_cursorx
  246.   point.y = g_cursory
  247.   Call ClientToScreen(hWnd, point)
  248.   Call SetCursorPos(point.x, point.y)
  249. End Sub
  250.