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

📄 chessview.cpp

📁 VisualC++通信编程工程实例精解 Chapter 2 Example 1 MSCOMM控件编程实例 Example 2 基于Windows API的虚拟终端实现 Example 3
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	return 0;
}

LRESULT CChessView::OnStart(WPARAM wParam, LPARAM lParam)
{
	CString s((char*)lParam);
	CPoint point;
	CSize size(25, 25);

	int m = s.Find('M');
	m_iChessmanOp = TransChessman(atoi(s.Left(m)));
	
	int t = s.Find('T');
	point.x = atoi(s.Mid(m+1, t-m-1));
	point.x = END_X-point.x+START_X;

	int r = s.Find('\r');
	point.y = atoi(s.Mid(t+1, r-t-1));
	point.y = END_Y -point.y+START_Y;

	CClientDC dc(this);
    OnPrepareDC(&dc);
	
	CRgn circle[16];
	GetChessmanRgn(circle, (SIDE)g_side);
	int count;
	if((count=IsPtInChessman(circle, point)) != -1)
	{
		if(g_side == RED_SIDE)
		{
			CPoint p1 = m_chessArray1.GetPointOrg(count);
			p1 -= size;
			CRect r(p1, m_sizeEllipse);
			r.InflateRect(2, 2);
			m_chessArray1.SetChessmanPoint(count, CPoint(-100, -100));
			InvalidateRect(r);
		}
		else
		{
			CPoint p1 = m_chessArray2.GetPointOrg(count);
			p1 -= size;
			CRect r(p1, m_sizeEllipse);
			r.InflateRect(2, 2);
			m_chessArray2.SetChessmanPoint(count, CPoint(-100, -100));
			InvalidateRect(r);
		}
	}

	if(g_side == RED_SIDE)
	{
		CPoint point2 = m_chessArray2.GetPointOrg(m_iChessmanOp);
		m_chessArray2.SetChessmanPoint(m_iChessmanOp, point);
		point2 -= size;
		CRect rect(point2, m_sizeEllipse);
		dc.LPtoDP(rect);
		rect.InflateRect(2, 2);
		InvalidateRect(rect, TRUE);
		
		point -= size;
		CRect rect2(point, m_sizeEllipse);
		dc.LPtoDP(rect2);
		rect2.InflateRect(2, 2);
		InvalidateRect(rect2, TRUE);

		
	}
	else
	{
		CPoint point2 = m_chessArray1.GetPointOrg(m_iChessmanOp);
		m_chessArray1.SetChessmanPoint(m_iChessmanOp, point);
		point2 -= size;
		CRect rect(point2, m_sizeEllipse);
		dc.LPtoDP(rect);
		rect.InflateRect(2, 2);
		InvalidateRect(rect, TRUE);
		
		point -= size;
		CRect rect2(point, m_sizeEllipse);
		dc.LPtoDP(rect2);
		rect2.InflateRect(2, 2);
		InvalidateRect(rect2, TRUE);
	}

	g_time = CTimeSpan(0, 0, 5, 0);
	g_bEnabled = TRUE;
	return 0;
}


	
LRESULT CChessView::OnConnected(WPARAM wParam, LPARAM lParam)
{
	char *p = strchr((char*)lParam, '1');
	if(p)
		g_side = WHITE_SIDE;
	else
		g_side = RED_SIDE;
	
	if(g_side == RED_SIDE)
		g_bEnabled = TRUE;
	//	g_csStatus.Lock();
	
	SetSide();
	Invalidate();
	return 0;
}

BOOL CChessView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

