Showing posts with label microsoft. Show all posts
Showing posts with label microsoft. Show all posts

1/26/09

Microsoft visual c++ toolbar example

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 program is similar to a previous program that used menu items to control the position of a movable rectangle
There 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 and assign the following IDs to them ID_SHIFT_LEFT ,ID_SHIFT_RIGHT ,ID_MOVE_UP and ID_MOVE_DOWN sorry for the unpretty buttoms !!

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
and after moving it for some bushes on the button you will get similar to this and try to move near the corners to see what happens there


to dowbload the whole project clich the link here

and the exe file

and do not forget to add accelerator use GetClientRect instead of the border Rect

1/17/09

visual c++ Status bar example

1- Make single document MFC application and deselect printing and printview options
2- In the view menu chose resource and then add New resource and then add ID_TIME_STATUSBAR and accept the defaul value (101) .
3- Add message handler and update message handler in the CMainFrame class for both ID_VIEW_STATUS_BAR .
4- Re
place the previously created indicators array with single ID_SEPARATOR and make sure that the CMainFrame data member m_wndStatusBar is public instead of protected so that it can be accessed from the View class .
5- Replace if (!m_wndStatusBar.Create(this) with this, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM, ID_TIME_STATUSBAR) ;

6- Add the following code to ur CMainFrame.cpp
void CMainFrame::OnViewStatusBar()
{
m_wndStatusBar.ShowWindow((m_wndStatusBar.GetStyle() &
WS_VISIBLE) == 0);
RecalcLayout();

}
void CMainFrame::OnUpdateViewStatusBar(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck((m_wndStatusBar.GetStyle() & WS_VISIBLE) != 0);
}
7- Add the following function OnDraw to your View class pDC->TextOut(30, 30 ,Press the left key to see your computer current time on the status bar ");


8-
Edit OnDraw messege handler as follows

CTime currenttime = GetCurrentTime();
int sec , min , hours ;
hours = currenttime.GetHour();
min = currenttime.GetMinute();
sec = currenttime.GetSecond();
CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus = &pFrame->m_wndStatusBar;
CString strtime ;
strtime.Format( "%d %d %d ", hours,min , sec );
pStatus->SetPaneText(0, strtime );

Finally include #include "MainFrm.h" near the top with other include statements

to download the exe file click here

and the whole project here
this contains sum errors