我在做一个小的画图程序,里面要实现这样一个功能,就是先用鼠标点一下,然后再移动鼠标,这时就出来一条线,终点跟着鼠标移动,然后再点一下,就确定这条线
但是我在MOUSEMOVE里面用了这样的代码后,在移动鼠标的时候如果移快了就会留下许多点点消不了,不知道是为什么
dc.SetROP2(R2_XORPEN);
dc.SetBkMode(TRANSPARENT);
if(m_DrawCurrent==1&&m_PushNum==1)
{
if(m_PointOld!=point)
{

dc.MoveTo (m_PointOrign);
dc.LineTo(m_PointOld);
 
dc.MoveTo (m_PointOrign);
dc.LineTo(point);
m_PointOld=point;
  }
}

解决方案 »

  1.   

    void CLineArtView::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    CScrollView::OnLButtonDown(nFlags, point);
    if( DragDC )
    {
    delete DragDC;
    DragDC = NULL;
    }
    DragDC = new CClientDC( this );
    DragDC->SetROP2( R2_NOT ); p1 = p2 = point;
    DragDC->MoveTo( p1 );
    DragDC->LineTo( p2 );
    SetCapture();}void CLineArtView::OnMouseMove(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    CScrollView::OnMouseMove(nFlags, point);
    if( DragDC )
    {
    DragDC->MoveTo( p1 );
    DragDC->LineTo( p2 ); p2 = point;
    DragDC->MoveTo( p1 );
    DragDC->LineTo( p2 );
    }
    }void CLineArtView::OnLButtonUp(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    CPoint org;

    CScrollView::OnLButtonUp(nFlags, point);
    if( DragDC )
    {
    DragDC->MoveTo( p1 );
    DragDC->LineTo( p2 ); p2 = point;
    DragDC->SetROP2( R2_COPYPEN );
    DragDC->MoveTo( p1 );
    DragDC->LineTo( p2 ); delete DragDC;
    DragDC = NULL;
    ReleaseCapture();
    org = GetScrollPosition();
    ( ( CLineArtDoc* )GetDocument() )->LineList.AddTail( LineSegment (p1.x+org.x,
                                      p1.y+org.y, p2.x+org.x, p2.y+org.y ) );
    GetDocument()->SetModifiedFlag( TRUE );
    GetDocument()->UpdateAllViews( this );
    }
    }
      

  2.   

    // PolyView.h : interface of the CPolyView class
    //
    /////////////////////////////////////////////////////////////////////////////#if !defined(AFX_POLYVIEW_H__1682CC61_608F_4653_AAE6_F8400B0968DB__INCLUDED_)
    #define AFX_POLYVIEW_H__1682CC61_608F_4653_AAE6_F8400B0968DB__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    class CPolyView : public CView
    {
    protected: // create from serialization only
    CPolyView();
    DECLARE_DYNCREATE(CPolyView)//在這個文件里,只在這個地方添加了幾個變量,別的地方都沒有修改
    protected:
    CPoint m_PointOld;
    CPoint m_PointFirst;
    CPoint m_PointOrigin;
    CPoint m_PointLast;
    BOOL m_bDrawPoly;
    BOOL m_bDraw;
    INT m_nCount;
    // Attributes
    public:
    CPolyDoc* GetDocument();// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CPolyView)
    public:
    virtual void OnDraw(CDC* pDC);  // overridden to draw this view
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    protected:
    virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
    virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
    virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CPolyView();
    #ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
    #endifprotected:// Generated message map functions
    protected:
    //{{AFX_MSG(CPolyView)
    afx_msg void OnDrawPolyline();
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };#ifndef _DEBUG  // debug version in PolyView.cpp
    inline CPolyDoc* CPolyView::GetDocument()
       { return (CPolyDoc*)m_pDocument; }
    #endif///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_POLYVIEW_H__1682CC61_608F_4653_AAE6_F8400B0968DB__INCLUDED_)
      

  3.   

    // PolyView.cpp : implementation of the CPolyView class
    //#include "stdafx.h"
    #include "Poly.h"#include "PolyDoc.h"
    #include "PolyView.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CPolyViewIMPLEMENT_DYNCREATE(CPolyView, CView)BEGIN_MESSAGE_MAP(CPolyView, CView)
    //{{AFX_MSG_MAP(CPolyView)
    ON_COMMAND(ID_DRAW_POLYLINE, OnDrawPolyline)
    ON_WM_LBUTTONDOWN()
    ON_WM_MOUSEMOVE()
    ON_WM_LBUTTONUP()
    ON_WM_RBUTTONUP()
    //}}AFX_MSG_MAP
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CPolyView construction/destructionCPolyView::CPolyView()
    {
    // TODO: add construction code here
    m_bDrawPoly = FALSE;
    m_bDraw = FALSE;
    m_nCount = 0;
    }CPolyView::~CPolyView()
    {
    }BOOL CPolyView::PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs return CView::PreCreateWindow(cs);
    }/////////////////////////////////////////////////////////////////////////////
    // CPolyView drawingvoid CPolyView::OnDraw(CDC* pDC)
    {
    CPolyDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
    }/////////////////////////////////////////////////////////////////////////////
    // CPolyView printingBOOL CPolyView::OnPreparePrinting(CPrintInfo* pInfo)
    {
    // default preparation
    return DoPreparePrinting(pInfo);
    }void CPolyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add extra initialization before printing
    }void CPolyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add cleanup after printing
    }/////////////////////////////////////////////////////////////////////////////
    // CPolyView diagnostics#ifdef _DEBUG
    void CPolyView::AssertValid() const
    {
    CView::AssertValid();
    }void CPolyView::Dump(CDumpContext& dc) const
    {
    CView::Dump(dc);
    }CPolyDoc* CPolyView::GetDocument() // non-debug version is inline
    {
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPolyDoc)));
    return (CPolyDoc*)m_pDocument;
    }
    #endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
    // CPolyView message handlers//下面的這些函數是加的,上面的除了構造函數別的地方都沒有修改void CPolyView::OnDrawPolyline() 
    {
    // TODO: Add your command handler code here
    m_bDrawPoly = TRUE;
    m_nCount = 0;
    }void CPolyView::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    if(m_bDrawPoly == TRUE)
    {
    if(m_nCount == 0)
    {
    m_PointFirst = point;
    }
    CRect ClientRect;
    GetClientRect(&ClientRect);
    if(PtInRect(&ClientRect, point))
    {
    m_bDraw = TRUE;
    m_PointOrigin = point;
    m_PointOld = point;
    }
    } CView::OnLButtonDown(nFlags, point);
    }void CPolyView::OnMouseMove(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    if(m_bDraw == TRUE)
    {
    CRect ClientRect;
    GetClientRect(&ClientRect);
    if(PtInRect(&ClientRect, point))
    {
    CDC *pDC = GetDC();
    pDC->SetROP2(R2_NOT); pDC->MoveTo(m_PointOrigin);
    pDC->LineTo(m_PointOld);
    pDC->MoveTo(m_PointOrigin);
    pDC->LineTo(point); m_PointOld = point;
    ReleaseDC(pDC);
    }
    } CView::OnMouseMove(nFlags, point);
    }void CPolyView::OnLButtonUp(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    if(m_bDraw == TRUE)
    {
    CRect ClientRect;
    GetClientRect(&ClientRect);
    if(PtInRect(&ClientRect, point))
    {
    m_nCount++;
    CDC *pDC = GetDC();
    pDC->SetROP2(R2_NOT); pDC->MoveTo(m_PointOrigin);
    pDC->LineTo(m_PointOld);
    pDC->MoveTo(m_PointOrigin);
    pDC->LineTo(point);

    ReleaseDC(pDC);
    m_PointLast = point;
    }
    } CView::OnLButtonUp(nFlags, point);
    }void CPolyView::OnRButtonUp(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    if(m_bDraw == TRUE)
    {
    CRect ClientRect;
    GetClientRect(&ClientRect);
    if(PtInRect(&ClientRect, point))
    {
    CDC *pDC = GetDC();
    pDC->SetROP2(R2_NOT); pDC->MoveTo(m_PointOrigin);
    pDC->LineTo(m_PointOld); pDC->MoveTo(m_PointLast);
    pDC->LineTo(m_PointFirst);
    ReleaseDC(pDC); m_bDrawPoly = FALSE;
    m_bDraw = FALSE;
    }
    } CView::OnRButtonUp(nFlags, point);
    }
      

  4.   

    另外还有OnLButtonDown,线是可以画出来的,但就是在鼠标移动的时候旁边会产生很多黑点
      

  5.   

    我给你一点思路吧,
    当你按下鼠标时,先判断是不是起点,如果起点那就MoveTo鼠标point.
    如果不是的话,随着鼠标移动,把就的终点察掉,你用的是SetROP2(R2_XORPen),那么用SetROP2(R2_NotXORPen)覆盖就的线就行了。这样你画的线就不会很多条了,始终只有一条。
      

  6.   

    我的程序是这样的,各位大侠误会了我的意思,现在的问题并不是不能画线,也不是画出很多线,画出来的线是正确的,但是在线的周围出现许多没有擦除掉的小黑点,不知道是为什么,
    我是照着例子里面的代码写的,但是例子程序就没有这个问题
    void CpainterView::OnLButtonDown(UINT nFlags, CPoint point)
    {
     
    m_PointOld=m_PointOrign=point;
    SetCapture();
    m_drawing=1;
        
    CView::OnLButtonDown(nFlags, point);
    }void CpainterView::OnRButtonDown(UINT nFlags, CPoint point)
    {
        if(m_drawing) 
    {
      
        CClientDC dc(this);
         ReleaseCapture();
                  m_drawing=0;
    }
    CView::OnRButtonDown(nFlags, point);
    }void CpainterView::OnMouseMove(UINT nFlags, CPoint point)
    {
    CpainterDoc *pDoc=GetDocument();
    CClientDC dc(this); 
             dc.SetROP2(R2_XORPEN);  
    dc.SelectObject(&m_PenTemp);  
             dc.SetBkMode(TRANSPARENT);    
              if( m_drawing==1)

    dc.MoveTo (m_PointOrign);
    dc.LineTo(m_PointOld);
             
    dc.MoveTo (m_PointOrign);
    dc.LineTo(point);
    m_PointOld=point; 
    }
    CView::OnMouseMove(nFlags, point);
    }
      

  7.   

    程序的抓图放在这里,大家帮忙看一下啊
    http://img.album.enorth.com.cn:81/pic222/big/00/61/97/619799_892220.jpg
      

  8.   

    把dc.SetROP2(R2_XORPEN);
    改成dc.SetROP2(R2_NOT); 试试
      

  9.   

    原因是异或画图对不变的背景是可以的,但你在MouseMove里画图,你鼠标一动,背景就变了,你在异或自然不能恢复原样了.
    最好是在LButtonDown里完成画图,或者在MouseMove里记录鼠标的移动位置,在LButtonDown里画出最终的直线.
      

  10.   

    我的机子是VC7+WIN2000 SERVER SP3
    现在的问题是,该程序在别的机器上运行没有问题,不管是VC7还是VC6,但在我的机子上就有问题,而例子程序是在VC6下编辑的,我将它升级到VC7,却没有问题
      

  11.   

    是执行有问题啊,程序的执行画面在这里
    http://img.album.enorth.com.cn:81/pic222/big/00/61/97/619799_892220.jpg
      

  12.   

    我以前遇到过类似问题,原因是鼠标不动的话,异或画一条线,再画一遍,就恢复原样,如果鼠标动的话,情况就不一样了,由于机器的原因,有可能原来的背景没擦干净,就画上新线了。这就是你遇到问题的原因。至于VC7没问题,VC6不行,以及别人的机器没问题,我想大概是运行时鼠标移动速度不够,没产生问题。本人才疏学浅,说的不对请多多包涵!
      

  13.   

    my god,原因找到了,是显卡驱动的问题,以前装的是WIN2000自带的驱动,现在换上nvdia的驱动,问题就解决了。