www.gusucode.com > VC++三子棋游戏源码(类似五子棋)-源码程序 > VC++三子棋游戏源码(类似五子棋)-源码程序\code\MainFrm.cpp

    //Download by http://www.NewXing.com
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "SanQi.h"

#include "MainFrm.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_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP

	//Send Chat Message Event...
	ON_MESSAGE(WM_SEND_CHATMSG, OnSendChatMessage)
	//

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{	
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);


	pGameView = NULL;
	pWebAdvertView	=NULL;
	pUserListView = NULL;
	pChatView = NULL;
	m_pGameFrame = NULL;


	try
	{
		Create(NULL, "三棋游戏", 
			WS_OVERLAPPEDWINDOW, rectDefault, NULL, NULL, NULL);
	}
	catch(...)
	{
		::AfxMessageBox("无法创建主窗口", MB_OK | MB_ICONERROR);
	}
}

CMainFrame::~CMainFrame()
{
}
/*
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
	//if(m_pGameFrame != NULL)
	{
		return TRUE;
	}

	m_wndTopBar.Create(this);

	if (NULL == m_wndSplitter.CreateStatic(this, 1, 2))
		return FALSE;
		
	m_wndSplitter.CreateView(0, 0,		//设置GameView
		RUNTIME_CLASS(CGameView),
		CSize(600,600),
		pContext); 


	RECT	rect;
	memset(&rect, 0, sizeof(RECT));
	this->GetClientRect(&rect);


	//拆分右边的栏
	if(NULL == m_wndRightSplitter.CreateStatic(&m_wndSplitter,
		2, 1,
		WS_CHILD|WS_VISIBLE,
		m_wndSplitter.IdFromRowCol(0, 1))) return FALSE;

	m_wndRightSplitter.CreateView(0, 0, RUNTIME_CLASS(CUserListView),
		CSize(250,200), pContext);

//	m_wndRightSplitter.CreateView(1, 0, RUNTIME_CLASS(CHtmlViewEx),
//		CSize(250,300), pContext);

	m_wndRightSplitter.CreateView(1, 0, RUNTIME_CLASS(CChatView),
		CSize(250,100), pContext);


	SetActiveView((CView*)m_wndRightSplitter.GetPane(0,0));
	
	pGameView = (CGameView*)m_wndSplitter.GetPane(0, 0);
	pUserListView = (CUserListView*)m_wndRightSplitter.GetPane(0, 0);
	////pWebAdvertView	=(CHtmlViewEx*)m_wndRightSplitter.GetPane(1, 0);
	pChatView	=(CChatView*)m_wndRightSplitter.GetPane(1, 0);

	return TRUE;	
	return CFrameWnd::OnCreateClient(lpcs, pContext);
}
*/
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	this->ModifyStyleEx(WS_EX_OVERLAPPEDWINDOW , 0, 0);
/*
	LONG lWnd = GetWindowLong(this->m_hWnd,GWL_STYLE);
//	lWnd &= ~(WS_MINIMIZEBOX);
	lWnd &= ~(WS_MAXIMIZEBOX);	
	lWnd = SetWindowLong(this->m_hWnd, GWL_STYLE, lWnd);
//*/

	// Add "About..." menu item to system menu.
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		//Delete "Restore","Move","Size" menu
		pSysMenu->DeleteMenu(0,MF_BYPOSITION);		
		pSysMenu->DeleteMenu(0,MF_BYPOSITION);
		pSysMenu->DeleteMenu(0,MF_BYPOSITION);		
		////


		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	//Set Icon
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	//

	m_wndTopBar.Create(this);
	
	RECT	rect;
	memset(&rect, 0, sizeof(RECT));
	m_pGameFrame	=new CGameFrame(this, rect);	

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
//	cs.style &= ~WS_MAXIMIZEBOX;	
	
	return CFrameWnd::PreCreateWindow(cs);
}

