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

📄 weiqiclientview.cpp

📁 网络围棋对战的客户端
💻 CPP
字号:
// WeiQiClientView.cpp : implementation of the CWeiQiClientView class
//

#include "stdafx.h"
#include "WeiQiClient.h"

#include "WeiQiClientDoc.h"
#include "WeiQiClientView.h"
#include "Client.h"
#include "WeiQi.h"
#include "CreateDlg.h"
#include "GameListDlg.h"
#include "Top10Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CWeiQiClientView

IMPLEMENT_DYNCREATE(CWeiQiClientView, CView)

BEGIN_MESSAGE_MAP(CWeiQiClientView, CView)
	//{{AFX_MSG_MAP(CWeiQiClientView)
	ON_BN_CLICKED(IDC_BUTTON_CREATE, CreateGame)
	ON_BN_CLICKED(IDC_BUTTON_LIST, ListGame)
	ON_BN_CLICKED(IDC_BUTTON_TOP, Top)
	ON_BN_CLICKED(IDC_BUTTON_EXITGAME, ExitGame)
	ON_BN_CLICKED(IDC_BUTTON_SEND, SendMessage)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, Exit)
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWeiQiClientView construction/destruction

CWeiQiClientView::CWeiQiClientView()
{
	// TODO: add construction code here
	m_BKBmp=NULL;
	m_BWBmp=NULL;
	m_BlackBmp=NULL;
	m_WhiteBmp=NULL;
	m_pGameListDlg=NULL;
	m_pTop10Dlg=NULL;
	m_Color=BLACK;
	m_DrawPoint=FALSE;
	m_PointX=-1;
	m_PointY=-1;
}

CWeiQiClientView::~CWeiQiClientView()
{
	if(m_pGameListDlg) delete m_pGameListDlg;
	if(m_pTop10Dlg) delete m_pTop10Dlg;
}

/////////////////////////////////////////////////////////////////////////////
// CWeiQiClientView drawing

