16,473
社区成员
发帖
与我相关
我的任务
分享
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);
}
}