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);}
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);}
CView::OnRButtonDown(nFlags, point);}
and
void CIntersectrectangleView::OnLButtonDown(UINT nFlags, CPoint point) {
SetTimer(1, 450,NULL) ;
CView::OnLButtonDown(nFlags, 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() ;
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
//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
}
moncler, moncler outlet, links of london, bottes ugg, moncler, moncler, doudoune canada goose, sac louis vuitton pas cher, juicy couture outlet, canada goose, moncler, pandora charms, pandora charms, replica watches, louis vuitton, pandora jewelry, lancel, canada goose, louis vuitton, ugg boots uk, swarovski, louis vuitton, canada goose uk, wedding dresses, moncler, ugg,uggs,uggs canada, hollister, supra shoes, juicy couture outlet, toms shoes, canada goose outlet, ugg pas cher, montre pas cher, canada goose, moncler, karen millen, swarovski crystal, louis vuitton, pandora jewelry, ugg,ugg australia,ugg italia, marc jacobs, thomas sabo, coach outlet, moncler, canada goose, canada goose outlet
ReplyDeleteoakley sunglasses, polo ralph lauren outlet, tiffany jewelry, nike roshe run, louboutin pas cher, tiffany and co, kate spade outlet, oakley sunglasses, oakley sunglasses, nike free, louboutin outlet, prada outlet, christian louboutin outlet, tory burch outlet, ray ban sunglasses, jordan shoes, louis vuitton, longchamp outlet, ralph lauren pas cher, louis vuitton outlet, ugg boots, longchamp pas cher, ray ban sunglasses, oakley sunglasses, replica watches, polo ralph lauren outlet, uggs on sale, air max, louis vuitton outlet, michael kors, louis vuitton, prada handbags, longchamp, nike free, chanel handbags, sac longchamp, burberry, louis vuitton, nike air max, gucci outlet, nike outlet, louboutin, ugg boots, nike air max, replica watches, air jordan pas cher, ray ban sunglasses, louboutin shoes, longchamp outlet, cheap oakley sunglasses
ReplyDeletenike air max, hermes, oakley pas cher, burberry, coach outlet, coach outlet, lululemon, new balance pas cher, ugg boots, replica handbags, lacoste pas cher, vans pas cher, true religion jeans, michael kors, burberry outlet online, converse pas cher, true religion jeans, michael kors, hogan, sac guess, tn pas cher, coach purses, michael kors outlet, abercrombie and fitch, nike free run uk, michael kors, hollister pas cher, true religion outlet, kate spade handbags, vanessa bruno, timberland, ugg boots, nike air max, michael kors, north face, mulberry, north face, michael kors outlet, nike blazer, michael kors outlet, nike roshe, ralph lauren uk, michael kors outlet, air force, ray ban uk, nike air max, michael kors outlet, hollister, true religion jeans, ray ban pas cher
ReplyDeletejimmy choo shoes, converse outlet, north face outlet, insanity workout, soccer jerseys, wedding dresses, nike air max, ralph lauren, mcm handbags, oakley, hollister, lululemon, mont blanc, valentino shoes, vans shoes, vans, abercrombie and fitch, nike huarache, new balance, hollister, bottega veneta, nike trainers, birkin bag, celine handbags, ghd, soccer shoes, asics running shoes, instyler, timberland boots, beats by dre, nfl jerseys, converse, reebok shoes, nike roshe, hollister, ferragamo shoes, p90x workout, gucci, louboutin, north face outlet, iphone cases, babyliss, chi flat iron, baseball bats, longchamp, ray ban, nike air max, herve leger, mac cosmetics
ReplyDelete