void CWeiQiClientView::OnDraw(CDC* pDC)
{
	CWeiQiClientDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	//画背景
	
	pDC->BitBlt(0,0,m_Height,m_Width,&m_BKMemDC,0,0,SRCCOPY);

	//画棋子
	for(int i=0;i<19;i++)
	{
		for(int j=0;j<19;j++)
		{
			if(m_pClient->m_pWeiQi->m_Colors[i][j]==BLACK)
			{
				pDC->BitBlt(31+i*17-7,32+j*17-7,15,15,&m_BWMemDC,0,0,SRCPAINT);
				pDC->BitBlt(31+i*17-7,32+j*17-7,15,15,&m_BlackMemDC,0,0,SRCAND);
			}
			if(m_pClient->m_pWeiQi->m_Colors[i][j]==WHITE)
			{
				pDC->BitBlt(31+i*17-7,32+j*17-7,15,15,&m_BWMemDC,0,0,SRCPAINT);
				pDC->BitBlt(31+i*17-7,32+j*17-7,15,15,&m_WhiteMemDC,0,0,SRCAND);
			}
		}
	}
	if(m_DrawPoint)
	{
		CBrush Brush;
		Brush.CreateSolidBrush(RGB(200,0,0));
		CPen Pen;
		Pen.CreatePen(PS_SOLID,1,RGB(200,0,0));
		CBrush* pOldBrush = pDC->SelectObject(&Brush);
		CPen* pOldPen = pDC->SelectObject(&Pen);
		pDC->Ellipse(31+m_PointX*17-3,32+m_PointY*17-3,31+m_PointX*17+3,32+m_PointY*17+3);
		pDC->SelectObject(pOldBrush);
		pDC->SelectObject(pOldPen);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CWeiQiClientView diagnostics

#ifdef _DEBUG
void CWeiQiClientView::AssertValid() const
{
	CView::AssertValid();
}

void CWeiQiClientView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CWeiQiClientDoc* CWeiQiClientView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWeiQiClientDoc)));
	return (CWeiQiClientDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CWeiQiClientView message handlers

int CWeiQiClientView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here

	//读取图象信息
	HBITMAP hBmp;
	hBmp=(HBITMAP)::LoadImage(NULL,"BK.bmp",IMAGE_BITMAP,0,0,
				LR_LOADFROMFILE|LR_CREATEDIBSECTION);
	m_BKBmp=CBitmap::FromHandle(hBmp);

	hBmp=(HBITMAP)::LoadImage(NULL,"BW.bmp",IMAGE_BITMAP,0,0,
				LR_LOADFROMFILE|LR_CREATEDIBSECTION);
	m_BWBmp=CBitmap::FromHandle(hBmp);

	hBmp=(HBITMAP)::LoadImage(NULL,"Black.bmp",IMAGE_BITMAP,0,0,
				LR_LOADFROMFILE|LR_CREATEDIBSECTION);
	m_BlackBmp=CBitmap::FromHandle(hBmp);

	hBmp=(HBITMAP)::LoadImage(NULL,"White.bmp",IMAGE_BITMAP,0,0,
				LR_LOADFROMFILE|LR_CREATEDIBSECTION);
	m_WhiteBmp=CBitmap::FromHandle(hBmp);

	if(m_BKBmp==NULL||m_BWBmp==NULL||m_BlackBmp==NULL||m_WhiteBmp==NULL)
	{
		MessageBox(_T("读取图象失败"));
		return -1;
	}
	BITMAP Info;
	m_BKBmp->GetBitmap(&Info);
	m_Height=Info.bmHeight;
	m_Width=Info.bmWidth;
	CDC* pDC=GetDC();
	m_BKMemDC.CreateCompatibleDC(pDC);
	m_BWMemDC.CreateCompatibleDC(pDC);
	m_BlackMemDC.CreateCompatibleDC(pDC);
	m_WhiteMemDC.CreateCompatibleDC(pDC);
	ReleaseDC(pDC);
	m_BKMemDC.SelectObject(m_BKBmp);
	m_BWMemDC.SelectObject(m_BWBmp);
	m_BlackMemDC.SelectObject(m_BlackBmp);
	m_WhiteMemDC.SelectObject(m_WhiteBmp);
	
	CRect Rect(0,0,0,0);
	m_ListButton.LoadButtonBmp(IDB_BITMAPLIST1,IDB_BITMAPLIST2,IDB_BITMAPLIST3,IDB_BITMAPLISTBK);
	m_ListButton.Create(_T("列出游戏"),BS_OWNERDRAW|WS_VISIBLE|WS_CHILD,Rect,this,IDC_BUTTON_LIST);

	m_CreateButton.LoadButtonBmp(IDB_BITMAPCREATE1,IDB_BITMAPCREATE2,IDB_BITMAPCREATE3,IDB_BITMAPLISTBK);
	m_CreateButton.Create(_T("新建游戏"),BS_OWNERDRAW|WS_VISIBLE|WS_CHILD,Rect,this,IDC_BUTTON_CREATE);

	m_TopButton.LoadButtonBmp(IDB_BITMAPTOP1,IDB_BITMAPTOP2,IDB_BITMAPTOP3,IDB_BITMAPTOPBK);
	m_TopButton.Create(_T("查看排名"),BS_OWNERDRAW|WS_VISIBLE|WS_CHILD,Rect,this,IDC_BUTTON_TOP);

	m_ExitGameButton.LoadButtonBmp(IDB_BITMAPEXITGAME1,IDB_BITMAPEXITGAME2,IDB_BITMAPEXITGAME3,IDB_BITMAPEXITGAMEBK);
	m_ExitGameButton.Create(_T("退出当前游戏"),BS_OWNERDRAW|WS_VISIBLE|WS_CHILD,Rect,this,IDC_BUTTON_EXITGAME);

	m_SendButton.LoadButtonBmp(IDB_BITMAPSEND1,IDB_BITMAPSEND2,IDB_BITMAPSEND3,IDB_BITMAPSENDBK);
	m_SendButton.Create(_T("发出消息"),BS_DEFPUSHBUTTON|BS_OWNERDRAW|WS_VISIBLE|WS_CHILD,Rect,this,IDC_BUTTON_SEND);

	m_ExitButton.LoadButtonBmp(IDB_BITMAPEXIT1,IDB_BITMAPEXIT2,IDB_BITMAPEXIT3,IDB_BITMAPEXITBK);
	m_ExitButton.Create(_T("退出网络围棋"),BS_OWNERDRAW|WS_VISIBLE|WS_CHILD,Rect,this,IDC_BUTTON_EXIT);

	m_ReceiveEdit.Create(WS_CHILD|WS_BORDER|WS_VISIBLE|ES_AUTOVSCROLL|ES_READONLY|ES_MULTILINE,Rect,this,IDC_EDIT_RECEIVE);
	m_SendEdit.Create(WS_CHILD|WS_BORDER|WS_VISIBLE|ES_AUTOHSCROLL,Rect,this,IDC_EDIT_SEND);
	m_GameNameEdit.Create(WS_CHILD|WS_BORDER|WS_VISIBLE|ES_AUTOHSCROLL|ES_READONLY|ES_MULTILINE,Rect,this,IDC_EDIT_GAMENAME);
	m_Player1Edit.Create(WS_CHILD|WS_BORDER|WS_VISIBLE|ES_AUTOHSCROLL|ES_READONLY|ES_MULTILINE,Rect,this,IDC_EDIT_PLAYER1);
	m_Player2Edit.Create(WS_CHILD|WS_BORDER|WS_VISIBLE|ES_AUTOHSCROLL|ES_READONLY|ES_MULTILINE,Rect,this,IDC_EDIT_PLAYER2);

	m_Tip.Create(this);
	m_Tip.AddTool(&m_ListButton,_T("列出游戏"));
	m_Tip.AddTool(&m_CreateButton,_T("新建游戏"));
	m_Tip.AddTool(&m_TopButton,_T("查看排名"));
	m_Tip.AddTool(&m_ExitGameButton,_T("退出当前游戏"));
	m_Tip.AddTool(&m_SendButton,_T("发出消息"));
	m_Tip.AddTool(&m_ExitButton,_T("退出网络围棋"));

	TCHAR Buf[10];
	m_pClient=((CWeiQiClientApp*)AfxGetApp())->m_pClient;
	m_pClient->m_pView=this;
	m_Player1Info=m_pClient->m_Player.m_PlayerName;
	m_Player1Info+=_T("\r\n等级:");
	itoa(m_pClient->m_Player.m_Level,Buf,10);
	m_Player1Info+=Buf;
	m_Player1Info+=_T("\r\n积分:");
	itoa(m_pClient->m_Player.m_Score,Buf,10);
	m_Player1Info+=Buf;
	UpdateData(FALSE);

	return 0;
}

