⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 一个小游戏五子棋的浙江大学开发我的课程设计
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "MyGame.h"

#include "MyGameDoc.h"
#include "MainFrm.h"
#include "Enter.h"
#include "DlgEnterRoom.h"
#include "MyGameView.h"
#include "MyGameView1.h"
#include <string>
using namespace std;


#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_ENTER, OnEnter)
	ON_COMMAND(ID_ABOUT, OnAbout)
	ON_COMMAND(ID_START, OnStart)
	ON_COMMAND(ID_END, OnExit)
	ON_WM_TIMER()
	ON_UPDATE_COMMAND_UI(ID_ENTER, OnUpdateEnter)
	ON_UPDATE_COMMAND_UI(ID_REFRESH, OnUpdateRefresh)
	ON_UPDATE_COMMAND_UI(ID_START, OnUpdateStart)
	ON_UPDATE_COMMAND_UI(ID_END, OnUpdateEnd)
	ON_WM_DESTROY()
	//}}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()
{
	// TODO: add member initialization code here
	m_bConnected=false;
	m_bEnteredRoom=false;
}

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 toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	SetTimer(1,3000,NULL);
	return 0;
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
	CCreateContext* pContext)
{
	CRect cr;
	GetClientRect(&cr);

	CSize paneSize( 200, 550 );

	int rc;
	m_wndSplitter.CreateStatic( this, 1, 2 );

	rc = m_wndSplitter.CreateView(0, 0, 
			RUNTIME_CLASS(CMyGameView),
			paneSize, pContext);

	if( FALSE == rc )
		return rc;
	rc = m_wndSplitter.CreateView(0, 1, 
			RUNTIME_CLASS(CMyGameView1),
			paneSize, pContext);

	return rc;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style &= ~WS_MAXIMIZEBOX;
	cs.x = 0;
	cs.y = 0;
	cs.cx = 800;
	cs.cy = 550;

	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::OnEnter() 
{
	// TODO: Add your command handler code here
	CEnter dialog;
	if( dialog.DoModal() == IDCANCEL)
		return;
	CMyGameDoc *pDoc;
	pDoc = (CMyGameDoc*)GetActiveDocument();
	if( !pDoc->m_client.Connect(string(dialog.m_address)) )
	{
		AfxMessageBox("无法连接到服务器!");
		return;
	}
	if( !pDoc->m_client.Register( string( LPCTSTR(dialog.m_name)) ) )
	{
		AfxMessageBox("无法登陆到服务器。可能系统负荷过高,请稍后再试");
		return;
	}
	m_bConnected=true;
	return;
	
}

void CMainFrame::OnAbout() 
{
	// TODO: Add your command handler code here
	CDialog dialog(IDD_DIALOG1);
	dialog.DoModal();
	
}

void CMainFrame::OnStart() 
{
	// TODO: Add your command handler code here
	CDlgEnterRoom dialog;
	if( dialog.DoModal() == IDCANCEL)
		return;
	CMyGameDoc *pDoc;
	pDoc = (CMyGameDoc*)GetActiveDocument();
	//if(!pDoc->m_client.IsMyTurn())
	//	return;
	if(!pDoc->m_client.EnterRoom(dialog.m_num))
	{
		AfxMessageBox("游戏室已满,请换一间再试");
		return;
	}
	m_bEnteredRoom=true;
	return;


	
	
}

void CMainFrame::OnExit() 
{
	// TODO: Add your command handler code here
	if( AfxMessageBox("确实想退出游戏室吗?",MB_OKCANCEL) != IDOK )
		return;
	CMyGameDoc *pDoc;
	pDoc = (CMyGameDoc*)GetActiveDocument();
	//if(!pDoc->m_client.IsMyTurn())
	//	return;
	if(!pDoc->m_client.ExitRoom())
	{
		AfxMessageBox("关门了,以后不要玩得太晚!");
		return;
	}
	AfxMessageBox("欢迎下次再来!");
	m_bEnteredRoom=false;
	return;

	
}

void CMainFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(!m_bConnected)
		return;
	CMyGameDoc *pDoc = (CMyGameDoc*)GetActiveDocument();

	if(pDoc->m_client.IsNetFree())
		pDoc->m_client.GetAllUserInfo();
	if(m_bEnteredRoom && pDoc->m_client.IsNetFree())
		pDoc->m_client.QueryBoard();
	pDoc->UpdateAllViews(NULL);
	
	CFrameWnd::OnTimer(nIDEvent);
}

void CMainFrame::OnUpdateEnter(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(!m_bConnected);
}

void CMainFrame::OnUpdateRefresh(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
}

void CMainFrame::OnUpdateStart(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_bConnected && !m_bEnteredRoom);
}

void CMainFrame::OnUpdateEnd(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_bConnected && m_bEnteredRoom);
	
}

void CMainFrame::OnDestroy() 
{
	CFrameWnd::OnDestroy();
	
	// TODO: Add your message handler code here
	if(!m_bConnected)
		return;
	CMyGameDoc *pDoc = (CMyGameDoc*)GetActiveDocument();
	if(m_bEnteredRoom)
		pDoc->m_client.ExitRoom();
	pDoc->m_client.Logout();
	pDoc->m_client.Disconnect();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -