MainFrm.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:11k
源码类别:

游戏

开发平台:

Visual C++

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "Billiards.h"
  5. #include "BilliardsPlayView.h"
  6. #include "BilliardsFormView.h"
  7. #include "ClientDlg.h"
  8. #include "ServerDlg.h"
  9. #include "BilliardsDoc.h"
  10. #include "WizardDlg.h"
  11. #include "ToolBarDlg.h"
  12. #include "MainFrm.h"
  13. #include "Splash.h"
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CMainFrame
  21. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  22. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  23. //{{AFX_MSG_MAP(CMainFrame)
  24. ON_WM_CREATE()
  25. ON_COMMAND(ID_NEWGAME, OnNewgame)
  26. ON_COMMAND(ID_JOINGAME, OnJoingame)
  27. ON_COMMAND(ID_STOPGAME, OnStopgame)
  28. ON_WM_DESTROY()
  29. ON_COMMAND(ID_MOUSE_SELECT, OnMouseSelect)
  30. ON_UPDATE_COMMAND_UI(ID_MOUSE_SELECT, OnUpdateMouseSelect)
  31. ON_COMMAND(ID_MOUSE_SPIN, OnMouseSpin)
  32. ON_UPDATE_COMMAND_UI(ID_MOUSE_SPIN, OnUpdateMouseSpin)
  33. ON_COMMAND(ID_MOUSE_TRANSLATE, OnMouseTranslate)
  34. ON_UPDATE_COMMAND_UI(ID_MOUSE_TRANSLATE, OnUpdateMouseTranslate)
  35. ON_COMMAND(ID_MOUSE_ZOOM, OnMouseZoom)
  36. ON_UPDATE_COMMAND_UI(ID_MOUSE_ZOOM, OnUpdateMouseZoom)
  37. ON_COMMAND(ID_SOUND_BALL, OnSoundBall)
  38. ON_UPDATE_COMMAND_UI(ID_SOUND_BALL, OnUpdateSoundBall)
  39. ON_COMMAND(ID_SOUND_CUSH, OnSoundCush)
  40. ON_UPDATE_COMMAND_UI(ID_SOUND_CUSH, OnUpdateSoundCush)
  41. ON_COMMAND(ID_SOUND_GOAL, OnSoundGoal)
  42. ON_UPDATE_COMMAND_UI(ID_SOUND_GOAL, OnUpdateSoundGoal)
  43. ON_COMMAND(ID_SOUND_MSG, OnSoundMsg)
  44. ON_UPDATE_COMMAND_UI(ID_SOUND_MSG, OnUpdateSoundMsg)
  45. ON_WM_ERASEBKGND()
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. static UINT indicators[] =
  49. {
  50. ID_SEPARATOR,           // status line indicator
  51. ID_INDICATOR_CAPS,
  52. ID_INDICATOR_NUM,
  53. ID_INDICATOR_SCRL,
  54. };
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CMainFrame construction/destruction
  57. CMainFrame::CMainFrame()
  58. {
  59. m_bAutoMenuEnable  = FALSE;
  60. m_bSoundBall=1;
  61. m_bSoundCush=1;
  62. m_bSoundGoal=1;
  63. m_bSoundMsg=1;
  64. }
  65. CMainFrame::~CMainFrame()
  66. {
  67. }
  68. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  69. {
  70. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  71. return -1;
  72. if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
  73. | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
  74. !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  75. {
  76. TRACE0("Failed to create toolbarn");
  77. return -1;      // fail to create
  78. }
  79.     
  80.     if(!m_wndDialogBar.Create(this,IDD_DIALOG_COLORBAR,CBRS_BOTTOM| CBRS_TOOLTIPS | CBRS_FLYBY| CBRS_GRIPPER | CBRS_TOOLTIPS,IDD_DIALOG_COLORBAR))
  81.     {
  82.         TRACE("Failed to create dialogbarn");
  83.         return -1;
  84.     }
  85.     
  86. if (!m_wndStatusBar.Create(this) ||
  87. !m_wndStatusBar.SetIndicators(indicators,
  88.   sizeof(indicators)/sizeof(UINT)))
  89. {
  90. TRACE0("Failed to create status barn");
  91. return -1;      // fail to create
  92. }
  93.    
  94. m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  95.    
  96. EnableDocking(CBRS_ALIGN_ANY);
  97. DockControlBar(&m_wndToolBar);
  98.     
  99.     m_wndDialogBar.EnableDocking(CBRS_ALIGN_ANY);
  100.     DockControlBar(&m_wndDialogBar); 
  101.     m_wndToolBar.GetToolBarCtrl().HideButton(ID_STOPGAME);
  102.     CSplashWnd::ShowSplashScreen(this);
  103.     //CRect rect;
  104.     
  105.     
  106.     //dlg->GetWindowRect(rect);
  107.     //dlg->SetWindowPos(&CWnd::wndBottom,0,0,rect.Width(),55,SWP_NOMOVE);
  108.     return 0;
  109. }
  110. BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
  111. CCreateContext* pContext)
  112. {
  113. m_wndSplitter.CreateStatic(this,1,2);
  114. m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CBilliardsPlayView),CSize(550,200),pContext);
  115. m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CBilliardsFormView),CSize(550,400),pContext);
  116. return TRUE;
  117. }
  118. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  119. {
  120.     cs.cx=774;
  121.     cs.cy=520;
  122.     cs.style &=~WS_MAXIMIZEBOX&~WS_THICKFRAME;
  123. if( !CFrameWnd::PreCreateWindow(cs) )
  124. return FALSE;
  125. return TRUE;
  126. }
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CMainFrame diagnostics
  129. #ifdef _DEBUG
  130. void CMainFrame::AssertValid() const
  131. {
  132. CFrameWnd::AssertValid();
  133. }
  134. void CMainFrame::Dump(CDumpContext& dc) const
  135. {
  136. CFrameWnd::Dump(dc);
  137. }
  138. #endif //_DEBUG
  139. /////////////////////////////////////////////////////////////////////////////
  140. // CMainFrame message handlers
  141. void CMainFrame::OnNewgame() 
  142. {
  143. CServerDlg m_serverdlg;
  144.     
  145. CBilliardsFormView *pView = (CBilliardsFormView *)m_wndSplitter.GetPane(0,1);
  146. CBilliardsDoc *pDoc=pView->GetDocument();
  147.     
  148.     
  149. if(m_serverdlg.DoModal()==IDOK){
  150. UpdateData();
  151. if(m_serverdlg.m_strNickname.IsEmpty())
  152. m_serverdlg.m_strNickname="服务器端";
  153. pDoc->m_strServer=m_serverdlg.m_strNickname;
  154. pDoc->m_strServer+=":";
  155. pView->GetDlgItem(IDC_STATIC_SERVER)->SetWindowText(pDoc->m_strServer);
  156.         
  157. if(pDoc->m_pListenSocket->Create(6000)){
  158.             if(pDoc->m_pListenSocket->Listen()){
  159. AfxMessageBox("开始等待新用户加入",MB_OK);
  160.                 CMenu* mmenu = GetMenu();
  161.          CMenu* submenu = mmenu->GetSubMenu(0);
  162.                 submenu->EnableMenuItem(ID_NEWGAME,MF_GRAYED);
  163.                 submenu->EnableMenuItem(ID_JOINGAME,MF_GRAYED);
  164.                 submenu->EnableMenuItem(ID_STOPGAME,MF_ENABLED);
  165.                 
  166.                 m_wndToolBar.GetToolBarCtrl().HideButton(ID_NEWGAME);
  167.                 m_wndToolBar.GetToolBarCtrl().HideButton(ID_JOINGAME);
  168.                 m_wndToolBar.GetToolBarCtrl().HideButton(ID_STOPGAME,FALSE);
  169.                 
  170.                 pDoc->m_bPlayer=SERVER;
  171.                 
  172.     }
  173. else
  174. AfxMessageBox("无法开始侦听",MB_OK);
  175. }
  176. }
  177. }
  178. void CMainFrame::OnJoingame() 
  179. {
  180. CClientDlg m_clientdlg;
  181.     
  182.     CBilliardsFormView *pView = (CBilliardsFormView *)m_wndSplitter.GetPane(0,1);
  183.     CBilliardsPlayView *pPlayView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
  184. CBilliardsDoc *pDoc=pView->GetDocument();
  185.     CString strTemp;
  186. if(m_clientdlg.DoModal()==IDOK){
  187. UpdateData();
  188. if(m_clientdlg.m_strNickname.IsEmpty())
  189. m_clientdlg.m_strNickname="客户端";
  190. pDoc->m_strClient=m_clientdlg.m_strNickname;
  191. pDoc->m_strClient+=":";
  192. CBilliardsFormView *pView = (CBilliardsFormView *)m_wndSplitter.GetPane(0,1);
  193. pView->GetDlgItem(IDC_STATIC_CLIENT)->SetWindowText(pDoc->m_strClient);
  194.         if(pDoc->m_pClientSocket->Create())
  195.             if(pDoc->m_pClientSocket->Connect(m_clientdlg.m_strAddress,6000)){
  196.                 AfxMessageBox("成功连接服务器",MB_OK);
  197.                 CMenu* mmenu = GetMenu();
  198.          CMenu* submenu = mmenu->GetSubMenu(0);
  199.                 submenu->EnableMenuItem(ID_NEWGAME,MF_GRAYED);
  200.                 submenu->EnableMenuItem(ID_JOINGAME,MF_GRAYED);
  201.                 submenu->EnableMenuItem(ID_STOPGAME,MF_ENABLED);
  202.                 m_wndToolBar.GetToolBarCtrl().HideButton(ID_NEWGAME);
  203.                 m_wndToolBar.GetToolBarCtrl().HideButton(ID_JOINGAME);
  204.                 m_wndToolBar.GetToolBarCtrl().HideButton(ID_STOPGAME,FALSE);
  205.                 pView->GetDlgItem(IDC_BUTTON_SHOOT)->EnableWindow(FALSE);
  206.                 pPlayView->m_bCanHit=FALSE;
  207.                 pDoc->m_bPlayer=CLIENT;
  208.                 strTemp="0"+pDoc->m_strClient;
  209.                 pDoc->m_pClientSocket->Send(strTemp,strTemp.GetLength());
  210.             }
  211.             else{
  212.                 pDoc->m_pClientSocket->ShutDown();
  213.                 pDoc->m_pClientSocket->Close();
  214.                 
  215.                 AfxMessageBox("无法连接服务器");
  216.             }
  217.     }
  218. }
  219. void CMainFrame::OnStopgame() 
  220. {
  221. CBilliardsFormView *pView = (CBilliardsFormView *)m_wndSplitter.GetPane(0,1);
  222. CBilliardsDoc *pDoc=pView->GetDocument();
  223. pDoc->m_pClientSocket->Send("3",1);
  224. if(pDoc->m_bPlayer==SERVER){
  225. pDoc->m_pListenSocket->ShutDown();
  226. pDoc->m_pListenSocket->Close();
  227. pDoc->m_pClientSocket->ShutDown();
  228. pDoc->m_pClientSocket->Close();
  229.         m_wndToolBar.GetToolBarCtrl().HideButton(ID_NEWGAME,FALSE);
  230.         m_wndToolBar.GetToolBarCtrl().HideButton(ID_JOINGAME,FALSE);
  231.         m_wndToolBar.GetToolBarCtrl().HideButton(ID_STOPGAME);
  232.         pDoc->m_bPlayer=IDLE;
  233.         pView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);
  234. }
  235. else{
  236. pDoc->m_pClientSocket->ShutDown();
  237. pDoc->m_pClientSocket->Close();
  238.         m_wndToolBar.GetToolBarCtrl().HideButton(ID_NEWGAME,FALSE);
  239.         m_wndToolBar.GetToolBarCtrl().HideButton(ID_JOINGAME,FALSE);
  240.         m_wndToolBar.GetToolBarCtrl().HideButton(ID_STOPGAME);
  241.         pDoc->m_bPlayer=IDLE;
  242.         pView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);
  243. }
  244. }
  245. void CMainFrame::OnDestroy() 
  246. {
  247. CBilliardsFormView *pView = (CBilliardsFormView *)m_wndSplitter.GetPane(0,1);
  248. CBilliardsDoc *pDoc=pView->GetDocument();
  249. pDoc->m_pClientSocket->Send("3",1);
  250. if(pDoc->m_bPlayer==SERVER){
  251. pDoc->m_pListenSocket->ShutDown();
  252. pDoc->m_pListenSocket->Close();
  253. pDoc->m_pClientSocket->ShutDown();
  254. pDoc->m_pClientSocket->Close();
  255. }
  256. else{
  257. pDoc->m_pClientSocket->ShutDown();
  258. pDoc->m_pClientSocket->Close();
  259. }
  260.     CFrameWnd::OnDestroy();
  261. }
  262. void CMainFrame::OnMouseSelect() 
  263. {
  264.     CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
  265.     pView->m_nMouseAct=MOUSE_SELECT;
  266. }
  267. void CMainFrame::OnUpdateMouseSelect(CCmdUI* pCmdUI) 
  268. {
  269. CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
  270. pCmdUI->SetCheck(pView->m_nMouseAct==MOUSE_SELECT);
  271. }
  272. void CMainFrame::OnMouseSpin() 
  273. {
  274.     CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
  275.     pView->m_nMouseAct=MOUSE_SPIN;
  276. }
  277. void CMainFrame::OnUpdateMouseSpin(CCmdUI* pCmdUI) 
  278. {
  279. CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
  280. pCmdUI->SetCheck(pView->m_nMouseAct==MOUSE_SPIN);
  281. }
  282. void CMainFrame::OnMouseTranslate() 
  283. {
  284.     CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
  285.     pView->m_nMouseAct=MOUSE_TRANSLATE;
  286. }
  287. void CMainFrame::OnUpdateMouseTranslate(CCmdUI* pCmdUI) 
  288. {
  289. CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
  290. pCmdUI->SetCheck(pView->m_nMouseAct==MOUSE_TRANSLATE);
  291. }
  292. void CMainFrame::OnMouseZoom() 
  293. {
  294.     CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
  295.     pView->m_nMouseAct=MOUSE_ZOOM;
  296. }
  297. void CMainFrame::OnUpdateMouseZoom(CCmdUI* pCmdUI) 
  298. {
  299. CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
  300. pCmdUI->SetCheck(pView->m_nMouseAct==MOUSE_ZOOM);
  301. }
  302. void CMainFrame::OnSoundBall() 
  303. {
  304. m_bSoundBall=!m_bSoundBall;
  305. }
  306. void CMainFrame::OnUpdateSoundBall(CCmdUI* pCmdUI) 
  307. {
  308. pCmdUI->SetCheck(m_bSoundBall);
  309. }
  310. void CMainFrame::OnSoundCush() 
  311. {
  312. m_bSoundCush=!m_bSoundCush;
  313. }
  314. void CMainFrame::OnUpdateSoundCush(CCmdUI* pCmdUI) 
  315. {
  316. pCmdUI->SetCheck(m_bSoundCush);
  317. }
  318. void CMainFrame::OnSoundGoal() 
  319. {
  320. m_bSoundGoal=!m_bSoundGoal;
  321. }
  322. void CMainFrame::OnUpdateSoundGoal(CCmdUI* pCmdUI) 
  323. {
  324. pCmdUI->SetCheck(m_bSoundGoal);
  325. }
  326. void CMainFrame::OnSoundMsg() 
  327. {
  328. m_bSoundMsg=!m_bSoundMsg;
  329. }
  330. void CMainFrame::OnUpdateSoundMsg(CCmdUI* pCmdUI) 
  331. {
  332. pCmdUI->SetCheck(m_bSoundMsg);
  333. }
  334. BOOL CMainFrame::OnEraseBkgnd(CDC* pDC) 
  335. {
  336.     pDC->SetBkColor(RGB(255,255,255));
  337. return CFrameWnd::OnEraseBkgnd(pDC);
  338. }