void CWeiQiClientView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(!m_pClient->m_GameStart)	return;
	
	if(m_Color==m_pClient->m_pWeiQi->m_Color)
	{
		int x=point.x-23;
		int y=point.y-24;
		if(x<=0||y<=0||x>=321||y>=322)
			return;
		x/=17;
		y/=17;
		
		int Temp=m_pClient->m_pWeiQi->CanPlace(x,y,m_Color);
		if(Temp==0)
		{
			MessageBox(_T("此处不能放子"));
			return;
		}
		if(Temp==2)
		{
			MessageBox(_T("此处为劫棋"));
			return;
		}
		m_pClient->m_pWeiQi->Place(x,y,m_Color);
		m_Color*=-1;
		CString pMsg;
		TCHAR Buf[10];
		pMsg=_T("___PLACE__  ");
		itoa(x,Buf,10);
		pMsg+=Buf;
		if(x<10)
			pMsg+=_T(' ');
		pMsg+=_T(' ');
		itoa(y,Buf,10);
		pMsg+=Buf;
		if(y<10)
			pMsg+=_T(' ');
		pMsg+=_T(' ');
		m_pClient->SendMsg(pMsg);
		m_DrawPoint=FALSE;
		CRgn Rgn1,Rgn2;
		Rgn1.CreateRectRgn(m_pClient->m_pWeiQi->m_MinX*17+31-8,
			m_pClient->m_pWeiQi->m_MinY*17+32-8,
			m_pClient->m_pWeiQi->m_MaxX*17+31+8,
			m_pClient->m_pWeiQi->m_MaxY*17+32+8);
		Rgn2.CreateRectRgn(m_pClient->m_pWeiQi->m_LastX*17+31-8,
			m_pClient->m_pWeiQi->m_LastY*17+32-8,
			m_pClient->m_pWeiQi->m_LastX*17+31+8,
			m_pClient->m_pWeiQi->m_LastY*17+32+8);
		Rgn1.CombineRgn(&Rgn1,&Rgn2,RGN_OR);
		InvalidateRgn(&Rgn1);
	}

	CView::OnLButtonDown(nFlags, point);
}

