MFC简单绘图

来源:互联网 发布:淘宝详情页模版 编辑:程序博客网 时间:2024/05/21 22:59

1.1 绘制直线(HDC和CClientDC的比较)

private:

    CPoint m_nPoint;

    ////////.......

    CDrawTestView::CDrawTestView()

         : m_nPoint(0)

    {

         // TODO:  在此处添加构造代码

    }

    ////////.......

    void CDrawTestView::OnLButtonDown(UINTnFlags, CPointpoint)

    {

         // TODO:  在此添加消息处理程序代码和/或调用默认值

         //MessageBox(_T("鼠标左键按下!"));  //测试:没有如愿,视图类窗口始终覆盖在框架类窗口之上,所以视图类窗口会挡在框架类的鼠标、键盘消息。

         m_nPoint = point;

         CView::OnLButtonDown(nFlags,point);

    }

 

HDC:

    void CDrawTestView::OnLButtonUp(UINTnFlags, CPointpoint)

    {

         // TODO:  在此添加消息处理程序代码和/或调用默认值

         //MessageBox(_T("鼠标左键释放!"));

         HDC m_hdc =::GetDC(m_hWnd);

         MoveToEx(m_hdc, m_nPoint.x, m_nPoint.y,NULL);

         LineTo(m_hdc, point.x, point.y);

         ::ReleaseDC(m_hWnd, m_hdc);

         CView::OnLButtonUp(nFlags,point);

    }

CClientDC:

void CDrawTestView::OnLButtonUp(UINTnFlags, CPointpoint)

{

    // TODO:  在此添加消息处理程序代码和/或调用默认值

    //MessageBox(_T("鼠标左键释放!"));

    CClientDC dc(this);

    dc.MoveTo(m_nPoint);

    dc.LineTo(point);

    CView::OnLButtonUp(nFlags,point);

}

附加: CClientDC dc(GetParent()); //可以画到工具栏和菜单栏


CWindowDCdc(GetDesktopWindow());

改变颜色:

void CDrawTestView::OnLButtonUp(UINTnFlags, CPointpoint)

{

    // TODO:  在此添加消息处理程序代码和/或调用默认值

    //MessageBox(_T("鼠标左键释放!"));

    //画笔

    CPen pen(PS_SOLID, 1,RGB(255, 0, 0));

    CWindowDC dc(this);

    dc.SelectObject(&pen);

    CPen *OldPen = dc.SelectObject(&pen);//旧画笔

    dc.MoveTo(m_nPoint);

    dc.LineTo(point);

    dc.SelectObject(&OldPen);

    CView::OnLButtonUp(nFlags,point);

}

1.2 使用笔刷

使用笔刷(矩形):

void CDrawTestView::OnLButtonUp(UINTnFlags, CPointpoint)

{

    // TODO:  在此添加消息处理程序代码和/或调用默认值

    //MessageBox(_T("鼠标左键释放!"));

    CBrush m_brush(RGB(255, 0, 0));

    CClientDC dc(this);

    dc.FillRect(CRect(m_nPoint,point),&m_brush);

    CView::OnLButtonUp(nFlags,point);

}

使用位图笔刷:


空画刷(透明画刷):


 

0 0
原创粉丝点击