class  CMyRect
CMyRect::CMyRect(void)
: topPoint(0)
, botPoint(0)
, bDown(FALSE)
, bMove(FALSE)
, bCheck(FALSE)
, tempPt(0)
{
}CMyRect::~CMyRect(void)
{
}// 在OnDraw中调用重画
void CMyRect::Draw(CDC * pDC)
{
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);
}
}// 鼠标按下
void CMyRect::OnMDn(CPoint point)
{
bDown = TRUE;
bCheck = FALSE;
if((point.x>topPoint.x && point.x<botPoint.x) && (point.y>topPoint.y && point.y<botPoint.y))
bCheck = 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;
} bDown = FALSE;
bMove = FALSE;
bCheck = FALSE;
}void CMyRect::DrTo(CDC * pDC, CPoint oldpt, CPoint newpt)
{
int movex = newpt.x-oldpt.x;
int movey = newpt.y-oldpt.y;
//oldpt = newpt;
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;
}下面是在 CmyView中的调用
void CDrawRectView::OnRect()    //菜单事件,按一下构造一个矩形
{
// TODO: 在此添加命令处理程序代码
CDC * pDC = GetDC();
toppoint = CPoint(100,100);
botpoint = CPoint(200,200);
myrect1 = new CMyRect(toppoint,botpoint);
myrect1 ->Draw(pDC);
m_ptrArray.Add(myrect1);
}void CDrawRectView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(m_ptrArray.GetSize()>0)                                                         
         {     
          int i = 0;
                   for( i;i=m_ptrArray.GetSize();++i );
{
int x1 = ((CMyRect *)m_ptrArray.GetAt(i))->topPoint.x;
int x2 = ((CMyRect *)m_ptrArray.GetAt(i))->botPoint.x;
int y1 = ((CMyRect *)m_ptrArray.GetAt(i))->topPoint.y;
int y2 = ((CMyRect *)m_ptrArray.GetAt(i))->botPoint.y;

if((point.x>x1 && point.x<x2) && (point.y>y1 && point.y<y2))
{
((CMyRect *)m_ptrArray.GetAt(i))->bCheck = TRUE;
nIndex = i;
}
}
((CMyRect *)m_ptrArray.GetAt(nIndex))->OnMDn(point);
}
CView::OnLButtonDown(nFlags, point);
}void CDrawRectView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(m_ptrArray.GetSize()>0)
((CMyRect *)m_ptrArray.GetAt(nIndex))->OnMUp(point);
Invalidate(FALSE);
CView::OnLButtonUp(nFlags, point);
}void CDrawRectView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDC * pDC = GetDC(); 
if(m_ptrArray.GetSize()>0)
((CMyRect *)m_ptrArray.GetAt(nIndex))->OnMMv(pDC,point);
Invalidate(FALSE);
CView::OnMouseMove(nFlags, point);
}
m_ptrArray 是CPtrArray 对象 储存CMyRect * rect;
在鼠标左击事件中,我想判断我选中了哪个矩形,然后可以移动这个矩形。我现在就是鼠标左击事件中不知道怎么判断。请各位朋友指点,越详细越好。

解决方案 »

  1.   

    谢谢,可以了。就是因为重绘,在鼠标移动消息里面加了Invalidate();所以鼠标一移动就特别闪,我都用双缓冲,不知道有什么办法能解决闪屏问题啊。
      

  2.   

    我是在Mousemove中调用的Invalidate,所以只要移动鼠标,矩形框就会闪个不停。我有擦掉背景的代码,为了擦除移动矩形历史位置。不知道有什么好点的方法没。
      

  3.   

    有个问题啊
    鼠标消息里不应该有绘图函数啊
    ((CMyRect *)m_ptrArray.GetAt(nIndex))->OnMMv(pDC,point);
    这句话有问题吧
    应该在鼠标消息里只修改绘制参数
    然后通过Invalidate激活重绘
    OnPaint里用双缓冲
    OnEraseBkng只返回true
      

  4.   


    你试试InvalidateRect()只刷新你的那个矩形框