CommSettingDlg.cpp
上传用户:royluo
上传日期:2007-01-05
资源大小:1584k
文件大小:5k
源码类别:

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   CommSettingDlg.cpp
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Implements the communiction settings dialog enables 
  11. *                       the user specify a nickname, select between hosting or
  12. *                       connecting to a host and selecting the connection 
  13. *                       provider.
  14. *                       
  15. *                                                                             
  16. *   Authors: Eran Yariv - 28484475                                           
  17. *            Moshe Zur  - 24070856                                           
  18. *                                                                            
  19. *                                                                            
  20. *   Date: 23/09/98                                                           
  21. *                                                                            
  22. ******************************************************************************/
  23. #include "stdafx.h"
  24. #include "tanks.h"
  25. #include "CommSettingDlg.h"
  26. #include "CommManager.h"
  27. #include "dplay.h"
  28. #ifdef _DEBUG
  29. #define new DEBUG_NEW
  30. #undef THIS_FILE
  31. static char THIS_FILE[] = __FILE__;
  32. #endif
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CCommSettingDlg dialog
  35. CCommSettingDlg::CCommSettingDlg()
  36.     : CDialog(CCommSettingDlg::IDD, NULL)
  37. {
  38.     //{{AFX_DATA_INIT(CCommSettingDlg)
  39.     m_strPlayerName = _T("");
  40.     //}}AFX_DATA_INIT
  41. }
  42. void CCommSettingDlg::DoDataExchange(CDataExchange* pDX)
  43. {
  44.     CDialog::DoDataExchange(pDX);
  45.     //{{AFX_DATA_MAP(CCommSettingDlg)
  46.     DDX_Control(pDX, IDOK, m_ctrOK);
  47.     DDX_Control(pDX, IDCANCEL, m_ctrCancel);
  48.     DDX_Control(pDX, IDC_SPLISTBOX, m_ConnectionsList);
  49.     DDX_Text(pDX, IDC_PLAYERNAME_EDIT, m_strPlayerName);
  50.     //}}AFX_DATA_MAP
  51. }
  52. BEGIN_MESSAGE_MAP(CCommSettingDlg, CDialog)
  53.     //{{AFX_MSG_MAP(CCommSettingDlg)
  54.     //}}AFX_MSG_MAP
  55. END_MESSAGE_MAP()
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CCommSettingDlg message handlers
  58. void CCommSettingDlg::OnOK() 
  59. {
  60.     // Get data from text box:
  61.     UpdateData(TRUE);
  62.     // Return data to CommManager:
  63.     TANKS_APP->SetStoredPlayerName(m_strPlayerName);
  64.     TANKS_APP->SetStoredGUID (
  65.         *(LPGUID)(m_ConnectionsList.GetItemData(m_ConnectionsList.GetCurSel())) );
  66.     BOOL bIsHost = (1 == ((CButton*)GetDlgItem(IDC_HOST_RADIO))->GetCheck());
  67.     TANKS_APP->SetStoredIsHostFlag (bIsHost);
  68.     AfxGetMainWnd()->GetMenu()->EnableMenuItem 
  69.         (ID_SERVER_MGMT, bIsHost ? (MF_BYCOMMAND | MF_ENABLED) : (MF_BYCOMMAND | MF_GRAYED));
  70.     CDialog::OnOK();
  71. }
  72. BOOL CCommSettingDlg::OnInitDialog() 
  73. {
  74.     CDialog::OnInitDialog();
  75.     // Limit EditBox text to max player name:
  76.     ((CEdit*)GetDlgItem(IDC_PLAYERNAME_EDIT))->LimitText(MAX_PLAYER_NAME);
  77.     // Init player name:
  78.     m_strPlayerName = TANKS_APP->GetStoredPlayerName();
  79.     if (TANKS_APP->GetStoredIsHostFlag())
  80.         // Init radio button on Host:
  81.         ((CButton*)GetDlgItem(IDC_HOST_RADIO))->SetCheck(TRUE);
  82.     else
  83.         ((CButton*)GetDlgItem(IDC_PLAYER_RADIO))->SetCheck(TRUE);
  84.     // Init the connection list box:
  85.     DirectPlayEnumerate (EnumConnections, m_hWnd);
  86.     
  87.     // Init animated buttons:
  88.     m_ctrCancel.LoadAVI(IDR_AVI_CANCEL);
  89.     m_ctrOK.LoadAVI(IDR_AVI_OK);
  90.     UpdateData(FALSE);
  91.     
  92.     // Select stored GUID, if exists:
  93.     SelectStoredGUID();
  94.     GetDlgItem(IDOK)->SetFocus();
  95.     return TRUE;  // return TRUE unless you set the focus to a control
  96. }
  97. BOOL FAR PASCAL EnumConnections(
  98.     LPGUID lpSPGuid, 
  99.     LPTSTR lpszSPName, 
  100.     DWORD /*dwMajorVersion*/,
  101.     DWORD /*dwMinorVersion*/, 
  102.     LPVOID lpContext)
  103. {
  104.     HWND    hWnd = (HWND)lpContext;
  105.     LRESULT iIndex;
  106.     // Store the service provider name:
  107.     iIndex = SendDlgItemMessage(hWnd, IDC_SPLISTBOX, LB_ADDSTRING,
  108.         0, (LPARAM) lpszSPName);
  109.     // Store the service provider guid:
  110.     if (iIndex != LB_ERR)
  111.         SendDlgItemMessage(hWnd, IDC_SPLISTBOX, LB_SETITEMDATA, 
  112.             (WPARAM) iIndex, (LPARAM) lpSPGuid);
  113.     return(TRUE);
  114. }
  115. void
  116. CCommSettingDlg::SelectStoredGUID()
  117. {
  118.     GUID guidStored;
  119.     int ind = 0;
  120.     // Check if there is a stored GUID:
  121.     if (TANKS_APP->GetStoredGUID(&guidStored)) 
  122.     {
  123.         // Find index of entry with the same GUID as in registry:
  124.         for (; ind < m_ConnectionsList.GetCount(); ind++)
  125.             if (*(LPGUID)(m_ConnectionsList.GetItemData(ind)) == guidStored)
  126.                 break;
  127.         // If the stored GUID doesn't match any of the enumerated GUID's
  128.         // select the 1st:
  129.         if (ind == m_ConnectionsList.GetCount())
  130.             ind = 0;
  131.     }
  132.     // Select stored GUID:
  133.     m_ConnectionsList.SetCurSel(ind);
  134. }