MFC绘图

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();
    // TODO: 在此添加绘制代码
    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();
}

// CMyDrawView 消息处理程序

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);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值