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

📄 mainfrm.cpp

📁 井字棋
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "JingZiQi.h"

#include "MainFrm.h"
#include "JingZiQiView.h"
#include "SerCltDlg.h"
#include "ConnectDlg.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_SAVE, OnSave)
	ON_COMMAND(ID_LOAD, OnLoad)
	ON_UPDATE_COMMAND_UI(ID_ComputerFirst, OnUpdateComputerFirst)
	ON_COMMAND(ID_ComputerFirst, OnComputerFirst)
	ON_COMMAND(ID_Level, OnLevel)
	ON_UPDATE_COMMAND_UI(ID_Level, OnUpdateLevel)
	ON_COMMAND(ID_Regret, OnRegret)
	ON_COMMAND(ID_OnNet, OnOnNet)
	ON_COMMAND(ID_ManVsCmpt, OnManVsCmpt)
	ON_UPDATE_COMMAND_UI(ID_Regret, OnUpdateRegret)
	ON_UPDATE_COMMAND_UI(ID_LOAD, OnUpdateLoad)
	ON_UPDATE_COMMAND_UI(ID_SAVE, OnUpdateSave)
	//}}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_pClient=new CMySocket;
//	m_pConnect=new CMySocket;
//	m_pListen=new CMySocket;
	m_pClient=NULL;
	m_pConnect=NULL;
	m_pListen=NULL;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	
/*	pIntrDlg=new CIntruDlg();
	pIntrDlg->Create(IDD_DIALOG1);
	pIntrDlg->ShowWindow(SW_SHOW);
	pIntrDlg->UpdateWindow();

	Sleep(2000);
	pIntrDlg->SendMessage(WM_CLOSE);*/
	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);
	
	return 0;
}

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.dwExStyle=cs.dwExStyle|WS_EX_TOPMOST;//|WS_EX_TRANSPARENT;
	cs.style=WS_SYSMENU|WS_OVERLAPPED|WS_MINIMIZEBOX;
//	cs.style=WS_SYSMENU|WS_OVERLAPPED|WS_MAXIMIZEBOX;
	cs.cx=470;
	cs.cy=400;
	cs.x=GetSystemMetrics(SM_CXSCREEN)/4;
	cs.y=GetSystemMetrics(SM_CYSCREEN)/6;
	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::OnSave() 
{
	// TODO: Add your command handler code here
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();

	
	CFileDialog dlg(FALSE,"jzq",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
						"(*.JZQ)|*.jzq|All Files|*.*||",this);
	if(dlg.DoModal()==IDOK)
		dlg.GetFileName();
	else
		return;

	CString str;
	CStdioFile file;
	if(file.Open(dlg.GetFileName(),CFile::modeCreate|CFile::modeWrite|CFile::typeText)==0)
	{
		AfxMessageBox("Unable to save file!");
		return;
	}
	file.Write(pView->QP,9*sizeof(int));
	file.Write(pView->pre_qp,9*sizeof(int));
	file.Write(&(pView->man_turn),1);
	file.Write(&(pView->cmptfirst),1);
	file.Write(&(pView->Level),1);
	file.Write(&(pView->over),1);
	file.Close();
}	

void CMainFrame::OnLoad() 
{
	// TODO: Add your command handler code here
	//ShellExecute(NULL,NULL,_T("ReadMe.txt"),NULL,NULL,SW_SHOW);
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();

	
	CFileDialog dlg(TRUE,"jzq",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
						"(*.JZQ)|*.jzq|All Files|*.*||",this);
	if(dlg.DoModal()==IDOK)
		dlg.GetFileName();
	else
		return;

	CStdioFile file;
	if(file.Open(dlg.GetFileName(),CFile::modeRead)==0)
	{
		AfxMessageBox("Unable to open a file!");
		return;
	}
	file.Read(pView->QP,9*sizeof(int));
	file.Read(pView->pre_qp,9*sizeof(int));
	file.Read(&(pView->man_turn),1);
	file.Read(&(pView->cmptfirst),1);
	file.Read(&(pView->Level),1);
	file.Read(&(pView->over),1);
	file.Close();
	
	Invalidate();
}


void CMainFrame::OnUpdateComputerFirst(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
	
	
//	CMenu *pMenu=this->GetMenu();//->GetSubMenu(0);
//	if(pMenu->GetMenuState(ID_ComputerFirst,1))
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();
	if(pView->mode==2)
	{
		pCmdUI->Enable(FALSE);
		return;
	}
	if(pView->cmptfirst)
		pCmdUI->SetText("人 先 下");
	else
		pCmdUI->SetText("计算机先下");
}



void CMainFrame::OnComputerFirst() 
{
	// TODO: Add your command handler code here
/*	CCmdUI *pCmdUI=new CCmdUI;
	pCmdUI->Enable();*/

	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();
		
		pView->cmptfirst=!pView->cmptfirst;
		pView->man_turn=!pView->man_turn;
		pView->OnStart2();
//		pCmdUI->SetCheck(!pView->cmptfirst);

}

