home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / directplay / chat / frmchat.frm (.txt) next >
Encoding:
Visual Basic Form  |  2000-10-11  |  10.6 KB  |  248 lines

  1. VERSION 5.00
  2. Begin VB.Form frmChat 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "vbDirectPlay Chat"
  5.    ClientHeight    =   5085
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   7695
  9.    Icon            =   "frmChat.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   5085
  14.    ScaleWidth      =   7695
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin VB.CommandButton cmdWhisper 
  17.       Caption         =   "Whisper"
  18.       Height          =   255
  19.       Left            =   5820
  20.       TabIndex        =   3
  21.       Top             =   4740
  22.       Width           =   1695
  23.    End
  24.    Begin VB.TextBox txtSend 
  25.       Height          =   285
  26.       Left            =   60
  27.       TabIndex        =   0
  28.       Top             =   4740
  29.       Width           =   5595
  30.    End
  31.    Begin VB.ListBox lstUsers 
  32.       Height          =   4545
  33.       Left            =   5760
  34.       TabIndex        =   2
  35.       Top             =   120
  36.       Width           =   1815
  37.    End
  38.    Begin VB.TextBox txtChat 
  39.       Height          =   4635
  40.       Left            =   60
  41.       MultiLine       =   -1  'True
  42.       ScrollBars      =   3  'Both
  43.       TabIndex        =   1
  44.       TabStop         =   0   'False
  45.       Top             =   60
  46.       Width           =   5595
  47.    End
  48. Attribute VB_Name = "frmChat"
  49. Attribute VB_GlobalNameSpace = False
  50. Attribute VB_Creatable = False
  51. Attribute VB_PredeclaredId = True
  52. Attribute VB_Exposed = False
  53. Option Explicit
  54. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  55. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  56. '  File:       frmChat.frm
  57. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  58. Implements DirectPlay8Event
  59. Private Sub cmdWhisper_Click()
  60.     Dim lMsg As Long, lOffset As Long
  61.     Dim sChatMsg As String
  62.     Dim oBuf() As Byte
  63.     If lstUsers.ListIndex < 0 Then
  64.         MsgBox "You must select a user in the list before you can whisper to that person.", vbOKOnly Or vbInformation, "Select someone"
  65.         Exit Sub
  66.     End If
  67.     If lstUsers.ItemData(lstUsers.ListIndex) = 0 Then
  68.         MsgBox "Why are you whispering to yourself?", vbOKOnly Or vbInformation, "Select someone else"
  69.         Exit Sub
  70.     End If
  71.     If txtSend.Text = vbNullString Then
  72.         MsgBox "What's the point of whispering if you have nothing to say..", vbOKOnly Or vbInformation, "Enter text"
  73.         Exit Sub
  74.     End If
  75.         
  76.     'Send this message to the person you are whispering to
  77.     lMsg = MsgWhisper
  78.     lOffset = NewBuffer(oBuf)
  79.     AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  80.     sChatMsg = txtSend.Text
  81.     AddStringToBuffer oBuf, sChatMsg, lOffset
  82.     txtSend.Text = vbNullString
  83.     dpp.SendTo lstUsers.ItemData(lstUsers.ListIndex), oBuf, 0, DPNSEND_NOLOOPBACK
  84.     UpdateChat "**<" & gsUserName & ">** " & sChatMsg
  85. End Sub
  86. Private Sub Form_Load()
  87.     'Oh good, we want to play a multiplayer game.
  88.     'First lets get the dplay connection started
  89.     'Here we will init our DPlay objects
  90.     InitDPlay
  91.     'Now we can create a new Connection Form (which will also be our message pump)
  92.     Set DPlayEventsForm = New DPlayConnect
  93.     'Start the connection form (it will either create or join a session)
  94.     If Not DPlayEventsForm.StartConnectWizard(dx, dpp, AppGuid, 20, Me) Then
  95.         Cleanup
  96.         End
  97.     Else 'We did choose to play a game
  98.         gsUserName = DPlayEventsForm.UserName
  99.         If DPlayEventsForm.IsHost Then
  100.             Me.Caption = Me.Caption & " (HOST)"
  101.         End If
  102.     End If
  103. End Sub
  104. Private Sub Form_Unload(Cancel As Integer)
  105.     Me.Hide
  106.     DPlayEventsForm.DoSleep 50
  107.     Cleanup
  108. End Sub
  109. Private Sub UpdateChat(ByVal sString As String)
  110.     'Update the chat window first
  111.     txtChat.Text = txtChat.Text & sString & vbCrLf
  112.     'Now limit the text in the window to be 16k
  113.     If Len(txtChat.Text) > 16384 Then
  114.         txtChat.Text = Right$(txtChat.Text, 16384)
  115.     End If
  116.     'Autoscroll the text
  117.     txtChat.SelStart = Len(txtChat.Text)
  118. End Sub
  119. Private Sub txtSend_KeyPress(KeyAscii As Integer)
  120.     Dim lMsg As Long, lOffset As Long
  121.     Dim sChatMsg As String
  122.     Dim oBuf() As Byte
  123.     If KeyAscii = vbKeyReturn Then
  124.         If txtSend.Text <> vbNullString Then 'Make sure they are trying to send something
  125.             'Send this message to everyone
  126.             lMsg = MsgChat
  127.             lOffset = NewBuffer(oBuf)
  128.             AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  129.             sChatMsg = txtSend.Text
  130.             AddStringToBuffer oBuf, sChatMsg, lOffset
  131.             txtSend.Text = vbNullString
  132.             KeyAscii = 0
  133.             dpp.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK
  134.             UpdateChat "<" & gsUserName & ">" & sChatMsg
  135.         End If 'We won't set KeyAscii to 0 here, because if they are trying to
  136.                'send blank data, we don't care about the ding for hitting enter on
  137.                'an empty line
  138.     End If
  139. End Sub
  140. Private Function GetName(ByVal lID As Long) As String
  141.     Dim lCount As Long
  142.     GetName = vbNullString
  143.     For lCount = 0 To lstUsers.ListCount - 1
  144.         If lstUsers.ItemData(lCount) = lID Then 'This is the player
  145.             GetName = lstUsers.List(lCount)
  146.             Exit For
  147.         End If
  148.     Next
  149. End Function
  150. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  151.     'VB requires that we must implement *every* member of this interface
  152. End Sub
  153. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  154.     'VB requires that we must implement *every* member of this interface
  155. End Sub
  156. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  157.     'VB requires that we must implement *every* member of this interface
  158. End Sub
  159. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  160.     If dpnotify.hResultCode <> 0 Then
  161.         'For some reason we could not connect.  All available slots must be closed.
  162.         MsgBox "Connect Failed.  Error: 0x" & CStr(Hex$(dpnotify.hResultCode)) & "  - This sample will now close.", vbOKOnly Or vbCritical, "Closing"
  163.         DPlayEventsForm.CloseForm Me
  164.     End If
  165. End Sub
  166. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  167.     'VB requires that we must implement *every* member of this interface
  168. End Sub
  169. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  170.     Dim dpPeer As DPN_PLAYER_INFO
  171.     dpPeer = dpp.GetPeerInfo(lPlayerID)
  172.         
  173.     'Add this person to chat (even if it's me)
  174.     lstUsers.AddItem dpPeer.Name
  175.     If (dpPeer.lPlayerFlags And DPNPLAYER_LOCAL) <> DPNPLAYER_LOCAL Then 'this isn't me, someone just joined
  176.         UpdateChat "- " & dpPeer.Name & " is chatting"
  177.         'If it's not me, include an ItemData
  178.         lstUsers.ItemData(lstUsers.ListCount - 1) = lPlayerID
  179.     End If
  180. End Sub
  181. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  182.     'VB requires that we must implement *every* member of this interface
  183. End Sub
  184. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  185.     Dim lCount As Long
  186.     'We only care when someone leaves.  When they join we will receive a 'MSGJoin'
  187.     'Remove this player from our list
  188.     For lCount = 0 To lstUsers.ListCount - 1
  189.         If lstUsers.ItemData(lCount) = lPlayerID Then 'This is the player
  190.             UpdateChat "-- " & lstUsers.List(lCount) & " is no longer chatting."
  191.             lstUsers.RemoveItem lCount
  192.             Exit For
  193.         End If
  194.     Next
  195. End Sub
  196. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  197.     'VB requires that we must implement *every* member of this interface
  198. End Sub
  199. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  200.     'VB requires that we must implement *every* member of this interface
  201. End Sub
  202. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  203.     Dim dpPeer As DPN_PLAYER_INFO
  204.     dpPeer = dpp.GetPeerInfo(lNewHostID)
  205.     If (dpPeer.lPlayerFlags And DPNPLAYER_LOCAL) = DPNPLAYER_LOCAL Then 'I am the new host
  206.         Me.Caption = Me.Caption & " (HOST)"
  207.     End If
  208. End Sub
  209. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  210.     'VB requires that we must implement *every* member of this interface
  211. End Sub
  212. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  213.     'VB requires that we must implement *every* member of this interface
  214. End Sub
  215. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  216.     'VB requires that we must implement *every* member of this interface
  217. End Sub
  218. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  219.     'process what msgs we receive.
  220.     Dim lMsg As Long, lOffset As Long
  221.     Dim dpPeer As DPN_PLAYER_INFO, sName As String
  222.     Dim sChat As String
  223.     With dpnotify
  224.     GetDataFromBuffer .ReceivedData, lMsg, LenB(lMsg), lOffset
  225.     Select Case lMsg
  226.     Case MsgChat
  227.         sName = GetName(.idSender)
  228.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  229.         UpdateChat "<" & sName & "> " & sChat
  230.     Case MsgWhisper
  231.         sName = GetName(.idSender)
  232.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  233.         UpdateChat "**<" & sName & ">** " & sChat
  234.     End Select
  235.     End With
  236. End Sub
  237. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  238.     'VB requires that we must implement *every* member of this interface
  239. End Sub
  240. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  241.     If dpnotify.hResultCode = DPNERR_HOSTTERMINATEDSESSION Then
  242.         MsgBox "The host has terminated this session.  This sample will now exit.", vbOKOnly Or vbInformation, "Exiting"
  243.     Else
  244.         MsgBox "This session has been lost.  This sample will now exit.", vbOKOnly Or vbInformation, "Exiting"
  245.     End If
  246.     DPlayEventsForm.CloseForm Me
  247. End Sub
  248.