void CChessView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
{
	// TODO: Add your specialized code here and/or call the base class
  //  m_dibFile.SetSystemPalette(pDC);

	CView::OnPrepareDC(pDC, pInfo);
}
void CChessView::GetChessmanRgn(CRgn *rgn, SIDE enumSide)
{
	CRect rectArray[16];
	CPoint pointMan;
	CSize size(25, 25);

	CClientDC dc(this);
    OnPrepareDC(&dc);
  
	if(enumSide == RED_SIDE)
	{
		for(int i=0; i<16; i++)
		{
			pointMan = m_chessArray1.GetPointOrg(i) - size;
			rectArray[i] = CRect(pointMan, m_sizeEllipse);
			dc.LPtoDP(rectArray[i]); // Now it's in device coordinates
			(rgn+i)->CreateEllipticRgnIndirect(rectArray[i]);
		}
	}
	else
	{
		for(int i=0; i<16; i++)
		{
			pointMan = m_chessArray2.GetPointOrg(i) - size;
			rectArray[i] = CRect(pointMan, m_sizeEllipse);
			dc.LPtoDP(rectArray[i]); // Now it's in device coordinates
			(rgn+i)->CreateEllipticRgnIndirect(rectArray[i]);
		}
	}
}
 
int CChessView::IsPtInChessman(CRgn *rgn, CPoint point)
{
	int count = -1;
	for(int i=0; i<16; i++)
	{
		if((rgn+i)->PtInRegion(point))
		{
			count = i;
			break;
		}
	}
	return count;
}

void CChessView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(!g_bEnabled)
		return;

	CRgn circle[16];
	GetChessmanRgn(circle, g_side);
	m_iChessman = IsPtInChessman(circle, point);
	if(m_iChessman!=-1 && (circle+m_iChessman)->PtInRegion(point))
	{
		CClientDC dc(this);
		OnPrepareDC(&dc);
		
        SetCapture();
        m_bCaptured = TRUE;
		CSize size(25, 25);
		if(g_side == RED_SIDE)
		{
			m_pointTopLeft = m_chessArray1.GetPointOrg(m_iChessman)-size;
			m_pointArray.SetValue(m_chessArray1, m_chessArray2);
		}
		else
		{
			m_pointTopLeft = m_chessArray2.GetPointOrg(m_iChessman)-size;
			m_pointArray.SetValue(m_chessArray2, m_chessArray1);
		}

        CPoint pointTopLeft(m_pointTopLeft);
        dc.LPtoDP(&pointTopLeft);
        m_sizeOffset = point - pointTopLeft; // device coordinates
        // New mouse cursor is active while mouse is captured
        ::SetCursor(::LoadCursor(NULL, IDC_CROSS));
	}

	CView::OnLButtonDown(nFlags, point);
}

void CChessView::MyInvalidate(CPoint point)
{
	CSize size(25, 25);
	point -= size;
	CRect rect(point, m_sizeEllipse);
	rect.InflateRect(2, 2);
	InvalidateRect(rect);
}

