home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectPlay / Conferencer / frmChat.frm (.txt) next >
Encoding:
Visual Basic Form  |  2001-10-08  |  9.4 KB  |  225 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     =   7710
  9.    Icon            =   "frmChat.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   5085
  14.    ScaleWidth      =   7710
  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.Timer tmrUpdate 
  25.       Enabled         =   0   'False
  26.       Interval        =   50
  27.       Left            =   10200
  28.       Top             =   120
  29.    End
  30.    Begin VB.TextBox txtSend 
  31.       Height          =   285
  32.       Left            =   60
  33.       TabIndex        =   0
  34.       Top             =   4740
  35.       Width           =   5655
  36.    End
  37.    Begin VB.ListBox lstUsers 
  38.       Height          =   4545
  39.       Left            =   5760
  40.       TabIndex        =   2
  41.       Top             =   120
  42.       Width           =   1815
  43.    End
  44.    Begin VB.TextBox txtChat 
  45.       Height          =   4635
  46.       Left            =   60
  47.       Locked          =   -1  'True
  48.       MultiLine       =   -1  'True
  49.       ScrollBars      =   2  'Vertical
  50.       TabIndex        =   1
  51.       TabStop         =   0   'False
  52.       Top             =   60
  53.       Width           =   5595
  54.    End
  55. Attribute VB_Name = "frmChat"
  56. Attribute VB_GlobalNameSpace = False
  57. Attribute VB_Creatable = False
  58. Attribute VB_PredeclaredId = True
  59. Attribute VB_Exposed = False
  60. Option Explicit
  61. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  62. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  63. '  File:       frmChat.frm
  64. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  65. Implements DirectPlay8Event
  66. Private Sub cmdWhisper_Click()
  67.     Dim lMsg As Long, lOffset As Long
  68.     Dim sChatMsg As String
  69.     Dim oBuf() As Byte
  70.     If lstUsers.ListIndex < 0 Then
  71.         MsgBox "You must select a user in the list before you can whisper to that person.", vbOKOnly Or vbInformation, "Select someone"
  72.         Exit Sub
  73.     End If
  74.     If lstUsers.ItemData(lstUsers.ListIndex) = 0 Then
  75.         MsgBox "Why are you whispering to yourself?", vbOKOnly Or vbInformation, "Select someone else"
  76.         Exit Sub
  77.     End If
  78.     If txtSend.Text = vbNullString Then
  79.         MsgBox "What's the point of whispering if you have nothing to say..", vbOKOnly Or vbInformation, "Enter text"
  80.         Exit Sub
  81.     End If
  82.         
  83.     'Send this message to the person you are whispering to
  84.     lMsg = MsgWhisper
  85.     lOffset = NewBuffer(oBuf)
  86.     AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  87.     sChatMsg = txtSend.Text
  88.     AddStringToBuffer oBuf, sChatMsg, lOffset
  89.     txtSend.Text = vbNullString
  90.     dpp.SendTo lstUsers.ItemData(lstUsers.ListIndex), oBuf, 0, DPNSEND_NOLOOPBACK
  91.     UpdateChat "**<" & gsUserName & ">** " & sChatMsg
  92. End Sub
  93. Private Sub Form_Load()
  94.     'load all of the players into our list
  95.     LoadAllPlayers
  96. End Sub
  97. Private Sub UpdateChat(ByVal sString As String)
  98.     'Update the chat window first
  99.     txtChat.Text = txtChat.Text & sString & vbCrLf
  100.     'Now limit the text in the window to be 16k
  101.     If Len(txtChat.Text) > 16384 Then
  102.         txtChat.Text = Right$(txtChat.Text, 16384)
  103.     End If
  104.     'Autoscroll the text
  105.     txtChat.SelStart = Len(txtChat.Text)
  106. End Sub
  107. Private Sub txtSend_KeyPress(KeyAscii As Integer)
  108.     Dim lMsg As Long, lOffset As Long
  109.     Dim sChatMsg As String
  110.     Dim oBuf() As Byte
  111.     If KeyAscii = vbKeyReturn Then
  112.         KeyAscii = 0
  113.         If txtSend.Text = vbNullString Then Exit Sub
  114.         'Send this message to everyone
  115.         lMsg = MsgChat
  116.         lOffset = NewBuffer(oBuf)
  117.         AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  118.         sChatMsg = txtSend.Text
  119.         AddStringToBuffer oBuf, sChatMsg, lOffset
  120.         txtSend.Text = vbNullString
  121.         dpp.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK
  122.         UpdateChat "<" & gsUserName & ">" & sChatMsg
  123.     End If
  124. End Sub
  125. Private Function GetName(ByVal lID As Long) As String
  126.     Dim lCount As Long
  127.     GetName = vbNullString
  128.     For lCount = 0 To lstUsers.ListCount - 1
  129.         If lstUsers.ItemData(lCount) = lID Then 'This is the player
  130.             GetName = lstUsers.List(lCount)
  131.             Exit For
  132.         End If
  133.     Next
  134. End Function
  135. Public Sub LoadAllPlayers()
  136.     Dim lCount As Long
  137.     Dim dpPlayer As DPN_PLAYER_INFO
  138.     lstUsers.Clear
  139.     For lCount = 1 To dpp.GetCountPlayersAndGroups(DPNENUM_PLAYERS)
  140.         dpPlayer = dpp.GetPeerInfo(dpp.GetPlayerOrGroup(lCount))
  141.         lstUsers.AddItem dpPlayer.Name
  142.         If ((dpPlayer.lPlayerFlags And DPNPLAYER_LOCAL) <> DPNPLAYER_LOCAL) Then
  143.             'Do not add a ItemData key for myself
  144.             lstUsers.ItemData(lstUsers.ListCount - 1) = dpp.GetPlayerOrGroup(lCount)
  145.         End If
  146.     Next
  147. End Sub
  148. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  149.     'VB requires that we must implement *every* member of this interface
  150. End Sub
  151. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  152.     'VB requires that we must implement *every* member of this interface
  153. End Sub
  154. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  155.     'VB requires that we must implement *every* member of this interface
  156. End Sub
  157. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  158.     'VB requires that we must implement *every* member of this interface
  159. End Sub
  160. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  161.     'VB requires that we must implement *every* member of this interface
  162. End Sub
  163. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  164.     'VB requires that we must implement *every* member of this interface
  165. End Sub
  166. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  167.     'VB requires that we must implement *every* member of this interface
  168. End Sub
  169. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  170.     Dim lCount As Long
  171.     'We only care when someone leaves.  When they join we will receive a 'MSGJoin'
  172.     'Remove this player from our list
  173.     For lCount = 0 To lstUsers.ListCount - 1
  174.         If lstUsers.ItemData(lCount) = lPlayerID Then 'This is the player
  175.             UpdateChat "---- " & lstUsers.List(lCount) & " has left the chat."
  176.             lstUsers.RemoveItem lCount
  177.             Exit For
  178.         End If
  179.     Next
  180. End Sub
  181. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  182.     'VB requires that we must implement *every* member of this interface
  183. End Sub
  184. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  185.     'VB requires that we must implement *every* member of this interface
  186. End Sub
  187. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  188.     'VB requires that we must implement *every* member of this interface
  189. End Sub
  190. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  191.     'VB requires that we must implement *every* member of this interface
  192. End Sub
  193. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  194.     'VB requires that we must implement *every* member of this interface
  195. End Sub
  196. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  197.     'VB requires that we must implement *every* member of this interface
  198. End Sub
  199. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  200.     'process what msgs we receive.
  201.     'All we care about in this form is what msgs we receive.
  202.     Dim lMsg As Long, lOffset As Long
  203.     Dim dpPeer As DPN_PLAYER_INFO, sName As String
  204.     Dim sChat As String
  205.     With dpnotify
  206.     GetDataFromBuffer .ReceivedData, lMsg, LenB(lMsg), lOffset
  207.     Select Case lMsg
  208.     Case MsgChat
  209.         sName = GetName(.idSender)
  210.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  211.         UpdateChat "<" & sName & "> " & sChat
  212.     Case MsgWhisper
  213.         sName = GetName(.idSender)
  214.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  215.         UpdateChat "**<" & sName & ">** " & sChat
  216.     End Select
  217.     End With
  218. End Sub
  219. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  220.     'VB requires that we must implement *every* member of this interface
  221. End Sub
  222. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  223.     'VB requires that we must implement *every* member of this interface
  224. End Sub
  225.