橡皮筋CRectTracker使用心得

来源:互联网 发布:清华深圳研究生院 知乎 编辑:程序博客网 时间:2024/04/28 20:43

CRectTracker被称作“橡皮筋”,是我们再窗口编辑中,选中对象,进一步操作的基础。橡皮筋有两种方法:一、手写橡皮筋,这个在程序中控制也比较好,二、使用CRectTracker系统类。在这我介绍使用CRectTracker。CRectTracker具体的参数和使用大家可以参考CSDN。我主要介绍基本的自己的程序中使用到情况。

首先在XXXView类中新建一个成员变量:

CRectTracker m_tracker;

其次在XXXView类的构造函数设置橡皮筋的属性

m_tracker.m_nStyle =CRectTracker::resizeInside|CRectTracker::dottedLine;  m_tracker.m_rect = CRect(-1, -1, -1, -1);

m_nStyle是设置橡皮筋的类型,具体类型有:

CRectTracker::solidLine   Use a solid line for the rectangle border.

CRectTracker::dottedLine   Use a dotted line for the rectangle border.

CRectTracker::hatchedBorder   Use a hatched pattern for the rectangle border.

CRectTracker::resizeInside   Resize handles located inside the rectangle.

CRectTracker::resizeOutside   Resize handles located outside the rectangle.

CRectTracker::hatchInside   Hatched pattern covers the entire rectangle.

m_rect则设置初始橡皮筋的大小(矩形),由于我在程序中要求根据鼠标点击选中对象,故初始为(-1, -1, -1, -1)即不可见。

昨晚初始工作后,要在程序中得到橡皮筋,仅仅需要响应鼠标左击事件OnLButtonDown(UINT nFlags, CPoint point)。如下面所示代码:

//此代码参考某一高手修改所写           int nIn = m_tracker.HitTest(point); //看看点到了哪了if(nIn<0)  //不在四边形区域内;{m_tracker.m_rect.SetRectEmpty();Invalidate(); }else         //在四边形区域内:{CClientDC dc(this);m_tracker.Draw(&dc);m_tracker.Track(this,point,TRUE);// Track()是CRectTracker中最富魅力的函数。它时时的改变调用者的m_rect;OnPrepareDC(&dc);CPoint pt_end;//以下代码是考虑SCRollView,记录坐标的逻辑位置GetCursorPos(&pt_end);rect.right=pt_end.x;rect.bottom=pt_end.y;ScreenToClient(&rect);pt_end.x=rect.right;pt_end.y=rect.bottom;if(pt_end!=m_pt) // 手动调用LBUTTONUP;其实也可以不调用也完成了橡皮筋的移动。但我程序需要继续处理SendMessage(WM_LBUTTONUP);Invalidate();}

在响应WM_LBUTTONUP鼠标放开事件,可以加自己想处理的代码。这样就能很完整的使用CRectTracker类。

 

 

在这也推荐一个更加详细,具体使用的好例子

http://www.vckbase.com/vckbase/vckbase10/vc/nonctrls/misc_21/1021002.htm