Microsoft visual c++ toolbar example use toolbar buttons to move movable rectangle m_rect_movable in four directions up, down , left and right and when intersected with a fixed rectangle m_rect_fixed it draws new rectangle with red - hatched pattern and also it the movable rectangle become blue hatched when it approaches the border rectangle m_rectBorder .
This prog
ram is similar to a previous program that used menu items to control the position of a movable rectangleThere is 2 things to check the first one is to check the intersection between the movable rectangle and the fixed rectangle so that draw a hatched pattern in the intersection rectangle I could not use IntersectRect function because it takes LPCRECT as parameters and the other thing to check was if the movable rectangle is close to the border rectangle ,you can GetClientRect function to get the window's client area instead of the border rectangle
To build the application
1- Chose new and build single doc application and name it like TOOLBAR_MOVE_EX
2- In the resource view go to toolbar and delete some of the default buttoms and add 4 buttoms as shown in the fig below
3-Add the following data members to your derived CView class ( 3 rectangles and 5 boolean variables)
CRect m_rectBorder , m_rect_movable, m_rect_fixed ; // border ,movable and fixed rectangles respectively
BOOL m_bLeftBorder ,m_bRightBorder,m_bTopBorder ,m_bButtomBorder ,m_bIntersect;
// border and intersection indicators
4- In the view class initilize your data members as follows
CTOOLBAR_MOVE_EXView::CTOOLBAR_MOVE_EXView():m_rectBorder(10 ,10,340,340),m_rect_fixed(100,100,200,200) ,m_rect_movable(40,30,90,130)
{
m_bLeftBorder =m_bRightBorder =m_bTopBorder =m_bButtomBorder =m_bIntersect==FALSE ;
}
5- In your view add windows messege handlers for command and update command as follows
void CTOOLBAR_MOVE_EXView::OnShiftLeft()
{
if(m_rect_movable.left <=(m_rectBorder.left +40)) m_bLeftBorder = TRUE ; m_rect_movable.OffsetRect(-10,0); m_bRightBorder = FALSE ; if (m_rect_fixed.PtInRect(m_rect_movable.TopLeft()) | m_rect_fixed.PtInRect(m_rect_movable.BottomRight())| m_rect_fixed.PtInRect(CPoint(m_rect_movable.right,m_rect_movable.top))|m_rect_fixed.PtInRect(CPoint( m_rect_movable.left,m_rect_movable.bottom))) { m_bIntersect =TRUE ; } InvalidateRect(NULL,TRUE); } void CTOOLBAR_MOVE_EXView::OnUpdateShiftLeft(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bLeftBorder);
}
void CTOOLBAR_MOVE_EXView::OnShiftRight()
{
if(m_rect_movable.right >= ( m_rectBorder.right -30 ))
m_bRightBorder = TRUE ;
m_rect_movable.OffsetRect(10,0);
m_bLeftBorder = FALSE ;
if (m_rect_fixed.PtInRect(m_rect_movable.TopLeft()) | m_rect_fixed.PtInRect(m_rect_movable.BottomRight())| m_rect_fixed.PtInRect(CPoint(m_rect_movable.right,m_rect_movable.top))|m_rect_fixed.PtInRect(CPoint( m_rect_movable.left,m_rect_movable.bottom))) {
m_bIntersect =TRUE ;
}
InvalidateRect(NULL,TRUE);
}
void CTOOLBAR_MOVE_EXView::OnUpdateShiftRight(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_bRightBorder);
}
void CTOOLBAR_MOVE_EXView::OnMoveDown()
{
if( m_rect_movable.bottom >= ( m_rectBorder.bottom -20))
m_bButtomBorder = TRUE ;
m_rect_movable.OffsetRect(0,10) ;
m_bTopBorder = FALSE ;
if (m_rect_fixed.PtInRect(m_rect_movable.TopLeft()) | m_rect_fixed.PtInRect(m_rect_movable.BottomRight())| m_rect_fixed.PtInRect(CPoint(m_rect_movable.right,m_rect_movable.top))|m_rect_fixed.PtInRect(CPoint( m_rect_movable.left,m_rect_movable.bottom))) {
m_bIntersect =TRUE ;
}
InvalidateRect(NULL,TRUE);
}
void CTOOLBAR_MOVE_EXView::OnUpdateMoveDown(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_bButtomBorder);
}
void CTOOLBAR_MOVE_EXView::OnMoveUp()
{
if(m_rect_movable.top <= (m_rectBorder.top + 20)) m_bTopBorder =TRUE ; m_rect_movable.OffsetRect(0,-10) ; m_bButtomBorder = FALSE ; if (m_rect_fixed.PtInRect(m_rect_movable.TopLeft()) | m_rect_fixed.PtInRect(m_rect_movable.BottomRight())| m_rect_fixed.PtInRect(CPoint(m_rect_movable.right,m_rect_movable.top))|m_rect_fixed.PtInRect(CPoint( m_rect_movable.left,m_rect_movable.bottom))) { m_bIntersect =TRUE ; } InvalidateRect(NULL,TRUE); } void CTOOLBAR_MOVE_EXView::OnUpdateMoveUp(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bTopBorder);
}
6- Edit the OnDraw member function of your generated view
pDC->Rectangle(m_rectBorder) ; // white hallow
pDC->SelectStockObject(LTGRAY_BRUSH); // outer rectangle
pDC->Rectangle(m_rect_fixed); // fixed rectangle
CBrush bluebrush(HS_DIAGCROSS, RGB(0, 0, 250)); // blue brush for border condition when moveable rect on border
pDC->SelectStockObject(BLACK_BRUSH);
if(m_bLeftBorder |m_bRightBorder |m_bTopBorder |m_bButtomBorder)
pDC->SelectObject(bluebrush) ; // if in nearby the border rectangle
pDC->Rectangle(m_rect_movable);
if(m_bIntersect) {
CRect rc_intersection ;// use red hatch pattern
rc_intersection.IntersectRect(m_rect_movable ,m_rect_fixed);
CBrush brushHatch(HS_DIAGCROSS, RGB(255, 90, 0));
pDC->SelectObject(brushHatch) ;
pDC->Rectangle(rc_intersection);
}
7- try to move the movable rectangle around when you start the application you will start with a window like this