3/21/09

visual C++ Crect InflateRect Example




In this visual c++ CRect InflateRect example We build simple application that enlarge a rectangle and changing color
To initiate the move you press inside the small rectangle to get irregular enlargement or out side the rectangle to get regular inflation and multiple colors

To the Executable file click here InflatidREct_exe and the complete project theproject

Start Building the project

1- Start new MFC exe single document and name it like inflated_rect
2 - Add the following data members to your derived View class
public:
CRect m_rect_red ,m_rect_blue ;
CBrush m_bar_red ,m_br_green ,m_brush_blue ,m_br_orange ;


and initiliza them in Cview constructor as follows

CInflated_rectangleView::CInflated_rectangleView(): m_rect_red(40,40,120,400),m_rect_blue(70,210,90,230),m_bar_red(RGB(99,0,0)),m_br_green(RGB(0,99,0)),m_brush_blue(RGB(0,0,99)),m_br_orange(RGB(0xCC,0x99,33))
{

}


3- Add the following messege handlers to your derived View class WM_RBUTTONDOWN ,WM_LBUTTONDOWN ,WM_TIMER

and edit the functions they generate as follows

void CInflated_rectangleView::OnLButtonDown(UINT nFlags, CPoint point)
{

if(m_rect_blue.PtInRect(point)){
m_rect_blue.InflateRect(2,5,1,12) ; // irregular inflation
InvalidateRect(NULL,FALSE);
}
else
SetTimer(1, 300,NULL) ; CView::OnLButtonDown(nFlags, point);
}



void CInflated_rectangleView::OnRButtonDown(UINT nFlags, CPoint point)
{
KillTimer(1);
CView::OnRButtonDown(nFlags, point);
}



and finaly

void CInflated_rectangleView::OnTimer(UINT nIDEvent)
{
if( m_rect_blue.left>50)
m_rect_blue.InflateRect(4,1) ;
else
m_rect_blue.InflateRect(0,5);


if(m_rect_blue.top<>
m_rect_blue.SetRect(70,210,90,230); // return to first siza

InvalidateRect(NULL,TRUE);
}


4-

Finally edit t the ondraw function to as follows \

void CInflated_rectangleView::OnDraw(CDC* pDC)
{

pDC->TextOut(40,15," Press the left key to see the effect amd try to press inside the blue");

pDC->SelectObject( m_bar_red) ;
pDC->Rectangle( m_rect_red);
pDC->SelectObject(m_brush_blue) ;
if(m_rect_blue.left<=45)
pDC->SelectObject ( m_br_green);
if(m_rect_blue.top <=120)
pDC->SelectObject( m_br_orange);

pDC->Rectangle( m_rect_blue) ;


}




and then build the applications and enjoy it

6 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

leave me messege