home *** CD-ROM | disk | FTP | other *** search
/ Microsoft DirectX SDK 7.0 / Dx7.bin / DXF / samples / multimedia / vbsamples / dinput / src / scrawlb / frmcanvas.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-06-18  |  8.2 KB  |  245 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. Option Explicit
  61. Implements DirectXEvent
  62. Private Sub DirectXEvent_DXCallback(ByVal eventid As Long)
  63. ' This is where we respond to any change in mouse state. Usually this will be an axis movement
  64. ' or button press or release, but it could also mean we've lost acquisition.
  65. ' Note: no event is signalled if we voluntarily Unacquire. Normally loss of acquisition will
  66. ' mean that the app window has lost the focus.
  67.   Dim diDeviceData(1 To BufferSize) As DIDEVICEOBJECTDATA
  68.   Dim NumItems As Integer
  69.   Dim i As Integer
  70.   Static OldSequence As Long
  71.   ' Get data
  72.   On Error GoTo INPUTLOST
  73.   NumItems = objDIDev.GetDeviceData(diDeviceData, 0)
  74.   On Error GoTo 0
  75.   ' Process data
  76.   For i = 1 To NumItems
  77.     Select Case diDeviceData(i).lOfs
  78.       Case DIMOFS_X
  79.         g_cursorx = g_cursorx + diDeviceData(i).lData * g_Sensitivity
  80.            
  81.         ' We don't want to update the cursor or draw a line is response to
  82.         ' separate axis movements, or we will get a staircase instead of diagonal lines.
  83.         ' A diagonal movement of the mouse results in two events with the same sequence number.
  84.         ' In order to avoid postponing the last event till the mouse moves again, we always
  85.         ' reset OldSequence after it's been tested once.
  86.           
  87.         If OldSequence <> diDeviceData(i).lSequence Then
  88.           UpdateCursor
  89.           OldSequence = diDeviceData(i).lSequence
  90.         Else
  91.           OldSequence = 0
  92.         End If
  93.          
  94.       Case DIMOFS_Y
  95.         g_cursory = g_cursory + diDeviceData(i).lData * g_Sensitivity
  96.         If OldSequence <> diDeviceData(i).lSequence Then
  97.           UpdateCursor
  98.           OldSequence = diDeviceData(i).lSequence
  99.         Else
  100.           OldSequence = 0
  101.         End If
  102.         
  103.       Case DIMOFS_BUTTON0
  104.         If diDeviceData(i).lData And &H80 Then
  105.           Drawing = True
  106.            
  107.           ' Keep record for Line function
  108.           CurrentX = g_cursorx
  109.           CurrentY = g_cursory
  110.            
  111.           ' Draw a point in case button-up follows immediately
  112.           PSet (g_cursorx, g_cursory)
  113.         Else
  114.           Drawing = False
  115.         End If
  116.            
  117.       Case DIMOFS_BUTTON1
  118.         If diDeviceData(i).lData = 0 Then  ' button up
  119.           Popup
  120.         End If
  121.         
  122.     End Select
  123.   Next i
  124.   Exit Sub
  125. INPUTLOST:
  126.   ' Windows stole the mouse from us. DIERR_INPUTLOST is raised if the user switched to
  127.   ' another app, but DIERR_NOTACQUIRED is raised if the Windows key was pressed.
  128.   If (Err.Number = DIERR_INPUTLOST) Or (Err.Number = DIERR_NOTACQUIRED) Then
  129.     SetSystemCursor
  130.     Exit Sub
  131.   End If
  132. End Sub
  133. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  134.   Select Case KeyCode
  135.     Case 93         ' AppMenu key
  136.       Popup
  137.       
  138.     End Select
  139. End Sub
  140. Private Sub Form_Unload(Cancel As Integer)
  141.   ' Restore the default window procedure
  142.   If procOld <> 0 Then
  143.     Call SetWindowLong(hWnd, GWL_WNDPROC, procOld)
  144.   End If
  145.   If EventHandle <> 0 Then objDX.DestroyEvent EventHandle
  146. End Sub
  147. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  148.   Dim didevstate As DIMOUSESTATE
  149.   ' We want to force acquisition of the mouse whenever the context menu is closed,
  150.   ' whenever we switch back to the application, or in any other circumstance where
  151.   ' Windows is finished with the cursor. If a MouseMove event happens,
  152.   ' we know the cursor is in our app window and Windows is generating mouse messages, therefore
  153.   ' it's time to reacquire.
  154.   ' Note: this event is triggered whenever the window gets the mouse, even when there's no mouse
  155.   ' activity -- for example, when we have just Alt+Tabbed back, or cancelled out of the context
  156.   ' menu with the Esc key.
  157.    If Suspended Then Exit Sub    ' Allow continued use of Windows cursor
  158.   ' This event gets called again after we acquire the mouse. In order to prevent the cursor
  159.   ' position being set to the middle of the window, we check to see if we've already acquired,
  160.   ' and if so, we don't reposition our private cursor. The only way to find out if the mouse
  161.   ' is acquired is to try to retrieve data.
  162.   On Error GoTo NOTYETACQUIRED
  163.   Call objDIDev.GetDeviceStateMouse(didevstate)
  164.   On Error GoTo 0
  165.   Exit Sub
  166. NOTYETACQUIRED:
  167.   Call AcquireMouse
  168. End Sub
  169. Sub AcquireMouse()
  170.   Dim CursorPoint As POINTAPI
  171.   ' Move private cursor to system cursor.
  172.   Call GetCursorPos(CursorPoint)  ' Get position before Windows loses cursor
  173.   Call ScreenToClient(hWnd, CursorPoint)
  174.   On Error GoTo CANNOTACQUIRE
  175.   objDIDev.Acquire
  176.   g_cursorx = CursorPoint.x
  177.   g_cursory = CursorPoint.y
  178.   UpdateCursor
  179.   frmCanvas.imgPencil.Visible = True
  180.   On Error GoTo 0
  181.   Exit Sub
  182. CANNOTACQUIRE:
  183.   Exit Sub
  184. End Sub
  185. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  186.   ' Allows user to resume by clicking on the canvas.
  187.   If Button = 1 Then Suspended = False
  188. End Sub
  189. Private Sub mnuAbout_Click()
  190.   Call frmAbout.Show(vbModal, Me)
  191. End Sub
  192. Private Sub mnuClear_Click()
  193.    Cls
  194. End Sub
  195. Private Sub mnuSpeed1_Click()
  196.   g_Sensitivity = 1
  197.   mnuSpeed1.Checked = True
  198.   mnuSpeed2.Checked = False
  199.   mnuSpeed3.Checked = False
  200. End Sub
  201. Private Sub mnuSpeed2_Click()
  202.   g_Sensitivity = 2
  203.   mnuSpeed2.Checked = True
  204.   mnuSpeed1.Checked = False
  205.   mnuSpeed3.Checked = False
  206. End Sub
  207. Private Sub mnuSpeed3_Click()
  208.   g_Sensitivity = 3
  209.   mnuSpeed3.Checked = True
  210.   mnuSpeed1.Checked = False
  211.   mnuSpeed2.Checked = False
  212. End Sub
  213. Private Sub mnuSuspend_Click()
  214.   Suspended = Not Suspended
  215.   imgPencil.Visible = Not Suspended
  216. End Sub
  217. Public Sub UpdateCursor()
  218.   ' Update the position of our private cursor
  219.   If g_cursorx < 0 Then g_cursorx = 0
  220.   If g_cursorx >= frmCanvas.ScaleWidth Then g_cursorx = frmCanvas.ScaleWidth - 1
  221.   If g_cursory < 0 Then g_cursory = 0
  222.   If g_cursory >= frmCanvas.ScaleHeight Then g_cursory = frmCanvas.ScaleHeight - 1
  223.   frmCanvas.imgPencil.Left = g_cursorx
  224.   frmCanvas.imgPencil.Top = g_cursory
  225.   If Drawing Then
  226.     Line -(g_cursorx, g_cursory)
  227.   End If
  228. End Sub
  229. Public Sub Popup()
  230.   objDIDev.Unacquire
  231.   SetSystemCursor
  232.   Call PopupMenu(mnuContext)
  233. End Sub
  234. Public Sub SetSystemCursor()
  235.  ' Get the system cursor into the same position as the private cursor,
  236.  ' and stop drawing
  237.   Dim point As POINTAPI
  238.   imgPencil.Visible = False
  239.   Drawing = False
  240.   point.x = g_cursorx
  241.   point.y = g_cursory
  242.   Call ClientToScreen(hWnd, point)
  243.   Call SetCursorPos(point.x, point.y)
  244. End Sub
  245.