编写一个windows应用程序,要求在窗口的用户区中绘制一个圆 ,当单击左键时,该圆放大;单击右键时,该圆缩小;按下ctrl键时的同时鼠标移动,则该圆会随鼠标移动而移动

编写一个windows应用程序,要求在窗口的用户区中绘制一个圆 ,当单击左键时,该圆放大;单击右键时,该圆缩小;按下ctrl键时的同时鼠标移动,则该圆会随鼠标移动而移动。


1.建立一个SDI,名字为MFCexp7_9


2.在文档类中添加一个成员

    CRect m_Ell;


3.在文档类的构造函数中对成员初始化

CMy7_9Doc::CMy7_9Doc() : m_Ell( 100 , 100 , 200 , 200 )
{
// TODO: add one-time construction code here
}


4.在View类添加OnLButtonDown函数里添加:

void CMy7_9View::OnLButtonDown(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
CMy7_9Doc *pDoc = GetDocument();
CRect clientRec;
GetClientRect( &clientRec );

////if条件是判断是否越界
if( pDoc->m_Ell.left > 0 ) 
pDoc->m_Ell.left -= 5;
if( pDoc->m_Ell.right <=(clientRec.right-clientRec.left)  )
pDoc->m_Ell.right += 5;
if( pDoc->m_Ell.top > 0 )
pDoc->m_Ell.top -= 5;
if( pDoc->m_Ell.bottom <= (clientRec.bottom - clientRec.top) )
pDoc->m_Ell.bottom += 5;
Invalidate( true  );
CView::OnLButtonDown(nFlags, point);
}


5.在View类的OnRButtonDown函数里添加:

void CMy7_9View::OnRButtonDown(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
CMy7_9Doc *pDoc = GetDocument();
CRect clientRec;
GetClientRect( &clientRec );
if( pDoc->m_Ell.left + 5 <= pDoc->m_Ell.right )
pDoc->m_Ell.left += 5;
if( pDoc->m_Ell.right -5 >= pDoc->m_Ell.left ) 
pDoc->m_Ell.right -= 5;
if( pDoc->m_Ell.top + 5 <= pDoc->m_Ell.bottom )
pDoc->m_Ell.top += 5;
if( pDoc->m_Ell.bottom - 5 >= pDoc->m_Ell.top )
pDoc->m_Ell.bottom -= 5;
Invalidate(  true );
CView::OnRButtonDown(nFlags, point);
}


6.在View类的OnMouseMove函数里添加:

void CMy7_9View::OnMouseMove(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
CMy7_9Doc *pDoc = GetDocument();
CRect clientRect;  
GetClientRect(&clientRect);  
int r = ( pDoc->m_Ell.right -pDoc->m_Ell.left ) / 2 ; 
if( nFlags & MK_CONTROL ) 
{       
pDoc->m_Ell.left = point.x-r;  
pDoc->m_Ell.top = point.y-r; 
pDoc->m_Ell.right = point.x+r; 
pDoc->m_Ell.bottom = point.y+r; 
}  
InvalidateRect(NULL,TRUE); 
CView::OnMouseMove(nFlags, point);
}

OK,大功告成~


你可能感兴趣的:(编写一个windows应用程序,要求在窗口的用户区中绘制一个圆 ,当单击左键时,该圆放大;单击右键时,该圆缩小;按下ctrl键时的同时鼠标移动,则该圆会随鼠标移动而移动)