| 
				 4.3 接口功能函数 
(1)   与视图绑定函数 
void  SetParentWnd(void * pView)   
{  
m_pView = reinterpret_cast<CTestProView*>(pView); 
} 
(2)初始化矩形大小与位置函数 
void  SetRect(CRect rect)          
{ 
    m_Rect = rect;  
} 
5 应用实例 
现在,举一个例子具体的去应用该矩形操作类CRectGraph。首先,建立工程与添加CRectGraph类。用VC6.0建立一个新的单文档视图的工程TestPro。在工程中分别添类CRectGraph的头文件RectGraph.h与cpp文件RectGraph.cpp。其次,定义CRectGraph对象。 
在类CTestProView的头文件定义CRectGraph对象m_RectGraph。第三,对象与视图绑定和对象的初始化。 
分别在类CTestProView的构造函数中CRectGraph对象m_RectGraph与绑定,及初始化与定位m_RectGraph对象。代码如下: 
(1)CRectGraph对象m_RectGraph与该视图绑定 
m_RectGraph.SetParentWnd(this); 
(2)初始化与定位m_RectGraph对象 
CRect ret; 
ret.left = 100;             ret.right = 200; 
ret.top = 100;             ret.bottom = 200; 
m_RectGraph.SetRect(ret); 
(3)添加显示与操作该矩形操作对象函数 
1)在类CTestProView的OnDraw(CDC* pDC) 函数中添加画实体矩形函数 
if (!m_RectGraph.m_bIsDraw) 
    m_RectGraph.DrawRect(0,0,RGB(255,0,0)); 
2)在类CTestProView的OnLButtonDown(UINT nFlags, CPoint point)函数中添加矩形操作类的RectLButtonDown(nFlags,point) 
m_RectGraph.RectLButtonDown(nFlags,point); 
3)在类CTestProView的OnMouseMove(UINT nFlags, CPoint point) 函数中添加实体矩形操作类的RectMouseMove(nFlags,point)函数 
m_RectGraph.RectMouseMove(nFlags,point); 
4)在类CTestProView的OnLButtonUp (UINT nFlags, CPoint point) 函数中添加实体矩形操作类的RectLButtonUp (nFlags,point)函数 
m_RectGraph.RectLButtonUp(nFlags,point); 
另外,如果要想在框架中或其他地方实时显示矩形的坐标,可在OnMouseMove(UINT nFlags, CPoint point) 函数中取出矩形操作类的大小与位置的变量m_Rect,并显示到界面上。 
比如:实现在状态栏中实现实时显示矩形坐标的代码如下: 
CString str; 
CStatusBar *pStatus = NULL; 
CMainFrame *pMainWnd = NULL; 
pMainWnd = (CMainFrame *)AfxGetApp()->m_pMainWnd; 
pStatus = (CStatusBar *)pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR); 
if (pStatus) 
{ 
str.Format("左高(X =%d,Y=%d );右底(X = %d,Y= %d)",m_RectGraph.m_Rect.left,m_RectGraph.m_Rect.top,m_RectGraph.m_Rect.right,m_RectGraph.m_Rect.bottom); 
     pStatus->SetPaneText(1,str); 
} 
6 结语 
矩形操作类的实现,能够很方便地移植到各类视图中,进行范围的选取与定位,也可以很方便地扩展矩形操作类的功能,比如在界面中通过按钮,或通过键盘去定位移动,上下左右拖拽。另外,该类的实现原理不仅仅局现于矩形图形,也可以通过该种方法实现圆、多边形等不同的图形。 			
				 |