| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 727 人关注过本帖
标题:VC画橡皮筋直线问题
只看楼主 加入收藏
shuai_ke1234
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-1-29
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
VC画橡皮筋直线问题
我希望实现“按下鼠标左键后移动鼠标,生成随鼠标位置变化的直线,而松开鼠标左键后,直线不再变化。”的功能。
相关代码如下:
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
    m_ptNow = point;
    m_ptOrigin = point;
    CView::OnLButtonDown(nFlags, point);
}

void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
    CDC *pDC=GetDC();
    Line(point,pDC);
    CView::OnLButtonUp(nFlags, point);
}


void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
    CDC *pDC=GetDC();
    Line(point,pDC);
    pDC->SetROP2 (R2_NOTXORPEN);
    Line(m_ptNow,pDC);
    m_ptNow = point;
    CView::OnMouseMove(nFlags, point);
}

void CDrawView::Line(CPoint point,CDC* ptr_DC)//用布莱森汉姆算法实现
{
    int x,y;
    if(m_ptOrigin.x == point.x)
    {
        if(m_ptOrigin.y <= point.y)
            for(y = m_ptOrigin.y ;y <= point.y ; y++)
                ptr_DC->SetPixel(m_ptOrigin.x,y,m_clr);
        else
            for(y = point.y ; y <= m_ptOrigin.y ; y++)
                ptr_DC->SetPixel (m_ptOrigin.x,y,m_clr);
    }
    if(m_ptOrigin.y == point.y)
    {
        if(m_ptOrigin.x <= point.x)
            for(x = m_ptOrigin.x ; x <= point.x ; x++)
                ptr_DC->SetPixel(x,m_ptOrigin.y ,m_clr);
        else
            for(x = point.x ; x <= m_ptOrigin.x ; x++)
                ptr_DC->SetPixel (x,m_ptOrigin.y,m_clr);
    }
    double k = (point.y - m_ptOrigin.y)*1.0/(point.x - m_ptOrigin.x);//斜率
    double p = 2*k-1;//布莱森汉姆算法中的中间量 p
    if(k > 0 && k <= 1)//斜率为0到1
    {
        if(x <= point.x)
        {
            while(x <= point.x)
            {
                ptr_DC->SetPixel(x,y,m_clr);
                if(p>0)
                {
                    y += 1;
                    x ++;
                    p += 2*k-2;
                }
                else
                {
                    y = y;
                    x ++;
                    p += 2*k;
                }
            }
        }
        if(x > point.x)
        {
            while(x > point.x)
            {
                ptr_DC->SetPixel(x,y,m_clr);
                if(p>0)
                {
                    y -= 1;
                    x --;
                    p -= 2*(k-1);
                }
                else
                {
                    y = y;
                    x --;
                    p -= 2*k;
                }
            }
        }
    }
    if(k < 0 && k >= -1)//斜率从-1到0
    {
        if(x <= point.x)
        {
            while(x <= point.x)
            {
                ptr_DC->SetPixel (x,y,m_clr);
                if(p > 0)
                {
                    y = y;
                    x ++;
                    p += 2*k;
                }
                else
                {
                    y += 1;
                    x ++;
                    p += 2*(k-1);
                }
            }
        }
        if(x > point.x)
        {
            while(x > point.x)
            {
                ptr_DC->SetPixel(x,y,m_clr);
                if(p >0)
                {
                    y = y;
                    x --;
                    p -= 2*k;
                }
                else
                {
                    y -= 1;
                    x --;
                    p -= 2*(k-1);
                }
            }
        }
    }
    p = 2.0/k-1;
    if(k > 1)
    {
        if(y <= point.y)
        {
            while(y <= point.y)
            {
                ptr_DC->SetPixel (x,y,m_clr);
                if(p >= 0)
                {
                    x += 1 ;
                    y ++;
                    p += 2.0/k-2;
                }
                if(p < 0)
                {
                    x = x;
                    y ++;
                    p += 2.0/k;
                }
            }
        }
        if(y > point.y)
        {
            while(x > point.x)
            {
                ptr_DC->SetPixel(x,y,m_clr);
                if(p >0)
                {
                    x = x;
                    y --;
                    p -= 2.0/k;
                }
                else
                {
                    x -= 1;
                    y --;
                    p -= 2.0/k-2;
                }
            }
        }
    }
}

问题描述:我在执行之后,如果鼠标不移入窗口区则正常,一移入,则程序就进入了死循环。用断点调试,发现问题出在Line函数,可是一直没有发现Line函数的错误到底在哪。请各位帮我挑一挑错误,谢谢!
搜索更多相关主题的帖子: 橡皮筋 
2014-01-29 15:41
yuccn
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:10 
调试一下不就知道了?
如果定位到line 出现问题,那么内部那处出现死循环 就不难跟踪了

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2014-02-02 18:02
shuai_ke1234
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-1-29
收藏
得分:0 
回复 2楼 yuccn
嗯,那我再去吧line函数调试一下。
2014-02-04 07:34
快速回复:VC画橡皮筋直线问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045657 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved