Showing posts with label visual. Show all posts
Showing posts with label visual. Show all posts

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

3/18/09

Visual c++ IntersectRect example tutorial




In this visual c++ IntersectRect Example we will learn how to use the mfc intersection function


The intersected rectangle hatched in Red

to get the exefile click here and download the file and get the complete project and thinks of additions you can make



To start


1- Start a new project and let us name it IntersectRectangle

2- chose single document and deselect printing and print view no need for them

3- Add the following members to the view class

public:

BOOL m_b_IsIntersected; // weather there is intersection or not

CRect m_Gray_Rectangle , m_Moving_Rectangle ,m_RectIntersection ;

// constant REct and moving and the rectangle of intersection

make your data members public to avoid using getters and setters

4-Initilize the values of the two rectangles in the constructor as follows


CIntersectrectangleView::CIntersectrectangleView():m_Moving_Rectangle(100,60,160,120),m_Gray_Rectangle(90,80,120,110),m_b_IsIntersected(FALSE)

5-

Add the following messege handler to the derived view class


WM_TIMER ,WM_LBUTONDOWN, WM_RBUTTONDOWN

and edit them as follows

start with Wm timer


void CIntersectrectangleView::OnTimer(UINT nIDEvent) {

if(m_Moving_Rectangle.right> 300) m_Moving_Rectangle.OffsetRect(-280,0) ;

m_Moving_Rectangle.OffsetRect(20,0);InvalidateRect(NULL,TRUE); CView::OnTimer(nIDEvent);}


and

void CIntersectrectangleView::OnRButtonDown(UINT nFlags, CPoint point) { KillTimer(1);



CView::OnRButtonDown(nFlags, point);}


and


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

SetTimer(1, 450,NULL) ;

CView::OnLButtonDown(nFlags, point);

}


6-


edit the ondraw member functions as follows


void CIntersectrectangleView::OnDraw(CDC* pDC){






pDC->TextOut(60,13,"Press the right button to start moving the black rectangle and the right to stop it and notice the intersection");

pDC->SelectStockObject(BLACK_BRUSH);

pDC->Rectangle(m_Moving_Rectangle);

pDC->SelectStockObject(LTGRAY_BRUSH);

pDC->Rectangle(m_Gray_Rectangle);

LPRECT lp_move = m_Moving_Rectangle.operator LPRECT() ;

// cast the moving rect to LPRECT

LPRECT lp_gray = m_Gray_Rectangle.operator LPRECT() ;

// and so as to the grayrect



m_b_IsIntersected = m_RectIntersection.IntersectRect( lp_move ,lp_gray);
// find whethere there is intersection and draw the intersection rect with hatchbrush
if( m_b_IsIntersected){



CBrush brushHatch(HS_DIAGCROSS, RGB(255, 90, 0));

pDC->SelectObject(brushHatch) ;

pDC->Rectangle(m_RectIntersection);
//I used getstockobject because it is easier to use

}

8- finally build the application and see how it works





Do not forget to use casting when using intersictRect it only accept arguments of type LPRECT

for more example see the rest of the blog


I hope that you enjoyed this tutorial









}



2/3/09

CFormView Example



In this Visual c++ CformView Example we used a class derived instead of CDialog
This applications contains 2 buttons one for devision and one for multiplication and it has 2 Edit controls to hold the 2 number involved in the arithmetic operation

After pressing the devision button here what you get

To create the application
1-Start a new MFC application wizard chose a name for the project let us CFORMVIEWEX or any thing u like
2-In the last step chose CFormView as the parent of your derived view class

3-Start building the dialog as shown above chose IDC_EDIT1 ,IDC_EDIT2 For the numbers on the lefts ,IDC_RESULT FOr the result number IDC_MULTIPLY FOR multiplication button finally chose IDC_DEVIDE for the devision

4-In class wizard the following member variables m_number1 for the first edit control NUM1 ;
m_number2 for the second edit control second number and finally for the result m_results do not forget to chose CString type for all of the variables
5- Add messege handler for the 2 buttons and name them Mulitiplication and Devide Respectively and edit the code of those functions

Here what you get the code is marked in Red

void CCFormViewExView::Mulitiplication()
{


double first_number , second_number , result;

UpdateData();// Read data

first_number = atof( m_number1) ;
second_number = atof( m_number2);

result = first_number * second_number ;

m_results.Format("%.3f", result) ;
UpdateData(FALSE); // WriteData

}

and the second function

as follows

void CCFormViewExView::Devide()
{



double NumA ,NumB , results ;

UpdateData();
NumA = atoi( m_number1);
NumB = atoi(m_number2);
results = NumA/NumB ;
m_results.Format("%.5f", results) ;
UpdateData(FALSE);
}

6- Finaly build the application stard putting numbers on the edit control and see the results
note the effect of using different parameters in Format function where Devision has 5 decimal numbers than Multiplication which has only 3

To download the whole project click here fullproject and if you want the exe file click here Exefile

This post contains many typos like devide and others i made them for search engines intentionaly Loll