我想让自己已画好的矩形,鼠标移到矩形边框时光标样式变成可改变大小的样式,鼠标左键按下移动鼠标,矩形的大小改变。就像CTrackerRect类一样改变大小的功能。但是我要自己实现。 不知道怎么判断鼠标是否在边框位置,还有拉伸时,鼠标是在矩形的左边还是右边好像也有说法啊。请好心人帮忙分析一下,该怎么做。
我用PtInRect()来判断 点的位置,我画出来的矩形 CRect rc的大小是知道的, 然后定义一个CRect rc1,rc1的top点坐标比rc的大1,rc1的bottom点的坐标比rc的小1. if(PtInRect(&rc,point)==TRUE && PtInRect(&rc1,point)==FALSE)  BOOL flag = TRUE;
这样点只有在矩形边框才能使 flag = TRUE ,不知道这个想法对不对。 后面如何改变大小就不怎么想得通了。求指点啊

解决方案 »

  1.   

    在OnMouseMove 中 写代码 用if(rect.ptinrect(point)&WM_LBUTTONDOW)
    判断鼠标在有效区域内 并且移动  通过在OnLbuttonDown() 中截获 Point 点。作为起始点
    在OnmouseMove 中 通过起始点 和终点 就可以画你想要大小得方框了。
      

  2.   

    void C***View::OnMouseMove(UINT nFlags, CPoint point) 
    {
            if(Rect.PtInRect(point) && nFlags&WM_LBUTTONDOWN)
    {
    ScreenToClient(&point);
                    这个point是终点;
                      在OnLButtonDown()中截获的point 是起始点 用一个变量 来记录这个起始点。
                      就可以画图了 。
             }
    }
      

  3.   

    做好了,不过觉得很烂。class CMyRect
    {
    public:
    CMyRect(void);
    ~CMyRect(void);
    // 在OnDraw中调用重画
    void Draw(CDC * pDC);
    // 构造矩形的顶点
    CPoint topPoint;
    // 构造矩形的下点
    CPoint botPoint;
    // 鼠标按下标识
    BOOL bDown;
    // 鼠标移动标识
    BOOL bMove;
    // 矩形选中标识
    BOOL bCheck;
    // 鼠标移动
    void OnMMv(CDC * pDC , CPoint point);
    // 鼠标按下
    void OnMDn(CPoint point);
    // 鼠标弹起
    void OnMUp(CPoint point);
    void DrTo(CDC * pDC, CPoint oldpt, CPoint newpt);
    CMyRect(CPoint toppt, CPoint botpt);
    CPoint tempPt;
    // 改变矩形大小
    void ChangeSize(CDC * pDC , CPoint oldpoint, CPoint point);
    BOOL bChange;
    BOOL bup;
    BOOL bdown;
    BOOL bright;
    BOOL bleft;
    };CMyRect::CMyRect(void)
    : topPoint(0)
    , botPoint(0)
    , bDown(FALSE)
    , bMove(FALSE)
    , bCheck(FALSE)
    , tempPt(0)
    , bChange(FALSE)
    , bup(FALSE)
    , bdown(FALSE)
    , bright(FALSE)
    , bleft(FALSE)
    {
    }CMyRect::~CMyRect(void)
    {
    }// 在OnDraw中调用重画
    void CMyRect::Draw(CDC * pDC)
    {
    CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
    pDC->SelectObject(pBrush);
    pDC->Rectangle(CRect(topPoint,botPoint));
    }
    // 鼠标移动
    void CMyRect::OnMMv(CDC * pDC , CPoint point)
    {
    if(bDown==TRUE)
    {
    bMove = TRUE;
    }
    else
    {
    bMove = FALSE;
    }
    if(bMove==TRUE && bCheck==TRUE)
    {
    DrTo(pDC,tempPt,point);
    }
    else if(bMove==TRUE && bChange ==TRUE)
    { ChangeSize(pDC,tempPt,point);

    }
    }// 鼠标按下
    void CMyRect::OnMDn(CPoint point)
    {
    bDown = TRUE;
    bCheck = FALSE;
    bChange = FALSE;
    CRect rc;
    CRect rc1;
    rc = CRect(topPoint.x+2,topPoint.y+2,botPoint.x-2,botPoint.y-2);
    rc1 = CRect(topPoint.x-1,topPoint.y-1,botPoint.x+1,botPoint.y+1);
    if(PtInRect(&rc,point)) {bCheck = TRUE;}
    if( PtInRect(&rc1,point)== TRUE && PtInRect(&rc,point) == FALSE ) 
    {
    bChange = TRUE;
    }
    CRect rc2;
    CRect rcup;
    CRect rcdown;
    CRect rcright;
    CRect rcleft;
    rc2 = CRect(topPoint.x-1,topPoint.y-1,botPoint.x+1,botPoint.y+1);           //TRUE
    rcleft = CRect(topPoint.x+2,topPoint.y-1,botPoint.x+1,botPoint.y+1);
    rcup = CRect(topPoint.x-1,topPoint.y+2,botPoint.x+1,botPoint.y+1);
    rcdown = CRect(topPoint.x-1,topPoint.y-1,botPoint.x+1,botPoint.y-2);
    rcright = CRect(topPoint.x-1,topPoint.y-1,botPoint.x-2,botPoint.y+1);
    if(PtInRect(&rc2,point)== TRUE && PtInRect(&rcleft,point) == FALSE)    //左边
    {
    bleft = TRUE;
    }
    if(PtInRect(&rc2,point)== TRUE && PtInRect(&rcright,point) == FALSE)  //右边
    {
    bright = TRUE;
    }
    if(PtInRect(&rc2,point)== TRUE && PtInRect(&rcup,point) == FALSE)  //上边
    {
    bup = TRUE;
    }
    if(PtInRect(&rc2,point)== TRUE && PtInRect(&rcdown,point) == FALSE)  //下边
    {
    bdown = TRUE;
    }
    tempPt = point;}// 鼠标弹起
    void CMyRect::OnMUp(CPoint point)
    {
    if(bMove && bCheck && bDown == TRUE )
    {
    topPoint.x = topPoint.x+point.x - tempPt.x;
    topPoint.y = topPoint.y+point.y - tempPt.y;
    botPoint.x = botPoint.x+point.x - tempPt.x;
    botPoint.y = botPoint.y+point.y - tempPt.y;
    }
    if(bright == TRUE)
    {
    botPoint.x = botPoint.x + point.x - tempPt.x;
    }
    if(bleft == TRUE)
    {
    topPoint.x = topPoint.x + point.x - tempPt.x;
    }
    if(bup == TRUE)
    {
    topPoint.y = topPoint.y + point.y - tempPt.y;
    }
    if(bdown == TRUE)
    {
    botPoint.y  = botPoint.y + point.y - tempPt.y;
    }
    bDown = FALSE;
    bMove = FALSE;
    bCheck = FALSE;
    bChange = FALSE;
    bright = FALSE;
    bleft = FALSE;
    bup = FALSE;
    bdown = FALSE;
    }void CMyRect::DrTo(CDC * pDC, CPoint oldpt, CPoint newpt)
    {
    int movex = newpt.x-oldpt.x;
    int movey = newpt.y-oldpt.y; topPoint.x = topPoint.x+movex;
    topPoint.y = topPoint.y+movey;
    botPoint.x = botPoint.x+movex;
    botPoint.y = botPoint.y+movey; pDC->Rectangle(CRect(topPoint,botPoint)); topPoint.x = topPoint.x-movex;
    topPoint.y = topPoint.y-movey;
    botPoint.x = botPoint.x-movex;
    botPoint.y = botPoint.y-movey;
    }CMyRect::CMyRect(CPoint toppt, CPoint botpt)
    {
    this->topPoint = toppt;
    this->botPoint = botpt;
    }// 改变矩形大小
    void CMyRect::ChangeSize(CDC * pDC , CPoint oldpoint, CPoint point)
    {
    int movex = point.x - oldpoint.x;
    int movey = point.y - oldpoint.y; if(bleft)
    {
    topPoint.x = topPoint.x + movex;
    pDC->Rectangle(CRect(topPoint,botPoint));
    topPoint.x = topPoint.x - movex; }
    if(bright)
    {
    botPoint.x = botPoint.x + movex;
    pDC->Rectangle(CRect(topPoint,botPoint));
    botPoint.x = botPoint.x - movex;
    }
    if(bup)
    {

    topPoint.y = topPoint.y + movey;
    pDC->Rectangle(CRect(topPoint,botPoint));
    topPoint.y = topPoint.y - movey;
    }
    if(bdown)
    {
    botPoint.y = botPoint.y + movey;
    pDC->Rectangle(CRect(topPoint,botPoint));
    botPoint.y = botPoint.y - movey;
    }
    //else if()  //左上角
    //{
    //}
    //else if()  //右上角
    //{
    //}
    //else if()  //左下角
    //{
    //}
    //else if()  //右下脚
    //{
    //}}
    如果你有什么改进麻烦告诉我啊