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

📄 mainfrm.cpp

📁 传2机器人源代码VC
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "MirRobot.h"

#include "MainFrm.h"
#include "LoginDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame


IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_SETFOCUS()
	ON_COMMAND(ID_VIEW_CHAT_WND, OnViewChatWnd)
	ON_UPDATE_COMMAND_UI(ID_VIEW_CHAT_WND, OnUpdateViewChatWnd)
	ON_COMMAND(ID_LOGIN_GAME, OnLoginGame)
	ON_UPDATE_COMMAND_UI(ID_LOGIN_GAME, OnUpdateLoginGame)
	ON_COMMAND(ID_RUN_UP, OnRunUp)
	ON_COMMAND(ID_RUN_DOWN, OnRunDown)
	ON_COMMAND(ID_RUN_RIGHT, OnRunRight)
	ON_COMMAND(ID_RUN_LEFT, OnRunLeft)
	ON_COMMAND(ID_SEND_CHAT_MESSAGE, OnSendChatMessage)
	ON_COMMAND(ID_WALK_UP, OnWalkUp)
	ON_COMMAND(ID_WALK_DOWN, OnWalkDown)
	ON_COMMAND(ID_WALK_LEFT, OnWalkLeft)
	ON_COMMAND(ID_WALK_RIGHT, OnWalkRight)
	ON_WM_TIMER()
	ON_MESSAGE(WM_LOG_SYSTEM,OnLogSystem)
	ON_MESSAGE(WM_LOGOUT,OnLogout)
	ON_MESSAGE(WM_SET_GAME_MAP,OnSetGameMap)
	ON_MESSAGE(WM_SET_PERSON_INFO,OnSetPersonInfo)
	ON_COMMAND(ID_MOVE_TO, OnMoveTo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_SEPARATOR,           // status line indicator
	ID_SEPARATOR,           // status line indicator
	ID_SEPARATOR,           // status line indicator
	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_pGameThread=NULL;
	m_RetryTime=0;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	CString s;
	CPoint pnt;
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	// create a view to occupy the client area of the frame
	if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
		CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
	{
		TRACE0("Failed to create view window\n");
		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
	}

	if (!m_wndChat.Create("消息",this,12))
    {
        TRACE0("Failed to create mybar\n");
        return -1;      // fail to create
	}

	if (!m_wndInput.Create("聊天",this,12))
    {
        TRACE0("Failed to create mybar\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);

	m_wndChat.SetBarStyle(m_wndChat.GetBarStyle()|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC);
	m_wndChat.EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndChat, AFX_IDW_DOCKBAR_BOTTOM);

	m_wndInput.SetBarStyle(m_wndInput.GetBarStyle()|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC);
	m_wndInput.EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndInput, AFX_IDW_DOCKBAR_BOTTOM);
#ifdef MIR_SF_CLIENT
	GetWindowText(s);
	SetWindowText(s+" - 私服版本");
#endif
	//0    334  266
	s="0";
	::SendMessage(AfxGetMainWnd()->m_hWnd,WM_SET_GAME_MAP,NULL,(LPARAM)&s);
	pnt.x=334;
	pnt.y=266;
	::SendMessage(AfxGetMainWnd()->m_hWnd,WM_SET_PERSON_INFO,NULL,(LPARAM)&pnt);
	m_GameMap.SetGameMap("0",334,266);
	m_wndStatusBar.SetPaneInfo(1,0,0,50);
	m_wndStatusBar.SetPaneInfo(2,0,0,80);
	m_wndStatusBar.SetPaneInfo(3,0,0,150);

	SetTimer(816,1500,NULL);
	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 &= ~WS_EX_CLIENTEDGE;
	cs.lpszClass = AfxRegisterWndClass(0);
	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::OnSetFocus(CWnd* pOldWnd)
{
	// forward focus to the view window
	m_wndView.SetFocus();
}

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
	// let the view have first crack at the command
	if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
		return TRUE;

	// otherwise, do default handling
	return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

void CMainFrame::OnViewChatWnd() 
{
	// TODO: Add your command handler code here
	BOOL bShow = m_wndChat.IsVisible();
	ShowControlBar(&m_wndChat, !bShow, FALSE);
}

void CMainFrame::OnUpdateViewChatWnd(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_wndChat.IsVisible());
}

void CMainFrame::OnLoginGame() 
{
	if(m_pGameThread==NULL)
		m_pGameThread=AfxBeginThread(RUNTIME_CLASS(CGameThread));
}

afx_msg LRESULT CMainFrame::OnLogSystem(WPARAM w,LPARAM l)
{
	CString*pStr=(CString*)l;
	m_wndChat.AddString(*pStr);
	return TRUE;
}

afx_msg LRESULT CMainFrame::OnLogout(WPARAM w,LPARAM l)
{
	m_pGameThread=NULL;
	return TRUE;
}

