1/23/09

Visual c++ Menu Example

download
In this Visual c++ menu example we Top level Move with 4 pop_up items Up ,Down ,Left and Right to move a rectangle rcRect inside the borders of a bigger rectangle rcBorder .




working example
To create the program
1- Start new Single document MFC exe application and deselect printing and print view options
2-In the resource View to IDR_MAINFRAME menu and make add the following items as shown in Fig 1






Downloadshttp://www.4shared.com/file/88832447/4d0b1fea/Menu_Move_Rect.html
3- In your derived view class add the following member functions and make them public

BOOL m_bLeft,m_bRight ,m_bDOWN , m_bUp ; // these are the left right top bottom borders indicators

CRect rcRect ,rcBorder ; // small rectangle and the bigger rectangle rcBorder

4-In your derived view class initilise the date as shown
CMove_RectView::CMove_RectView():rcRect(100,100,50,50),rcBorder( 20 ,20 ,300,300)
{

m_bLeft =m_bRight = m_bDOWN =m_bUp =FALSE ;


}




5-Add messege handlers to your menuitem IDs



void CMove_RectView::OnMoveUp() { rcRect.OffsetRect(0 ,-20); m_bDOWN =FALSE ; if((rcRect.top -70) <= rcBorder.top) m_bUp =TRUE ; InvalidateRect(NULL ,TRUE ); } void CMove_RectView::OnUpdateMoveUp(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bUp); } void CMove_RectView::OnMoveDown() { rcRect.OffsetRect(0, 20); m_bUp =FALSE ; if((rcRect.bottom) >= (rcBorder.bottom -70) ) m_bDOWN =TRUE ; InvalidateRect(NULL ,TRUE ); } void CMove_RectView::OnUpdateMoveDown(CCmdUI* pCmdUI) { pCmdUI->Enable(! m_bDOWN) ; } void CMove_RectView::OnMoveLeft() { if( (rcRect.left) <=rcBorder.left + 70) m_bLeft = TRUE ; rcRect.OffsetRect(-10 , 0); m_bRight = FALSE ; InvalidateRect(NULL ,TRUE ); } void CMove_RectView::OnUpdateMoveLeft(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bLeft); } void CMove_RectView::OnMoveRight() { rcRect.OffsetRect(20 ,0); m_bLeft = FALSE ; InvalidateRect(NULL ,TRUE ); if((rcBorder.right)<= rcRect.right) m_bRight=TRUE ; } void CMove_RectView::OnUpdateMoveRight(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bRight); }





6- Finally edit the OnDraw function in your view class as follows

void CMove_RectView::OnDraw(CDC* pDC)
{


pDC->Rectangle(rcBorder) ;
if( m_bLeft |m_bRight |m_bDOWN |m_bUp)
pDC->SelectStockObject(DKGRAY_BRUSH ) ;
// if the small rect near the big rect

pDC->Rectangle(rcRect);

}
7- Build the application and see how it works


8- Try to move the small Rect with the menu options UP DOWN LEFT AND RIGHT And when move near the borders of the big rectangle you get something like in the fig 2



to download the whole project clich here
and the exe file click here

things to add are keyboad accelerators to do

3 comments:

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

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

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

    ReplyDelete

leave me messege