home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / directplay / dxvbmessenger / client / frmclient.frm (.txt) next >
Encoding:
Visual Basic Form  |  2000-10-02  |  19.2 KB  |  481 lines

  1. VERSION 5.00
  2. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  3. Begin VB.Form frmClient 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "vbMessenger Service (Not logged in)"
  6.    ClientHeight    =   4740
  7.    ClientLeft      =   150
  8.    ClientTop       =   720
  9.    ClientWidth     =   4185
  10.    Icon            =   "frmClient.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   4740
  15.    ScaleWidth      =   4185
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin VB.Timer tmrExit 
  18.       Interval        =   50
  19.       Left            =   8100
  20.       Top             =   840
  21.    End
  22.    Begin MSComctlLib.ImageList imlTree 
  23.       Left            =   4680
  24.       Top             =   1140
  25.       _ExtentX        =   1005
  26.       _ExtentY        =   1005
  27.       BackColor       =   -2147483643
  28.       ImageWidth      =   16
  29.       ImageHeight     =   16
  30.       MaskColor       =   12632256
  31.       _Version        =   393216
  32.       BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628} 
  33.          NumListImages   =   2
  34.          BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  35.             Picture         =   "frmClient.frx":030A
  36.             Key             =   ""
  37.          EndProperty
  38.          BeginProperty ListImage2 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  39.             Picture         =   "frmClient.frx":0C34
  40.             Key             =   ""
  41.          EndProperty
  42.       EndProperty
  43.    End
  44.    Begin MSComctlLib.TreeView tvwFriends 
  45.       Height          =   4695
  46.       Left            =   0
  47.       TabIndex        =   0
  48.       Top             =   0
  49.       Width           =   4155
  50.       _ExtentX        =   7329
  51.       _ExtentY        =   8281
  52.       _Version        =   393217
  53.       Indentation     =   88
  54.       LabelEdit       =   1
  55.       Style           =   7
  56.       ImageList       =   "imlTree"
  57.       Appearance      =   1
  58.    End
  59.    Begin VB.Menu mnuFile 
  60.       Caption         =   "&File"
  61.       Begin VB.Menu mnuLogon 
  62.          Caption         =   "&Log on..."
  63.          Shortcut        =   ^L
  64.       End
  65.       Begin VB.Menu mnuLogoff 
  66.          Caption         =   "Lo&g Off"
  67.          Shortcut        =   ^X
  68.       End
  69.       Begin VB.Menu mnuSep 
  70.          Caption         =   "-"
  71.       End
  72.       Begin VB.Menu mnuExit 
  73.          Caption         =   "E&xit"
  74.       End
  75.    End
  76.    Begin VB.Menu mnuOptions 
  77.       Caption         =   "&Options"
  78.       Begin VB.Menu mnuAddFriend 
  79.          Caption         =   "&Add Friend..."
  80.          Shortcut        =   ^A
  81.       End
  82.       Begin VB.Menu mnuBlock 
  83.          Caption         =   "&Block User..."
  84.          Shortcut        =   ^B
  85.       End
  86.       Begin VB.Menu mnuSep1 
  87.          Caption         =   "-"
  88.       End
  89.       Begin VB.Menu mnuSendIM 
  90.          Caption         =   "&Send Message..."
  91.          Shortcut        =   ^S
  92.       End
  93.    End
  94.    Begin VB.Menu mnuPop 
  95.       Caption         =   "pop"
  96.       Visible         =   0   'False
  97.       Begin VB.Menu mnuSend 
  98.          Caption         =   "Send Message"
  99.       End
  100.    End
  101. Attribute VB_Name = "frmClient"
  102. Attribute VB_GlobalNameSpace = False
  103. Attribute VB_Creatable = False
  104. Attribute VB_PredeclaredId = True
  105. Attribute VB_Exposed = False
  106. Option Explicit
  107. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  108. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  109. '  File:       frmClient.frm
  110. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  111. Implements DirectPlay8Event
  112. Private Const msAppTitle As String = "vbMessenger Service"
  113. Private mfExit As Boolean
  114. Private oLog As frmLogin
  115. Private oLeafOnline As Node
  116. Private oLeafOffline As Node
  117. Private oMsgWnd() As frmMsgTemplate
  118. Private mfServerExit As Boolean
  119. Private Sub Form_Load()
  120.     'Initialize DirectPlay
  121.     Set gofrmClient = Me
  122.     InitDPlay
  123.     'Lets put an icon in the system tray
  124.     With sysIcon
  125.         .cbSize = LenB(sysIcon)
  126.         .hwnd = Me.hwnd
  127.         .uFlags = NIF_DOALL
  128.         .uCallbackMessage = WM_MOUSEMOVE
  129.         .hIcon = Me.Icon
  130.         .sTip = msAppTitle & " - Not logged in." & vbNullChar
  131.     End With
  132.     Shell_NotifyIcon NIM_ADD, sysIcon
  133.     SetupDefaultTree
  134.     EnableLoggedinUI False
  135.     EnableSendUI False
  136.     Me.Caption = msAppTitle & " - Not logged in."
  137. End Sub
  138. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  139.     Dim ShellMsg As Long
  140.     ShellMsg = X / Screen.TwipsPerPixelX
  141.     Select Case ShellMsg
  142.     Case WM_LBUTTONDBLCLK
  143.         Me.Visible = True
  144.         Me.SetFocus
  145.     End Select
  146. End Sub
  147. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  148.     If Not mfExit Then
  149.         Cancel = 1
  150.         Me.Hide
  151.     End If
  152. End Sub
  153. Private Sub Form_Unload(Cancel As Integer)
  154.     'Cleanup the DPlay objects
  155.     Cleanup
  156.     'Remove the icon from the system tray
  157.     Shell_NotifyIcon NIM_DELETE, sysIcon
  158. End Sub
  159. Private Sub mnuAddFriend_Click()
  160.     'Let's get the name of the friend we want to add
  161.     Dim sFriend As String
  162.     sFriend = InputBox("Please enter the name of the friend you wish to add", "Add Friend")
  163.     If sFriend = vbNullString Then
  164.         'nothing was entered
  165.         MsgBox "You must enter a friends name to add one.", vbOKOnly Or vbInformation, "Nothing entered."
  166.         Exit Sub
  167.     ElseIf sFriend = gsUserName Then
  168.         'Entered our own name
  169.         MsgBox "Everyone wants to be friends with themselves, but in this sample, it's not allowed.", vbOKOnly Or vbInformation, "Don't enter your name."
  170.         Exit Sub
  171.     End If
  172.     'Ok, let's add the friend
  173.     AddFriend sFriend
  174. End Sub
  175. Private Sub mnuBlock_Click()
  176.     'Let's get the name of the friend we want to block
  177.     Dim sFriend As String
  178.     sFriend = InputBox("Please enter the name of the user you wish to block", "Block user")
  179.     If sFriend = vbNullString Then
  180.         'nothing was entered
  181.         MsgBox "You must enter a user name to block one.", vbOKOnly Or vbInformation, "Nothing entered."
  182.         Exit Sub
  183.     ElseIf sFriend = gsUserName Then
  184.         'Entered our own name
  185.         MsgBox "Why would you want to block yourself?.", vbOKOnly Or vbInformation, "Don't enter your name."
  186.         Exit Sub
  187.     End If
  188.     'Ok, let's add the friend
  189.     BlockUser sFriend
  190. End Sub
  191. Private Sub mnuExit_Click()
  192.     mfExit = True
  193.     Unload Me
  194. End Sub
  195. Private Sub mnuLogoff_Click()
  196.     EnableLoggedinUI False
  197.     gfConnected = False
  198.     gfCreatePlayer = False
  199.     gfLoggedIn = False
  200.     gsUserName = vbNullString
  201.     gsPass = vbNullString
  202.     gsServerName = vbNullString
  203.     Me.Caption = "vbMessenger Service (Not logged in)"
  204.     UpdateText "vbMessenger Service (Not logged in)"
  205.     SetupDefaultTree
  206.     'Initialize DirectPlay
  207.     InitDPlay
  208. End Sub
  209. Private Sub mnuLogon_Click()
  210.     'They want to log on, show the logon screen
  211.     Set oLog = New frmLogin
  212.     oLog.Show , Me
  213. End Sub
  214. Private Sub EnableLoggedinUI(ByVal fEnable As Boolean)
  215.     mnuAddFriend.Enabled = fEnable
  216.     mnuBlock.Enabled = fEnable
  217.     mnuLogoff.Enabled = fEnable
  218.     mnuLogon.Enabled = Not fEnable
  219. End Sub
  220. Private Sub EnableSendUI(ByVal fEnable As Boolean)
  221.     mnuSend.Enabled = fEnable
  222.     mnuSendIM.Enabled = fEnable
  223. End Sub
  224. Private Sub mnuSend_Click()
  225.     mnuSendIM_Click 'Go ahead and send a message
  226. End Sub
  227. Private Sub mnuSendIM_Click()
  228.     Dim frm As frmMsgTemplate
  229.     Set frm = GetMsgWindow(tvwFriends.SelectedItem.Text)
  230.     frm.UserName = tvwFriends.SelectedItem.Text
  231.     frm.Show
  232.     frm.SetFocus
  233. End Sub
  234. Private Sub tmrExit_Timer()
  235.     If mfServerExit Then 'Gotta quit now
  236.         tmrExit.Enabled = False
  237.         MsgBox "The server has disconnected.  This session will now end.", vbOKOnly Or vbInformation, "Exiting..."
  238.         mfExit = True
  239.         Unload Me
  240.         End
  241.     End If
  242. End Sub
  243. Private Sub tvwFriends_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  244.     Dim oNode As Node
  245.     If Button = vbRightButton Then 'They right clicked, should we show the menu?
  246.         If tvwFriends.SelectedItem.Parent Is Nothing Then
  247.             Set oNode = oLeafOffline
  248.         Else
  249.             Set oNode = tvwFriends.SelectedItem
  250.         End If
  251.         If (oNode.Children = 0) And oNode <> oLeafOffline Then
  252.             PopupMenu mnuPop
  253.         End If
  254.     End If
  255. End Sub
  256. Private Sub tvwFriends_NodeClick(ByVal Node As MSComctlLib.Node)
  257.     Dim oNode As Node
  258.     If Node.Parent Is Nothing Then
  259.         Set oNode = oLeafOffline
  260.     Else
  261.         Set oNode = Node
  262.     End If
  263.     If (oNode.Children = 0) And oNode <> oLeafOffline Then
  264.         EnableSendUI True
  265.     Else
  266.         EnableSendUI False
  267.     End If
  268. End Sub
  269. Private Sub UpdateText(sNewText As String)
  270.     'modify our icon text
  271.     sysIcon.sTip = sNewText & vbNullChar
  272.     sysIcon.uFlags = NIF_TIP
  273.     Shell_NotifyIcon NIM_MODIFY, sysIcon
  274. End Sub
  275. Private Function GetMsgWindow(ByVal sUser As String) As frmMsgTemplate
  276.     'Let's check to see if there is a window open
  277.     Dim lCount As Long, lNumWindows As Long
  278.     On Error Resume Next
  279.     lNumWindows = UBound(oMsgWnd)
  280.     If Err = 0 Then
  281.         For lCount = 0 To lNumWindows
  282.             If sUser = oMsgWnd(lCount).UserName Then
  283.                 Set GetMsgWindow = oMsgWnd(lCount)
  284.                 Exit Function
  285.             End If
  286.         Next
  287.         ReDim oMsgWnd(UBound(oMsgWnd) + 1)
  288.         Set oMsgWnd(UBound(oMsgWnd)) = New frmMsgTemplate
  289.         Set GetMsgWindow = oMsgWnd(UBound(oMsgWnd))
  290.     Else
  291.         ReDim oMsgWnd(0)
  292.         Set oMsgWnd(0) = New frmMsgTemplate
  293.         Set GetMsgWindow = oMsgWnd(0)
  294.     End If
  295.         
  296. End Function
  297. Private Sub SetupDefaultTree()
  298.     'Clear the tree first
  299.     tvwFriends.Nodes.Clear
  300.     'Let's add the two default icons into our treeview
  301.     Set oLeafOnline = tvwFriends.Nodes.Add(, , "OnlineLeafKey", "Friends online", 1, 1)
  302.     Set oLeafOffline = tvwFriends.Nodes.Add(, , "OfflineLeafKey", "Friends offline", 2, 2)
  303. End Sub
  304. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  305.     'VB requires that we must implement *every* member of this interface
  306. End Sub
  307. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  308.     'VB requires that we must implement *every* member of this interface
  309. End Sub
  310. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  311.     'VB requires that we must implement *every* member of this interface
  312. End Sub
  313. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  314.     If dpnotify.hResultCode <> 0 Then
  315.         MsgBox "The server does not exist or is unavailable.", vbOKOnly Or vbInformation, "Unavailable"
  316.     Else
  317.         If gfCreatePlayer Then
  318.             CreatePlayer 'We're creating a player
  319.         Else
  320.             LogonPlayer 'We're just logging in
  321.         End If
  322.     End If
  323.     gfConnected = True
  324. End Sub
  325. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  326.     'VB requires that we must implement *every* member of this interface
  327. End Sub
  328. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  329.     'VB requires that we must implement *every* member of this interface
  330. End Sub
  331. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  332.     'VB requires that we must implement *every* member of this interface
  333. End Sub
  334. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  335.     'VB requires that we must implement *every* member of this interface
  336. End Sub
  337. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  338.     'VB requires that we must implement *every* member of this interface
  339. End Sub
  340. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  341.     'VB requires that we must implement *every* member of this interface
  342. End Sub
  343. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  344.     'VB requires that we must implement *every* member of this interface
  345. End Sub
  346. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  347.     'VB requires that we must implement *every* member of this interface
  348. End Sub
  349. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  350.     'VB requires that we must implement *every* member of this interface
  351. End Sub
  352. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  353.     'VB requires that we must implement *every* member of this interface
  354. End Sub
  355. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  356.     'We need to get each message we receive from a client, process it, and respond accordingly
  357.     Dim lMsg As Long, lOffset As Long
  358.     Dim oNewMsg() As Byte, lNewOffSet As Long
  359.     Dim sUsername As String, lNumFriends As Long, lCount As Long
  360.     Dim lNewMsg As Long, oNode As Node
  361.     Dim sChat As String, fChatFrm As frmMsgTemplate
  362.     Dim fFriend As Boolean, fFound As Boolean
  363.     With dpnotify
  364.     GetDataFromBuffer .ReceivedData, lMsg, LenB(lMsg), lOffset
  365.     Select Case lMsg 'The server will only receive certain messages.  Handle those.
  366.     Case Msg_LoginSuccess 'Login successfully completed.
  367.         'All we really need to do is get rid of the login screen.
  368.         If Not (oLog Is Nothing) Then
  369.             Unload oLog
  370.             Set oLog = Nothing
  371.         End If
  372.         Unload frmCreate
  373.         gfLoggedIn = True
  374.         EnableLoggedinUI True
  375.         Me.Caption = msAppTitle & " - (" & gsUserName & ")"
  376.         UpdateText msAppTitle & " - (" & gsUserName & ")"
  377.     Case Msg_InvalidPassword 'The server didn't like our password
  378.         'The password they entered was invalid.
  379.         MsgBox "The password you entered was invalid.", vbOKOnly Or vbInformation, "Not valid."
  380.         oLog.txtPassword = vbNullString
  381.         oLog.txtPassword.SetFocus
  382.     Case Msg_InvalidUser 'We do not exist on this server
  383.         'This user does not exist
  384.         MsgBox "The username you entered does not exist.", vbOKOnly Or vbInformation, "Not valid."
  385.     Case Msg_UserAlreadyExists 'We can't create this account since the user exists
  386.         'This user already exists
  387.         MsgBox "The username you entered already exists." & vbCrLf & "You must choose a different one.", vbOKOnly Or vbInformation, "Not valid."
  388.     Case Msg_SendClientFriends 'The server is going to send us a list of our current friends
  389.         GetDataFromBuffer .ReceivedData, lNumFriends, LenB(lNumFriends), lOffset
  390.         'Ok, now go through and add each friend to our 'offline' list (The server will notify who is online after this message
  391.         For lCount = 1 To lNumFriends
  392.             GetDataFromBuffer .ReceivedData, fFriend, LenB(fFriend), lOffset
  393.             sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  394.             'Add this user to our list
  395.             If fFriend Then
  396.                 tvwFriends.Nodes.Add oLeafOffline, tvwChild, sUsername, sUsername, 2, 2
  397.             Else
  398.                 tvwFriends.Nodes.Add oLeafOffline, tvwChild, sUsername, sUsername & " (BLOCKED)", 2, 2
  399.             End If
  400.         Next
  401.         oLeafOffline.Expanded = True
  402.         oLeafOnline.Expanded = True
  403.     Case Msg_FriendAdded
  404.         sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  405.         fFound = False
  406.         For Each oNode In tvwFriends.Nodes
  407.             If oNode.Key = sUsername Then
  408.                 oNode.Text = sUsername
  409.                 fFound = True
  410.             End If
  411.         Next
  412.         If Not fFound Then tvwFriends.Nodes.Add oLeafOffline, tvwChild, sUsername, sUsername, 2, 2
  413.         'Friend added successfully
  414.         MsgBox sUsername & " added successfully to your friends list.", vbOKOnly Or vbInformation, "Added."
  415.     Case Msg_FriendBlocked
  416.         sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  417.         fFound = False
  418.         For Each oNode In tvwFriends.Nodes
  419.             If oNode.Key = sUsername Then
  420.                 oNode.Text = sUsername & " (BLOCKED)"
  421.                 fFound = True
  422.             End If
  423.         Next
  424.         If Not fFound Then tvwFriends.Nodes.Add oLeafOffline, tvwChild, sUsername, sUsername & " (BLOCKED)", 2, 2
  425.         'Friend blocked successfully
  426.         MsgBox sUsername & " added successfully to your blocked list.", vbOKOnly Or vbInformation, "Added."
  427.             
  428.     Case Msg_FriendDoesNotExist
  429.         'Friend doesn't exist
  430.         MsgBox "You cannot add this friend, since they do not exist.", vbOKOnly Or vbInformation, "Unknown."
  431.     Case Msg_BlockUserDoesNotExist
  432.         'Friend doesn't exist
  433.         MsgBox "You cannot block this user, since they do not exist.", vbOKOnly Or vbInformation, "Unknown."
  434.         
  435.     Case Msg_FriendLogon
  436.         'We need to go through each of the current nodes and see if this is that friend
  437.         sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  438.         For Each oNode In tvwFriends.Nodes
  439.             If oNode.Key = sUsername And oNode.Children = 0 Then
  440.                 oNode.Image = 1: oNode.SelectedImage = 1
  441.                 Set oNode.Parent = oLeafOnline
  442.             End If
  443.         Next
  444.     Case Msg_FriendLogoff
  445.         'We need to go through each of the current nodes and see if this is that friend
  446.         sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  447.         For Each oNode In tvwFriends.Nodes
  448.             If oNode.Key = sUsername And oNode.Children = 0 Then
  449.                 oNode.Image = 2: oNode.SelectedImage = 2
  450.                 Set oNode.Parent = oLeafOffline
  451.             End If
  452.         Next
  453.     Case Msg_ReceiveMessage
  454.         'We need to go through each of the current forms and see if this is friend is loaded
  455.         sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  456.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  457.         Set fChatFrm = GetMsgWindow(sUsername)
  458.         fChatFrm.UserName = sUsername
  459.         fChatFrm.Show
  460.         fChatFrm.SetFocus
  461.         fChatFrm.AddChatMessage sChat
  462.     Case Msg_UserBlocked
  463.         'This user has blocked me
  464.         sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  465.         Set fChatFrm = GetMsgWindow(sUsername)
  466.         fChatFrm.UserName = sUsername
  467.         fChatFrm.Show
  468.         fChatFrm.SetFocus
  469.         fChatFrm.AddChatMessage "***** Your message to " & sUsername & " could not be delivered since they have blocked you."
  470.         
  471.     End Select
  472.     End With
  473. End Sub
  474. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  475.     'VB requires that we must implement *every* member of this interface
  476. End Sub
  477. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  478.     'We're no longer connected for some reason.
  479.     mfServerExit = True
  480. End Sub
  481.