[MFC] OnDraw 동작

Posted by seunggwon
2008. 11. 24. 14:29 IT Note/기타언어

1. Project 생성 - MDI로 생성
2. Document class 에 CPoint 형의 멤버 m_pt 추가
protected,  초기값 ( x=10, y=10 )
3. m_pt를 access 할 수 있는 interface function 추가
GetPoint, SetPoint
4. WM_LBUTTONDOWN 의 handler 추가 (View)
m_pt에 현재 마우스 위치를 setting
CClientDC 생성, 이 위치에 푸른 점 출력
CDC::SetPixel
5. OnDraw 구현
m_pt의 위치에 붉은 점 출력
6. Build 후 실행
New Window 메뉴 : 하나의 Doc에 2개의 Views로 보기
왼쪽 마우스 클릭,  resize 등에 의한 결과 비교  

class CShapeDoc : public CDocument
{    ……
     protected:    CPoint    m_pt;
     public :         void       SetPoint(CPoint  pt);
                        CPoint   GetPoint();
      ……
};
BOOL CShapeDoc::OnNewDocument()
{    ……
      m_pt = CPoint(10,10);
     ……
}

void CShapeDoc::SetPoint(CPoint pt)
{
         m_pt=pt;
}
CPoint CShapeDoc::GetPoint()
{
        return m_pt;
}

void CShapeView::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 CClientDC dc(this);
 GetDocument()->SetPoint(point);
 dc.SetPixel(point.x,point.y,RGB(0,0,255));

 CView::OnLButtonDown(nFlags, point);
}
void CShapeView::OnDraw(CDC* pDC)
{
 CShapeDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
  
          // TODO: add draw code for native data here
       CPoint pt = pDoc->GetPoint();
 pDC->SetPixel(pt.x,pt.y,RGB(255,0,0));
}

'IT Note > 기타언어' 카테고리의 다른 글

[MFC] Shape  (0) 2008.11.24
[MFC] Graphic 출력  (0) 2008.11.24
[MFC] Text 출력  (0) 2008.11.07