LRESULT CMainFrame::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(message == WM_SYSCOMMAND)
	{
		UINT nID = (UINT)wParam;
		int xPos = LOWORD(lParam);
		int yPos = HIWORD(lParam);

		if ((nID & 0xFFF0) == IDM_ABOUTBOX)	//About menu
		{
			CAboutDlg dlgAbout;
			dlgAbout.DoModal();
		}		
		else if(nID == SC_RESTORE+2)	//Double Click Window Frame
		{
			return FALSE;
		}
	}

	return CFrameWnd::DefWindowProc(message, wParam, lParam);
}
/////////////////////////////////////////////////////////////////////////////
// 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::OnSize(UINT nType, int cx, int cy) 
{
//	if(pGameView == NULL) return;

	CFrameWnd::OnSize(nType, cx, cy);
	
	{	
		CSplitterWnd*	pWnd	=&m_wndSplitter;
		SAFE_CHECKWND(pWnd)
		{
			RECT	rect;
			memset(&rect, 0, sizeof(RECT));
			this->GetClientRect(&rect);

			int nLeftWidth = (rect.right/4)*3-20;
			int nRightWidth = (rect.right/4)+20;
			
			pWnd->SetColumnInfo(0, nLeftWidth, 50);
			pWnd->SetColumnInfo(1, nRightWidth, 50);
			pWnd->RecalcLayout();
		}

		pWnd = &m_wndRightSplitter;
		SAFE_CHECKWND(pWnd)
		{
			RECT	rect;
			memset(&rect, 0, sizeof(RECT));
			this->GetClientRect(&rect);

			pWnd->SetColumnInfo(0, rect.bottom/3, 50);
			pWnd->SetColumnInfo(0, rect.right/3*2, 50);
			pWnd->RecalcLayout();
		}
		{
		}
	}

	const int	nReBarHeight	=50;

///*
	SAFE_CHECKWND(m_pGameFrame)
	{
		RECT	rect;
		memset(&rect, 0, sizeof(RECT));
		this->GetClientRect(&rect);
		rect.top			=nReBarHeight;

		m_pGameFrame->MoveWindow(&rect, TRUE);
		m_pGameFrame->OnSize(0,nType,cx);
		m_pGameFrame->Invalidate();
	}
//*/
	{
		CWnd*	pWnd	=&m_wndTopBar;
		SAFE_CHECKWND(pWnd)
		{
			RECT	rect;
			memset(&rect, 0, sizeof(RECT));
			this->GetClientRect(&rect);
			rect.bottom			=nReBarHeight;

			pWnd->MoveWindow(&rect, TRUE);
		}
	}

	return;
}

BOOL CMainFrame::OnEraseBkgnd(CDC* pDC) 
{
	return TRUE;

	return CFrameWnd::OnEraseBkgnd(pDC);
}

void CMainFrame::OnClose() 
{
	if(m_pGameFrame != NULL)
	{		
		CGameView *pView = (CGameView *)m_pGameFrame->GetView(0);
		if(pView !=NULL && pView->IsInGame())
		{
			CString strComment = (CString)"游戏进行中, 你真的要强行退出吗?";			
			int code = MessageBox(strComment,"确认退出",MB_DEFBUTTON2|MB_YESNO|MB_ICONQUESTION); 
			if(code == IDNO) return;
		}
	}
	
	CFrameWnd::OnClose();
}

CView *CMainFrame::GetView(int nView)
{
	CView *pView = NULL;

/*
	switch(nView)
	{
		case 0:
		{
			pView = pGameView;

			break;
		}

		case 1:
		{
			pView = pWebAdvertView;
			break;
		}

		case 2:
		{
			pView = pChatView;

			break;
		}
	}
*/

	return pView;
}

LRESULT CMainFrame::OnSendChatMessage(WPARAM wParam, LPARAM lParam)
{
	char *szChatMessage = (char *)wParam;

	return 0;
}

void CMainFrame::ShowChatMessage(CString strSender,CString strChatMessage)
{
	SAFE_CHECKWND(m_pGameFrame)
	{
		CChatView *pChatView = (CChatView *)m_pGameFrame->GetView(2);
		pChatView->ShowChatMessage(strSender,strChatMessage);
	}
}