home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectInput / Scrawl / frmcanvas.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  9.2 KB  |  266 lines

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