SetModifiedFlag(TRUE); 出现chkesp.c line42错误?

rushpixy 2014-01-19 03:16:55
程序是一个4×4的格子,点击格子会变色。
学习doc/view的,点击会出错,调试时发现是 SetModifiedFlag(TRUE) 出错
完整代码:http://pan.baidu.com/s/1mgNopSG
代码如下

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

// TODO: add reinitialization code here
// (SDI documents will reuse this document)
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
m_clrGrid[i][j] = RGB(0,255,0);
}
}
m_curColor = RGB(0,0,255);
return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CSdiSquaresDoc serialization

void CSdiSquaresDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
ar<<m_clrGrid[i][j];
}
}
ar<<m_curColor;
}
else
{
// TODO: add loading code here
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
ar>>m_clrGrid[i][j];
}
}
ar>>m_curColor;
}
}

/////////////////////////////////////////////////////////////////////////////
// CSdiSquaresDoc diagnostics

#ifdef _DEBUG
void CSdiSquaresDoc::AssertValid() const
{
CDocument::AssertValid();
}

void CSdiSquaresDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}

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

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

COLORREF CSdiSquaresDoc::GetCurColor()
{
return m_curColor;
}


====================================================
void CSdiSquaresView::OnDraw(CDC* pDC)
{
CSdiSquaresDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
COLORREF color = pDoc->GetSquare(i,j);
CBrush brush(color);
int x1 = j*100+50;
int y1 = i*100+50;
int x2 = x1+100;
int y2 = y1+100;
CRect rect(x1,y1,x2,y2);
pDC->FillRect(&rect,&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);
}
}

void CSdiSquaresView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

if (point.x>50 && point.x<450 && point.y>50 && point.y<450)
{
int x=(point.x-50)/100;
int y=(point.y-50)/100;
CSdiSquaresDoc * pDoc = (CSdiSquaresDoc *)GetDC();
COLORREF color = pDoc->GetCurColor();
pDoc->SetSquare(x,y,color);
}
}


...全文
104 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rushpixy 2014-01-19
  • 打赏
  • 举报
回复
感谢,
schlafenhamster 2014-01-19
  • 打赏
  • 举报
回复
4. 索引有问题: void CSdiSquaresDoc::SetSquare(int i,int j,COLORREF color) { ASSERT(i>=0 && i<4 && j>=0 && j<4); m_clrGrid[j][i]= color; SetModifiedFlag(TRUE); UpdateAllViews(NULL); }
schlafenhamster 2014-01-19
  • 打赏
  • 举报
回复
1. CSdiSquaresDoc::CSdiSquaresDoc() { // TODO: add one-time construction code here for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { m_clrGrid[i][j] = RGB(0,255,0); } } m_curColor = RGB(0,0,255); } 2. void CSdiSquaresView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CSdiSquaresDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (point.x>50 && point.x<450 && point.y>50 && point.y<450) { int x=(point.x-50)/100; int y=(point.y-50)/100; COLORREF color = pDoc->GetCurColor(); pDoc->SetSquare(x,y,color); } CView::OnLButtonDown(nFlags, point); } 3.与 void CSdiSquaresDoc::SetSquare(int i,int j,COLORREF color) { ASSERT(i>=0 && i<4 && j>=0 && j<4); m_clrGrid[i][j]= color; SetModifiedFlag(TRUE); UpdateAllViews(NULL); } 无关。 是 int x=(point.x-50)/100; int y=(point.y-50)/100; CSdiSquaresDoc * pDoc = (CSdiSquaresDoc *)GetDC(); 错了

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