void CMainFrame::OnLevel() 
{
	// TODO: Add your command handler code here
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();
	if(pView->Level==1)
		pView->Level=2;
	else
		pView->Level=1;
	pView->OnStart2();
}

void CMainFrame::OnUpdateLevel(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();
	if(pView->mode==2)
	{
		pCmdUI->Enable(FALSE);
		return;
	}
	if(pView->Level==1)
		pCmdUI->SetText("难度(难)");
	else
		pCmdUI->SetText("难度(易)");	
}

void CMainFrame::OnRegret() 
{
	// TODO: Add your command handler code here
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();

		pView->over=0;
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
				pView->QP[i][j]=pView->pre_qp[i][j];
		Invalidate();
	
}

void CMainFrame::OnOnNet() 
{
	// TODO: Add your command handler code here
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();
	CSerCltDlg dlg;
//	dlg.CheckRadioButton(IDC_RADIO1,IDC_RADIO2,IDC_RADIO2);
	dlg.DoModal();
//	pView->Empty();
}

void CMainFrame::SetNetwork()
{
	//CMySocket *pSocket;
	if(m_pListen)
	{
		MessageBox("服务器已处于连接状态,无须再设定!","提醒你",MB_OK);
		return;
	}
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();
	m_pListen=new CMySocket;
	if(!m_pListen->Create(100))
	{
		AfxMessageBox("Fail to create socket!");
		return;
	}
	if(!m_pListen->Listen())
	{
		AfxMessageBox("端口设置错误!");
		return;
	}
	pView->mode=2;
    pView->over=0;
	pView->cmptfirst=0;
	MessageBox("服务器准备就绪!","设定成功",MB_OK);
	pView->Empty();
}

void CMainFrame::ConnectServer()
{
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();

	if(m_pClient)
	{
		MessageBox("已经连接到服务器了,请先断开再连接!","提醒你",MB_OK);
		return;
	}
	CMySocket *pSocket=new CMySocket;
	if(!(pSocket->Create()))
	{	
		delete pSocket;
	//	pView->mode=1;
	//	UpdateData();
		MessageBox("Fail to create socket!");
		return;
	}
	if(!(pSocket->Connect(m_Server,m_Port)))
	{
		delete pSocket;
	//	pView->mode=1;
	//	UpdateData();
		MessageBox("请求连接到服务器失败!","连接失败",MB_OK);
		
		return;
	}
	else
	{
		pView->mode=2;
		pView->over=0;
		pView->cmptfirst=0;
		m_pClient=pSocket;
		MessageBox("成功连接到服务器!","连接成功",MB_OK);
		pView->Empty();
	}
}

void CMainFrame::OnManVsCmpt() 
{
	// TODO: Add your command handler code here
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();
	if(pView->mode==2)
	{
		char pMsg[1000];
		strcpy(pMsg,"");
		sprintf(pMsg,"MSG:%s","对方已断开连接!");
		if(pView->IsServer&&m_pListen)
		{
			
		//	if(m_pListen!=NULL)
		//	{
				if(m_pConnect)
				{
					SendMsg(m_pConnect,pMsg);
					m_pConnect->ShutDown();
					delete m_pConnect;
					m_pConnect=NULL;
				}
			   m_pListen->Close();
			   delete m_pListen;
			   m_pListen=NULL;
		//	}
		}
		else if(m_pClient)
		{
			SendMsg(m_pClient,pMsg);
			if(m_pClient!=NULL)
			{
			   m_pClient->ShutDown();
			   delete m_pClient;
			   m_pClient=NULL;
			}
		}
	}
	pView->mode=1;
	pView->Empty();
}

void CMainFrame::SendMsg(CMySocket *pSocket, char *pMsg)
{
	pSocket->Send(pMsg,strlen(pMsg));
}

void CMainFrame::OnUpdateRegret(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();
	
	if(pView->mode==2)
		pCmdUI->Enable(FALSE);
	
}

void CMainFrame::OnUpdateLoad(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();
	
	if(pView->mode==2)
		pCmdUI->Enable(FALSE);
}

void CMainFrame::OnUpdateSave(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CJingZiQiView *pView=(CJingZiQiView*)GetActiveView();
	
	if(pView->mode==2)
		pCmdUI->Enable(FALSE);
}

//DEL void CMainFrame::OnTimer(UINT nIDEvent) 
//DEL {
//DEL 	// TODO: Add your message handler code here and/or call default
//DEL /*	if(nIDEvent==1)
//DEL 	{
//DEL 		if(pIntrDlg->IsWindowVisible())
//DEL 		{
//DEL 			pIntrDlg->SetActiveWindow();
//DEL 			pIntrDlg->UpdateWindow();
//DEL 			Sleep(2000);
//DEL 			pIntrDlg->SendMessage(WM_CLOSE);
//DEL 		}
//DEL 		else
//DEL 		{
//DEL 			SetActiveWindow();
//DEL 			KillTimer(1);
//DEL 		}
//DEL 	}*/
//DEL 	CFrameWnd::OnTimer(nIDEvent);
//DEL }

⌨️ 快捷键说明

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