void CWeiQiClientView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	m_CreateButton.MoveWindow(370,10,34,34);
	m_ListButton.MoveWindow(415,10,34,34);
	m_TopButton.MoveWindow(370,55,34,34);
	m_ExitGameButton.MoveWindow(415,55,34,34);
	m_SendButton.MoveWindow(370,370,34,34);
	m_ExitButton.MoveWindow(415,370,34,34);

	m_ReceiveEdit.MoveWindow(0,364,363,40);
	m_SendEdit.MoveWindow(0,404,363,20);
	m_GameNameEdit.MoveWindow(370,130,85,20);
	m_Player1Edit.MoveWindow(370,195,85,52);
	m_Player2Edit.MoveWindow(370,250,85,52);
}

void CWeiQiClientView::CreateGame()
{
	if(m_pClient->m_GameStart)
	{
		MessageBox(_T("请先退出当前游戏"));
		return;
	}
	m_CreateButton.Restart();
	CCreateDlg dlg;
	if(dlg.DoModal()==IDOK)
	{
		m_pClient->m_GameName=dlg.m_GameNameEdit;
		CString Msg=_T("__CREATE__  ")+dlg.m_GameNameEdit;
		m_pClient->SendMsg(Msg);
	}
}

void CWeiQiClientView::ListGame()
{
	m_ListButton.Restart();
	if(m_pClient->m_InGame)
	{
		MessageBox(_T("请先退出当前游戏"));
		return;
	}
	m_pGameListDlg=new CGameListDlg;
	if(m_pGameListDlg->DoModal()==IDOK)
	{
		if(m_GameNameText!=m_pGameListDlg->m_Select)
		{
			CString Msg=_T("_JOINGAME_  ")+m_pGameListDlg->m_Select;
			m_pClient->SendMsg(Msg);
		}
	}
	delete m_pGameListDlg;
	m_pGameListDlg=NULL;
}

void CWeiQiClientView::Top()
{
	m_TopButton.Restart();
	m_pTop10Dlg=new CTop10Dlg;
	m_pTop10Dlg->DoModal();
	delete m_pTop10Dlg;
	m_pTop10Dlg=NULL;
}

void CWeiQiClientView::ExitGame()
{
	m_ExitButton.Restart();
	if(m_pClient->m_InGame)
	{
		m_pClient->SendMsg(_T("_EXITGAME_  ")+m_GameNameText);
		m_pClient->m_InGame=FALSE;
		m_pClient->m_GameStart=FALSE;
		m_GameNameText=_T("");
		m_ReceiveText=_T("");
		m_SendText=_T("");
	}
	else
		MessageBox(_T("你不在游戏中"));
}

void CWeiQiClientView::SendMessage()
{
	m_SendButton.Restart();
	UpdateData(TRUE);
	if(m_pClient->m_GameStart&&!m_SendText.IsEmpty())
	{
		UpdateData(TRUE);
		m_ReceiveText+=m_pClient->m_Player.m_PlayerName;
		m_ReceiveText+=_T(":");
		m_ReceiveText+=m_SendText;
		m_ReceiveText+=_T("\r\n");
		m_pClient->SendMsg(_T("__CHATMSG_  ")+m_SendText);
	}
	m_SendText="";
	UpdateData(FALSE);
	int n=m_ReceiveEdit.GetLineCount();
	m_ReceiveEdit.LineScroll(n);
}

void CWeiQiClientView::Exit()
{
	AfxGetMainWnd()->DestroyWindow();
}

BOOL CWeiQiClientView::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	m_Tip.RelayEvent(pMsg);
	if(pMsg->message==WM_KEYDOWN&&pMsg->wParam==13)
	{
		SendMessage();
		return TRUE;
	}

	return CView::PreTranslateMessage(pMsg);
}

void CWeiQiClientView::DoDataExchange(CDataExchange* pDX) 
{
	// TODO: Add your specialized code here and/or call the base class
	//{{AFX_DATA_MAP(CLogInDlg)
	DDX_Text(pDX, IDC_EDIT_PLAYER1, m_Player1Info);
	DDX_Text(pDX, IDC_EDIT_PLAYER2, m_Player2Info);
	DDX_Text(pDX, IDC_EDIT_RECEIVE, m_ReceiveText);
	DDX_Text(pDX, IDC_EDIT_GAMENAME, m_GameNameText);
	DDX_Text(pDX, IDC_EDIT_SEND, m_SendText);
	//}}AFX_DATA_MAP
	CView::DoDataExchange(pDX);
}

⌨️ 快捷键说明

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