MFC中MFC中 Doc 和 View之间的交互

今天主要学习了MFC中 Doc 和 View之间的交互。主要通过View来显示数据,Doc用来存储数据。

常用函数的说明:

①SetModifiedFlag();

       在对文档作了修改之后调用该函数。

CDocument::UpdateAllViews

void UpdateAllViews( CView* pSender ,  LPARAM lHint = 0L ,  CObject* pHint = NULL );

       文档被修改后可调用此函数,把文档被修改的信息通知给每个视图。第一个参数通常设置为NULL=

③SetMapMode

       设置映射模式。

④DPtoLP

       该函数将设备坐标转变为逻辑坐标,转变依赖于设备的图形模式,窗口和坐标的起点及范围的设置,和转换的内容。

⑤GetDocument ()

       在View类中获得指向Doc类的指针。


程序设计步骤:

①在Doc中添加数据为保护类型 :

protected:
	COLORREF m_clrCurrentColor;
	COLORREF m_clrGrid[4][4];

②将Doc中的数据封装成set和get函数用来调用:

COLORREF CSquaresDoc::GetCurrentColor()
{
	return m_clrCurrentColor;
}

COLORREF CSquaresDoc::GetSquare(int i, int j)
{
	ASSERT (i >= 0 && i <= 3 && j >= 0 && j <= 3);
	return m_clrGrid[i][j];
}

void CSquaresDoc::SetSquare(int i, int j, COLORREF color)
{
	ASSERT (i >= 0 && i <= 3 && j >= 0 && j <= 3);
	m_clrGrid[i][j] = color;
	SetModifiedFlag (TRUE);
	UpdateAllViews (NULL);
}


③在OnNewDocument中初始化数据。

BOOL CSquaresDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	for (int i=0; i<4; i++)
		for (int j=0; j<4; j++)
			m_clrGrid[i][j] = RGB (255, 255, 255);

	m_clrCurrentColor = RGB (255, 0, 0);
	return TRUE;
}


④Serialize(CArchive& ar)用来存储数据

void CSquaresDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		for (int i=0; i<4; i++)
			for (int j=0; j<4; j++)
				ar << m_clrGrid[i][j];
		ar << m_clrCurrentColor;
	}
	else
	{
		for (int i=0; i<4; i++)
			for (int j=0; j<4; j++)
				ar >> m_clrGrid[i][j];
		ar >> m_clrCurrentColor;
	}
}

⑤通过菜单响应来更换颜色:

void CSquaresDoc::OnUpdateColorBlue(CCmdUI* pCmdUI) 
{
	pCmdUI->SetRadio (m_clrCurrentColor == RGB (0, 0, 255));	
}

void CSquaresDoc::OnUpdateColorWhite(CCmdUI* pCmdUI) 
{
	pCmdUI->SetRadio (m_clrCurrentColor == RGB (255, 255, 255));	
}


在View中用来显示数据,主要在OnDraw中显示:

void CSdiSquaresView::OnDraw(CDC* pDC)
{
	CSdiSquaresDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	//MM_LOENGLISH x轴朝右是正方向,y轴朝上是正方向
	pDC->SetMapMode(MM_LOENGLISH);
		
	for (int i = 0 ; i < 4  ;i++)
	{
		for (int j = 0; j < 4 ; j++)
		{
			int x1 = 50 + i*100;
			int x2 = 50 + (i+1)*100;

			int y1 = -50 - j*100;
			int y2 = -50 - (j+1)*100;
			COLORREF color = pDoc->GetSquare(i,j);
			CBrush brush(color);
			pDC->FillRect(CRect(CPoint(x1,y1),CPoint(x2,y2)),&brush);
		}
	}

	for (int x = 50 ; x <= 450 ; x += 100  )
	{
		pDC->MoveTo(x,-50);
		pDC->LineTo(x,-450);
	}

	for (int y = -50 ; y >= -450 ; y -= 100)
	{
		pDC->MoveTo(50,y);
		pDC->LineTo(450,y);
	}
}

在OnLButtonDown中设置相关的相应数据:

void CSdiSquaresView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	
	CView::OnRButtonDown(nFlags, point);

	CClientDC dc (this);
	dc.SetMapMode (MM_LOENGLISH);
	CPoint pos = point;
	dc.DPtoLP (&pos);

	if (pos.x >= 50 && pos.x <= 450 && pos.y <= -50 && pos.y >= -450) 
	{
		int i = (pos.x - 50) / 100;
		int j = (-pos.y - 50) / 100;
		
		CSdiSquaresDoc* pDoc = GetDocument ();
		COLORREF clrCurrentColor = pDoc->GetCurrentColor ();
		pDoc->SetSquare (i,j, clrCurrentColor);
	}
}








  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值