MainFrm.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:11k
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "Billiards.h"
- #include "BilliardsPlayView.h"
- #include "BilliardsFormView.h"
- #include "ClientDlg.h"
- #include "ServerDlg.h"
- #include "BilliardsDoc.h"
- #include "WizardDlg.h"
- #include "ToolBarDlg.h"
- #include "MainFrm.h"
- #include "Splash.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_COMMAND(ID_NEWGAME, OnNewgame)
- ON_COMMAND(ID_JOINGAME, OnJoingame)
- ON_COMMAND(ID_STOPGAME, OnStopgame)
- ON_WM_DESTROY()
- ON_COMMAND(ID_MOUSE_SELECT, OnMouseSelect)
- ON_UPDATE_COMMAND_UI(ID_MOUSE_SELECT, OnUpdateMouseSelect)
- ON_COMMAND(ID_MOUSE_SPIN, OnMouseSpin)
- ON_UPDATE_COMMAND_UI(ID_MOUSE_SPIN, OnUpdateMouseSpin)
- ON_COMMAND(ID_MOUSE_TRANSLATE, OnMouseTranslate)
- ON_UPDATE_COMMAND_UI(ID_MOUSE_TRANSLATE, OnUpdateMouseTranslate)
- ON_COMMAND(ID_MOUSE_ZOOM, OnMouseZoom)
- ON_UPDATE_COMMAND_UI(ID_MOUSE_ZOOM, OnUpdateMouseZoom)
- ON_COMMAND(ID_SOUND_BALL, OnSoundBall)
- ON_UPDATE_COMMAND_UI(ID_SOUND_BALL, OnUpdateSoundBall)
- ON_COMMAND(ID_SOUND_CUSH, OnSoundCush)
- ON_UPDATE_COMMAND_UI(ID_SOUND_CUSH, OnUpdateSoundCush)
- ON_COMMAND(ID_SOUND_GOAL, OnSoundGoal)
- ON_UPDATE_COMMAND_UI(ID_SOUND_GOAL, OnUpdateSoundGoal)
- ON_COMMAND(ID_SOUND_MSG, OnSoundMsg)
- ON_UPDATE_COMMAND_UI(ID_SOUND_MSG, OnUpdateSoundMsg)
- ON_WM_ERASEBKGND()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- m_bAutoMenuEnable = FALSE;
- m_bSoundBall=1;
- m_bSoundCush=1;
- m_bSoundGoal=1;
- m_bSoundMsg=1;
- }
- CMainFrame::~CMainFrame()
- {
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
- | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
-
- if(!m_wndDialogBar.Create(this,IDD_DIALOG_COLORBAR,CBRS_BOTTOM| CBRS_TOOLTIPS | CBRS_FLYBY| CBRS_GRIPPER | CBRS_TOOLTIPS,IDD_DIALOG_COLORBAR))
- {
- TRACE("Failed to create dialogbarn");
- return -1;
- }
-
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
-
- m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
-
- EnableDocking(CBRS_ALIGN_ANY);
- DockControlBar(&m_wndToolBar);
-
- m_wndDialogBar.EnableDocking(CBRS_ALIGN_ANY);
- DockControlBar(&m_wndDialogBar);
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_STOPGAME);
- CSplashWnd::ShowSplashScreen(this);
-
- //CRect rect;
-
-
- //dlg->GetWindowRect(rect);
- //dlg->SetWindowPos(&CWnd::wndBottom,0,0,rect.Width(),55,SWP_NOMOVE);
- return 0;
- }
- BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
- CCreateContext* pContext)
- {
- m_wndSplitter.CreateStatic(this,1,2);
- m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CBilliardsPlayView),CSize(550,200),pContext);
- m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CBilliardsFormView),CSize(550,400),pContext);
- return TRUE;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- cs.cx=774;
- cs.cy=520;
- cs.style &=~WS_MAXIMIZEBOX&~WS_THICKFRAME;
- if( !CFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
- void CMainFrame::OnNewgame()
- {
- CServerDlg m_serverdlg;
-
- CBilliardsFormView *pView = (CBilliardsFormView *)m_wndSplitter.GetPane(0,1);
- CBilliardsDoc *pDoc=pView->GetDocument();
-
-
- if(m_serverdlg.DoModal()==IDOK){
- UpdateData();
- if(m_serverdlg.m_strNickname.IsEmpty())
- m_serverdlg.m_strNickname="服务器端";
- pDoc->m_strServer=m_serverdlg.m_strNickname;
- pDoc->m_strServer+=":";
-
- pView->GetDlgItem(IDC_STATIC_SERVER)->SetWindowText(pDoc->m_strServer);
-
- if(pDoc->m_pListenSocket->Create(6000)){
- if(pDoc->m_pListenSocket->Listen()){
- AfxMessageBox("开始等待新用户加入",MB_OK);
- CMenu* mmenu = GetMenu();
- CMenu* submenu = mmenu->GetSubMenu(0);
- submenu->EnableMenuItem(ID_NEWGAME,MF_GRAYED);
- submenu->EnableMenuItem(ID_JOINGAME,MF_GRAYED);
- submenu->EnableMenuItem(ID_STOPGAME,MF_ENABLED);
-
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_NEWGAME);
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_JOINGAME);
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_STOPGAME,FALSE);
-
- pDoc->m_bPlayer=SERVER;
-
- }
- else
- AfxMessageBox("无法开始侦听",MB_OK);
- }
- }
-
- }
- void CMainFrame::OnJoingame()
- {
- CClientDlg m_clientdlg;
-
- CBilliardsFormView *pView = (CBilliardsFormView *)m_wndSplitter.GetPane(0,1);
- CBilliardsPlayView *pPlayView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
- CBilliardsDoc *pDoc=pView->GetDocument();
- CString strTemp;
- if(m_clientdlg.DoModal()==IDOK){
- UpdateData();
- if(m_clientdlg.m_strNickname.IsEmpty())
- m_clientdlg.m_strNickname="客户端";
- pDoc->m_strClient=m_clientdlg.m_strNickname;
- pDoc->m_strClient+=":";
- CBilliardsFormView *pView = (CBilliardsFormView *)m_wndSplitter.GetPane(0,1);
- pView->GetDlgItem(IDC_STATIC_CLIENT)->SetWindowText(pDoc->m_strClient);
- if(pDoc->m_pClientSocket->Create())
- if(pDoc->m_pClientSocket->Connect(m_clientdlg.m_strAddress,6000)){
- AfxMessageBox("成功连接服务器",MB_OK);
- CMenu* mmenu = GetMenu();
- CMenu* submenu = mmenu->GetSubMenu(0);
- submenu->EnableMenuItem(ID_NEWGAME,MF_GRAYED);
- submenu->EnableMenuItem(ID_JOINGAME,MF_GRAYED);
- submenu->EnableMenuItem(ID_STOPGAME,MF_ENABLED);
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_NEWGAME);
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_JOINGAME);
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_STOPGAME,FALSE);
- pView->GetDlgItem(IDC_BUTTON_SHOOT)->EnableWindow(FALSE);
- pPlayView->m_bCanHit=FALSE;
- pDoc->m_bPlayer=CLIENT;
- strTemp="0"+pDoc->m_strClient;
- pDoc->m_pClientSocket->Send(strTemp,strTemp.GetLength());
- }
- else{
- pDoc->m_pClientSocket->ShutDown();
- pDoc->m_pClientSocket->Close();
-
- AfxMessageBox("无法连接服务器");
- }
- }
- }
- void CMainFrame::OnStopgame()
- {
- CBilliardsFormView *pView = (CBilliardsFormView *)m_wndSplitter.GetPane(0,1);
- CBilliardsDoc *pDoc=pView->GetDocument();
-
- pDoc->m_pClientSocket->Send("3",1);
- if(pDoc->m_bPlayer==SERVER){
- pDoc->m_pListenSocket->ShutDown();
- pDoc->m_pListenSocket->Close();
- pDoc->m_pClientSocket->ShutDown();
- pDoc->m_pClientSocket->Close();
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_NEWGAME,FALSE);
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_JOINGAME,FALSE);
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_STOPGAME);
- pDoc->m_bPlayer=IDLE;
- pView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);
- }
- else{
- pDoc->m_pClientSocket->ShutDown();
- pDoc->m_pClientSocket->Close();
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_NEWGAME,FALSE);
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_JOINGAME,FALSE);
- m_wndToolBar.GetToolBarCtrl().HideButton(ID_STOPGAME);
- pDoc->m_bPlayer=IDLE;
- pView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);
- }
- }
- void CMainFrame::OnDestroy()
- {
- CBilliardsFormView *pView = (CBilliardsFormView *)m_wndSplitter.GetPane(0,1);
- CBilliardsDoc *pDoc=pView->GetDocument();
-
- pDoc->m_pClientSocket->Send("3",1);
- if(pDoc->m_bPlayer==SERVER){
- pDoc->m_pListenSocket->ShutDown();
- pDoc->m_pListenSocket->Close();
- pDoc->m_pClientSocket->ShutDown();
- pDoc->m_pClientSocket->Close();
- }
- else{
- pDoc->m_pClientSocket->ShutDown();
- pDoc->m_pClientSocket->Close();
- }
- CFrameWnd::OnDestroy();
- }
- void CMainFrame::OnMouseSelect()
- {
- CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
- pView->m_nMouseAct=MOUSE_SELECT;
-
- }
- void CMainFrame::OnUpdateMouseSelect(CCmdUI* pCmdUI)
- {
- CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
- pCmdUI->SetCheck(pView->m_nMouseAct==MOUSE_SELECT);
- }
- void CMainFrame::OnMouseSpin()
- {
- CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
- pView->m_nMouseAct=MOUSE_SPIN;
- }
- void CMainFrame::OnUpdateMouseSpin(CCmdUI* pCmdUI)
- {
- CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
- pCmdUI->SetCheck(pView->m_nMouseAct==MOUSE_SPIN);
- }
- void CMainFrame::OnMouseTranslate()
- {
- CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
- pView->m_nMouseAct=MOUSE_TRANSLATE;
- }
- void CMainFrame::OnUpdateMouseTranslate(CCmdUI* pCmdUI)
- {
- CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
- pCmdUI->SetCheck(pView->m_nMouseAct==MOUSE_TRANSLATE);
- }
- void CMainFrame::OnMouseZoom()
- {
- CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
- pView->m_nMouseAct=MOUSE_ZOOM;
- }
- void CMainFrame::OnUpdateMouseZoom(CCmdUI* pCmdUI)
- {
- CBilliardsPlayView *pView=(CBilliardsPlayView *)m_wndSplitter.GetPane(0,0);
- pCmdUI->SetCheck(pView->m_nMouseAct==MOUSE_ZOOM);
- }
- void CMainFrame::OnSoundBall()
- {
- m_bSoundBall=!m_bSoundBall;
- }
- void CMainFrame::OnUpdateSoundBall(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bSoundBall);
-
- }
- void CMainFrame::OnSoundCush()
- {
- m_bSoundCush=!m_bSoundCush;
- }
- void CMainFrame::OnUpdateSoundCush(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bSoundCush);
- }
- void CMainFrame::OnSoundGoal()
- {
- m_bSoundGoal=!m_bSoundGoal;
- }
- void CMainFrame::OnUpdateSoundGoal(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bSoundGoal);
- }
- void CMainFrame::OnSoundMsg()
- {
- m_bSoundMsg=!m_bSoundMsg;
- }
- void CMainFrame::OnUpdateSoundMsg(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bSoundMsg);
- }
- BOOL CMainFrame::OnEraseBkgnd(CDC* pDC)
- {
- pDC->SetBkColor(RGB(255,255,255));
- return CFrameWnd::OnEraseBkgnd(pDC);
- }