afx_msg LRESULT CMainFrame::OnSetGameMap(WPARAM w,LPARAM l)
{
	CString*pStr=(CString*)l;
	m_GameMap.SetGameMap(*pStr,1,1);
	m_wndView.RedrawWindow();

	char buf[100];
	CString s;
	s=m_GameMap.GetMapname();
	GetPrivateProfileString("MapCaption",s,s,buf,64,".\\MirRobot.ini");
	s.Format("%s(%d,%d)",buf,m_GameMap.GetPersonInfo().x,m_GameMap.GetPersonInfo().y);
	m_wndStatusBar.SetPaneText(3,s);
	return TRUE;
}

afx_msg LRESULT CMainFrame::OnSetPersonInfo(WPARAM w,LPARAM l)
{
	char buf[100];
	CString s;

	CPoint*p=(CPoint*)l;
	m_GameMap.SetPersonInfo(p->x,p->y,0);

	m_wndView.Enter();
	if(!m_wndView.m_MovePoints.IsEmpty())
	{
		tag_MovePoint*pMP=(tag_MovePoint*)m_wndView.m_MovePoints.GetHead();
		if(m_GameMap.GetPersonInfo()!=pMP->oldPos)
		{
			//s="移动发生错误!!";
			pMP=(tag_MovePoint*)m_wndView.m_MovePoints.GetTail();
			CPoint Target(pMP->oldPos.x,pMP->oldPos.y);
			//OnLogSystem(0,(LPARAM)&s);
			while(!m_wndView.m_MovePoints.IsEmpty())
			{
				pMP=(tag_MovePoint*)m_wndView.m_MovePoints.RemoveHead();
				delete pMP;
			}
			/*if(m_RetryTime<4)
			{
				m_wndView.MoveTo(Target);
				s.Format("移动发生错误,第%d重试! 目的地:(%d,%d)",m_RetryTime,Target.x,Target.y);
				m_wndChat.AddString(s);
				m_RetryTime++;
			}
			else
			{
				m_wndChat.AddString("重试超过3次,放弃重试!");
				m_RetryTime=0;
			}*/
		}
		else if(m_GameMap.GetPersonInfo()!=pMP->oldPos)
		{
			m_RetryTime=0;
			SetTimer(815,450,NULL);
		}
		else
		{
			m_RetryTime=0;
			m_wndView.m_MovePoints.RemoveHead();
			delete pMP;
			SetTimer(815,450,NULL);
		}
	}
	m_wndView.Leave();
	s=m_GameMap.GetMapname();
	GetPrivateProfileString("MapCaption",s,s,buf,64,".\\MirRobot.ini");
	s.Format("%s(%d,%d)",buf,m_GameMap.GetPersonInfo().x,m_GameMap.GetPersonInfo().y);
	m_wndStatusBar.SetPaneText(3,s);
	return TRUE;
}

void CMainFrame::OnUpdateLoginGame(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_pGameThread==NULL)pCmdUI->Enable(TRUE);
	else pCmdUI->Enable(FALSE);
}

CMirGameMap*CMainFrame::GetGameMap()
{
	return &m_GameMap;
}

#include "GameThread.h"

void CMainFrame::OnRunUp() 
{
	OnRunDirect(0);
}

void CMainFrame::OnRunDown() 
{
	OnRunDirect(4);
}

void CMainFrame::OnRunRight() 
{
	OnRunDirect(2);
}

void CMainFrame::OnRunLeft() 
{
	OnRunDirect(6);
}

LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	RECT*pRect=(RECT*)lParam;
	tag_MonListItem*pItem;
	POSITION pos;
	CString s;
	BOOL bFlag;
	switch(message)
	{
	case WM_SB_DISAPPEAR:
		m_wndView.Enter();
		bFlag=FALSE;
		pos=m_wndView.m_MonList.GetHeadPosition();
		while(pos!=NULL /*&& !bFlag*/)
		{
			pItem=(tag_MonListItem*)m_wndView.m_MonList.GetAt(pos);
			if(pItem!=NULL)
			{
				if(pItem->id==(DWORD)lParam)
				{
					s.Format("%s消失了!",pItem->name);
					m_wndChat.AddString(s);
					m_wndView.m_MonList.RemoveAt(pos);
					delete pItem;
					bFlag=TRUE;
					break;
				}
			}
			m_wndView.m_MonList.GetNext(pos);
		}
		m_wndView.Leave();
		m_wndView.RedrawWindow();
		break;
	case WM_SB_APPEAR:
		m_wndView.Enter();
		bFlag=FALSE;
		pos=m_wndView.m_MonList.GetHeadPosition();
		while(pos!=NULL)
		{
			pItem=(tag_MonListItem*)m_wndView.m_MonList.GetNext(pos);
			if(pItem!=NULL)
			{
				if(pItem->id == (DWORD)pRect->top)
				{
					pItem->pos.x=pRect->left;
					pItem->pos.y=pRect->right;
					if(wParam==0)
						pItem->name=CString((char*)pRect->bottom);
					bFlag=TRUE;
					break;
				}
			}
		}
		if(!bFlag)
		{
			pItem=new tag_MonListItem;
			pItem->id=pRect->top;
			pItem->pos.x=pRect->left;
			pItem->pos.y=pRect->right;
			pItem->name=CString((char*)pRect->bottom);
			m_wndView.m_MonList.AddHead(pItem);
		}
		m_wndView.Leave();
		m_wndView.RedrawWindow();
		break;
	case WM_SB_DEAD:
		if(wParam==1)
		{
			pos=m_wndView.m_MonList.GetHeadPosition();
			while(pos!=NULL)
			{
				pItem=(tag_MonListItem*)m_wndView.m_MonList.GetNext(pos);
				if(pItem!=NULL)
				{
					if(pItem->id == (DWORD)lParam)
					{
						m_wndChat.AddString(pItem->name+"被杀了!");
						break;
					}
				}
			}
		}
		break;
	case WM_HP_CHANGE:
		if(wParam==1)
		{
			pos=m_wndView.m_MonList.GetHeadPosition();
			while(pos!=NULL)
			{
				pItem=(tag_MonListItem*)m_wndView.m_MonList.GetNext(pos);
				if(pItem!=NULL)
				{
					if(pItem->id == (DWORD)pRect->bottom)
					{
						s.Format(pItem->name+"被别人攻击:减少%d的生命,剩下%d的生命,总共%d的生命.",
							pRect->right,pRect->left,pRect->top);
						m_wndChat.AddString(s);
						break;
					}
				}
			}
		}
		break;
	}
	return CFrameWnd::WindowProc(message, wParam, lParam);
}

void CMainFrame::OnSendChatMessage() 
{
	// TODO: Add your command handler code here
	if(m_pGameThread!=NULL)
	{
		CString s;
		if(m_wndInput.IsActive())
		{
			s=m_wndInput.GetAndEmpty();
			::SendMessage(
				((CGameThread*)m_pGameThread)->m_pWnd->m_hWnd,
				WM_SEND_CHAT_NSG,0,(LPARAM)&s);
		}
	}
	else
	{
		OnLoginGame();
	}
}

void CMainFrame::OnWalkUp() 
{
	OnWalkDirect(0);
}

void CMainFrame::OnWalkDown() 
{
	OnWalkDirect(4);
}

void CMainFrame::OnWalkLeft() 
{
	OnWalkDirect(6);
}

void CMainFrame::OnWalkRight() 
{
	OnWalkDirect(2);
}

void CMainFrame::OnRunDirect(int d)
{
	if(m_pGameThread==NULL)return;
	::SendMessage(
		((CGameThread*)m_pGameThread)->m_pWnd->m_hWnd,
		WM_RUN_DIRECT,d%8,d%8);
}

void CMainFrame::OnWalkDirect(int d)
{
	if(m_pGameThread==NULL)return;
	::SendMessage(
		((CGameThread*)m_pGameThread)->m_pWnd->m_hWnd,
		WM_WALK_DIRECT,d%8,d%8);
}

void CMainFrame::OnTimer(UINT nIDEvent) 
{
	CString s;
	s.Format("剩余步数:%d",m_wndView.m_MovePoints.GetCount());
	m_wndStatusBar.SetPaneText(4,s);
	if(nIDEvent==815)
	{
		KillTimer(815);
		m_wndView.Enter();
		if(!m_wndView.m_MovePoints.IsEmpty())
		{
			tag_MovePoint*pMP=(tag_MovePoint*)m_wndView.m_MovePoints.GetHead();
			if(pMP!=NULL)
			{
				if(pMP->isRun)
					OnRunDirect((pMP->direct)%8);
				else
					OnWalkDirect((pMP->direct)%8);
			}

		}
		m_wndView.Leave();
	}else if(nIDEvent==815)
	{
		/*m_wndView.Enter();
		if(!m_wndView.m_MovePoints.IsEmpty())
		{
			tag_MovePoint*pMP=(tag_MovePoint*)m_wndView.m_MovePoints.GetHead();
			if(pMP!=NULL)
			{
				if(pMP->isRun)
					OnRunDirect((pMP->direct)%8);
				else
					OnWalkDirect((pMP->direct)%8);
			}

		}
		m_wndView.Leave();*/
	}
	CFrameWnd::OnTimer(nIDEvent);
}
#include "MoveToDlg.h"
void CMainFrame::OnMoveTo() 
{
	CMoveToDlg dlg;
	if(dlg.DoModal()==IDOK)
	{
		CPoint t(dlg.m_x,dlg.m_y);
		m_wndView.MoveTo(t);
	}
}

⌨️ 快捷键说明

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