3
votes

I have been learning MFC these days.I want to draw lines with MoveTo() and LineTo() functions both in VC++6.0 and VS2010,but it seems that it does not work in vs2010.I only add two windows message handler,WM_LBUTTONDOWN and WM_LBUTTONUP,in the single document project. Here is the code in VC++6.0:

CPoint m_ptOrign;
void CStyleView::OnLButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    m_ptOrign=point;
    CView::OnLButtonDown(nFlags, point);
}

void CStyleView::OnLButtonUp(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    CClientDC dc(this);
    dc.MoveTo(m_ptOrign);
    dc.LineTo(point);
    CView::OnLButtonUp(nFlags, point);
}

Here is the code in vs2010:

CPoint m_ptOrign;
void CStyleView::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    m_ptOrign=point;
    CView::OnLButtonDown(nFlags, point);
}
void CStyleView::OnLButtonUp(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    CClientDC dc(this);

    dc.MoveTo(m_ptOrign);
    dc.LineTo(point);

    CView::OnLButtonUp(nFlags, point);
}

The codes I add to the two projects are the same.When I release the left button ,the line appears immediately in the vc++6.0 project,but it doesn't appear in the vs 2010 mfc project. If the size or location of the window of the vs 2010 project changes,the line apprears. But when I use dc.Rectangle(CRect(m_ptOrign,point)) in the vs 2010 project ,it works well. I do not know why.....

What's more,if I use

CBrush *pBbrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.SelectObject(pBbrush);
dc.Rectangle(CRect(m_ptOrign,point))

in vs2010,it does not work again,like the drawing line case

1
Why would you expect a NULL_BRUSH to draw anything?Cody Gray
The default brush is WHITE.The BRUSH doesn't affect the border of the rectangle. If i don't change the brush,the rectangle will be filled with some color.If i draw two rectangles at the same point,(first small,second big or they are close),the small one will totally be covered or erased by the big one.The small one can not be seen then.I make a NULL_BRUSH,so I can see the small one 'in' the big one's body.That is to say,I want to have all the rectangle(border) on the screen.@CodyGrayuser1196303

1 Answers

2
votes

LineTo is going to use the pen that is currently selected into the DC. Since you haven't selected a pen, it will use whatever is the default. I don't know why that would be different between VC6 and VC2010, perhaps it has something to do with the differences in MFC between the two versions.

In general it's a bad idea to grab a DC and start drawing on it. Better is to do all your drawing in the OnPaint or OnDraw methods. You can call InvalidateRect to cause a paint message to be sent to the window.