STATUS BARS
1. Run AppWizard to generate statusbar application.
1. Use the string editor to edit the application’s string table resource.
- double click on the string table icon in the string table folder on the ResourceView page to bring up the string editor.
- then double click on the empty entry at the end of the list.
- assign the ID and string value in the dialog as:
String ID String Caption
ID_INDICATOR_LEFT LEFT
ID_INDICATOR_RIGHT RIGHT
3. Use Visual C++ to edit the application’s symbols.
- choose Resource symbols from the View menu.
- add the new status bar identifier, ID_MY_STATUS_BAR and accept the default value.
4. Use ClassWizard to add View menu command handlers in the class CMainFrame.
- add the following message handlers
Object ID Message Member Function
ID_VIEW_STATUS_BAR COMMAND OnViewStatusBar
ID_VIEW_STATUS_BAR UPDATECOMMAND_UI
OnUpdateViewStatusBar
5. Add the function prototypes to MainFrm.h file
afx_msg void OnUpdateLeft (CCmdUI* pCmdUI);
afx_msg void OnUpdateRight (CCmdUI* pCmdUI);
add the message handler statements inside the AFX_MSG brackets so that ClassWizard will access and edit the code later.
6. Edit the MainFrm.cpp file
- replace the original indicators array with the following code
static UINT indicators[] =
{
ID_SEPARATOR,
ID_SEPARATOR,
ID_INDICATOR_LEFT,
ID_INDICATOR_RIGHT< }; - edit the OnCreate member function if (!m_wndStatusBar.Create (this, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM | ID_MY_STATUS_BAR) || !m_wndStatusBar.SetIndicators (indicators, sizeof (indicators) / sizeof(UINT))); { TRACE0(“Failed to create status bar\n”); return -1; } Add the following message map entries for the class CMainFrame. ON_UPDATE_COMMAND_UI (ID_INDICATOR_LEFT, OnUpdateLeft) ON_UPDATE_COMMAND_UI (ID_INDICATOR_RIGHT, OnUpdateRight) Add the following CMainFrame member function that updates the two status indicators. void CMainFrame :: OnUpdateLeft (CCmdUI* pCmdUI) { pCmdUI -> Enable (::GetKeyState (VK_LBUTTON) <> Enable (::GetKeyState (VK_RBUTTON) <0);> SetCheck(m_wndStatusBar.GetStyle() &
WS_VISIBLE) != 0);
}
8. Edit the OnDraw function in statusView.cpp
void CstatusView :: OnDraw (CDC* pDC)
{
pDC -> TextOut (0,0, “Watch the status bar”);
}
9. Add a WM_MOUSEMOVE handler in the CstatusView class
void CstatusView :: OnMouseMove (UINT nFlags, CPoint point)
{
CString str;
CMainFrame* pFrame = (CMainFrame*) AfxGetApp() ->m_pMainWnd;
CStatusBar* pStatus = &pFrame -> m_wndStatusBar;
if (pStatus){
str.Format (“X =%d”, point.x);
pStatus -> SetPaneText (0, str);
str.Format (“Y=%d”, point.y);
pStatus -> SetPaneText(1,str);
}
}
Finally add the statement
#include “MainFrm.h”
near the top of the file statusView.cpp
9. Build and test the application.
No comments:
Post a Comment