1, 新建MFC SDI工程Test
2, 在视图类CTestView添加成员变量 CDC *pDc
3, 响应OnLButtonDown消息void CTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
       pDc = GetDC();
       pDc->MoveTo(point);
}4, 响应OnLButtonUp消息void CTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
       pDc->LineTo(point);

}然后我运行程序,在A点按下鼠标不放,拖动到B点放开鼠标,为什么没有画出线出来?
如果我将pDc->LineTo(point)移动到OnLButtonDown函数里面是可以画线的
我跟踪的了程序,发现OnLButtonUp函数被执行了,里面的pDc->LineTo(point)这个代码也被执行到了,但就是没有线条出来还有我想知道为什么MFC中总是有这些莫名其妙的,让我无法解释的,稀奇古怪的错误出现?

解决方案 »

  1.   

    pDc->LineTo(point);
    这个pDC是从哪来的呢?
      

  2.   

    画图的代码要写到OnDraw函数里.在鼠标消息响应函数中保存需要的点,然后在OnDraw函数中绘制.
      

  3.   


    void CTestView::OnLButtonDown(UINT nFlags, CPoint point)
    {
           pDc = GetDC();
           m_ptStart = point;       
    }
    void CTestView::OnLButtonUp(UINT nFlags, CPoint point)
    {
           pDc->MoveTo(m_ptStart);  
           pDc->LineTo(point);
        
    }
    这样就可以了,但是画图要到画图的消息函数里进行.
    而且用GetDC完后要进行::ReleaseDC,否则会内存泄露,还有要判断指针pDC是否为空...
      

  4.   


    void CTestView::OnLButtonDown(UINT nFlags, CPoint point)
    {
                 m_ptStart = point;       
    }
    void CTestView::OnLButtonUp(UINT nFlags, CPoint point)
    {
           CClientDC dc(this);
           dc.MoveTo(m_ptStart);  
           dc.LineTo(point);
        
    }
      

  5.   


    void CMoveToView::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default

    m_sPoint = point;

    CView::OnLButtonDown(nFlags, point);
    }void CMoveToView::OnLButtonUp(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    m_pDc = GetDC();
    m_pDc->MoveTo(m_sPoint);
    m_pDc->LineTo(point);
    ReleaseDC(m_pDc);
    CView::OnLButtonUp(nFlags, point);
    }
    你的有问题,这样就OK了,看看。
      

  6.   

     4,5,6的方法都可以解决问题。其实这个问题在于,在响应onLbuttonup时,引起了界面重绘,以前的绘图都被刷新,消失了。你可以调试一下,在OnDraw里打一个断点,然后使其响应OnLbuttonup消息,应该会执行到断点。