class CMyDrawView : public CView
{
DECLARE_DYNCREATE(CMyDrawView)
protected:
CMyDrawView();
virtual ~CMyDrawView();
public:
void drawCarSpace(CDC* pDC,int x,int y,int width,int height);
void drawBody(CDC* pDC,int x,int y,int width,int height);
void drawText(CDC* pDC,int x,int y,CString text);
void drawCar(CDC* pDC);
public:
virtual void OnDraw(CDC* pDC);
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnAddcar();
};
void CMyDrawView::OnDraw(CDC* pDC)
{
int space_width=50;
int space_height=50;
CDocument* pDoc = GetDocument();
drawBody(pDC,20,20,1030,615);
drawCarSpace(pDC,40,40,50,100);
drawText(pDC,62,87,_T("1"));
drawCarSpace(pDC,150,40,50,100);
drawText(pDC,172,87,_T("2"));
}
void CMyDrawView::drawCar(CDC* pDC)
{
CBitmap bitmap;
bitmap.LoadBitmapW(IDB_Car1);
BITMAP bm;
bitmap.GetBitmap(&bm);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap *oldBitmap=(CBitmap *)memDC.SelectObject(&bitmap);
CRect rect(40,40,bm.bmWidth+40,bm.bmHeight+40);
TransparentBlt(pDC->GetSafeHdc(),rect.left,rect.top,rect.Width(),
rect.Height(),memDC.GetSafeHdc(),0,0,bm.bmWidth,bm.bmHeight,
RGB(0,0,0));
}
void CMyDrawView::drawText(CDC* pDC,int x,int y,CString text)
{
pDC->TextOutW(x,y,text);
}
void CMyDrawView::drawBody(CDC* pDC,int x,int y,int width,int height)
{
CPen pen;
pen.CreatePen(PS_SOLID,2,RGB(0,255,0));
CPen *oldPen=(CPen *)pDC->SelectObject(&pen);
pDC->SelectObject(oldPen);
pDC->Rectangle(x,y,width+x,height+y);
pen.DeleteObject();
}
void CMyDrawView::drawCarSpace(CDC* pDC,int x,int y,int width,int height)
{
CBrush brush(HS_HORIZONTAL,RGB(0,255,0));
CBrush *oldBrush=pDC->SelectObject(&brush);
pDC->Rectangle(CRect(x,y,width+x,height+y));
brush.DeleteObject();
}
void CMyDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
CMainFrame *pFrmWnd = (CMainFrame *)GetTopLevelFrame();
CString str;
str.Format(_T("横坐标:%d"),point.x);
pFrmWnd->m_wndStatusBar.SetPaneText(2,str);
str.Format(_T("纵坐标:%d"),point.y);
pFrmWnd->m_wndStatusBar.SetPaneText(3,str);
CView::OnMouseMove(nFlags, point);
}
void CMyDrawView::OnAddcar()
{
CDC* pdc=GetDC();
drawCar(pdc);
}