void CChessView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (m_bCaptured)
	{
        ::ReleaseCapture();
        m_bCaptured = FALSE;
		g_bEnabled = FALSE;
		BOOL flag;
		CPoint man;
		if(g_side == RED_SIDE)
		{
			man = m_chessArray1.GetPointOrg(m_iChessman);
			CPoint oldman = man;
			flag = m_pointArray.IsPointCorrect(m_iChessman, man);
			m_chessArray1.SetChessmanPoint(m_iChessman, man);
			MyInvalidate(oldman);
			MyInvalidate(man);
		}
		else
		{
			man = m_chessArray2.GetPointOrg(m_iChessman);
			CPoint oldman = man;
			flag = m_pointArray.IsPointCorrect(m_iChessman, man);
			m_chessArray2.SetChessmanPoint(m_iChessman, man);
			MyInvalidate(oldman);
			MyInvalidate(man);
		}
		if(flag)
		{
			CDC *pDC = GetDC();
			
			if(g_side == RED_SIDE)
			{
				if(m_iChessmanOp != -1){
					CPoint splash = m_chessArray2.GetPointOrg(m_iChessmanOp);
				m_iChessmanOp = -1;
				//COLORREF color = pDC->GetPixel(splash.x+23, splash.y);
				if(!signal)
				{
					signal = !signal;
					MyInvertRgn(pDC, splash);}
				MyInvalidate(splash);}
			}
			else
			{
				if(m_iChessmanOp != -1){
					CPoint splash = m_chessArray1.GetPointOrg(m_iChessmanOp);
				m_iChessmanOp = -1;
				//COLORREF color = pDC->GetPixel(splash.x+23, splash.y);
				if(!signal)
				{signal = !signal; MyInvertRgn(pDC, splash);}
				MyInvalidate(splash);}
			}
			g_stringInfo.Format("%d,M%d,T%d\r\n", m_iChessman, man.x, man.y);
	
			CRgn circle[16];
			GetChessmanRgn(circle, (SIDE)!g_side);
			int count;
			if((count=IsPtInChessman(circle, man)) != -1)
			{
				CSize size(25, 25);
				if(g_side == RED_SIDE)
				{
					CPoint p1 = m_chessArray2.GetPointOrg(count);
					p1 -= size;
					CRect r(p1, m_sizeEllipse);
					r.InflateRect(2, 2);
					m_chessArray2.SetChessmanPoint(count, CPoint(-100, -100));
					InvalidateRect(r);
				}
				else
				{
					CPoint p1 = m_chessArray1.GetPointOrg(count);
					p1 -= size;
					CRect r(p1, m_sizeEllipse);
					r.InflateRect(2, 2);
					m_chessArray1.SetChessmanPoint(count, CPoint(-100, -100));
					InvalidateRect(r);
				}
			}
		
			g_eventStart.SetEvent();
		}
		else 
			g_bEnabled = TRUE;
    }

	CView::OnLButtonUp(nFlags, point);
}

void CChessView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (m_bCaptured) 
	{
        CClientDC dc(this);
        OnPrepareDC(&dc);
		CSize size(25, 25);
        CRect rectOld(m_pointTopLeft, m_sizeEllipse);
      
		m_pointTopLeft = point - m_sizeOffset;
        dc.DPtoLP(&m_pointTopLeft);

		if(g_side)
			m_chessArray1.SetChessmanPoint(m_iChessman, m_pointTopLeft+size);
		else
			m_chessArray2.SetChessmanPoint(m_iChessman, m_pointTopLeft+size);
		
		dc.LPtoDP(rectOld);
		rectOld.InflateRect(2, 2);
		InvalidateRect(rectOld, TRUE);

        CRect rectNew(m_pointTopLeft, m_sizeEllipse);

        dc.LPtoDP(rectNew);
        rectNew.InflateRect(2,2);
		InvalidateRect(rectNew, TRUE);
		
    }

	CView::OnMouseMove(nFlags, point);
}


void CChessView::PreSubclassWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CView::PreSubclassWindow();
}

void CChessView::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CView::OnShowWindow(bShow, nStatus);
	
	// TODO: Add your message handler code here
	
}

void CChessView::OnTimer(UINT nIDEvent) 
{
	if(nIDEvent == 1)
	{
		g_time -= CTimeSpan(0, 0, 0, 1);
		TRACE("g_time:%s", g_time.Format("%M:%S"));
	}
	if(nIDEvent == 2)
	{
		if(m_iChessmanOp != -1)
		{
			CPoint p;
			CDC *pDC = GetDC();
			CSize size(25, 25);
			signal = !signal;
			if(g_side == RED_SIDE)
			{
				p = m_chessArray2.GetPointOrg(m_iChessmanOp);
				MyInvertRgn(pDC, p);
			}
			else
			{
				p = m_chessArray1.GetPointOrg(m_iChessmanOp);
				MyInvertRgn(pDC, p);
			}
		}
	}
	
	CView::OnTimer(nIDEvent);
}

void CChessView::MyInvertRgn(CDC *pDC, CPoint point)
{
	CSize size(25, 25);
	point -= size;
	CRect rect(point, m_sizeEllipse);
	CRgn c;
	c.CreateEllipticRgnIndirect(rect);
	pDC->InvertRgn(&c);
}

⌨️ 快捷键说明

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