![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Activating Status Bar IndicatorsThe list of status bar indicators is located in MainFrame.cpp. static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; To add a new indicator to the list, we simply add it to this list. We can add an overstrike (that is, Insert mode) indicator to our program now. It turns out that the program already has defined an (unused) indicator ID, ID_INDICATOR_OVR, for this purpose, so we simply add that to the list. static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, ID_INDICATOR_OVR, ⇐ }; Adding this new indicator ID automatically adds a fourth indicator (there are three by default) to the status bar. Now we can make that new indicator active. Because overstrike is a mode that toggles as the user presses the Ins key, we need to keep track of the overstrike state with a new variable, m_OVR, which we set to false (turning off overstrike) in the main frame windows constructor. ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { m_OVR = false; ⇐ } The caption associated with ID_INDICATOR_OVR is OVR in the string table, although you can set it to any caption you want and can even create your own indicators. Now we need some way of making that caption appear in its indicator box on the screen. We do that with an update function, OnUpdateOVR(). Unfortunately, we cant use ClassWizard to connect an update function to ID_INDICATOR_OVR. Furthermore, our previous trick of connecting an accelerator key to ID_INDICATOR_OVR so it shows up in ClassWizard wont work because indicator IDs, like ID_INDICATOR_OVR, cant take accelerator keys. Instead, we have to make the changes ourselves, adding the code necessary to support the new OVR indicators OnUpdateOVR() function. We do that by first declaring that function in MainFrm.h in a way that lets us use it in the message maps that ClassWizard creates. protected: //{{AFX_MSG(CMainFrame) afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnUpdateOVR(CCmdUI* pCmdUI); ⇐ . . . Next, we connect the indicator to the OnUpdateOVR() function so that function is called when the program displays the indicator. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) ON_WM_CREATE() ON_UPDATE_COMMAND_UI(ID_INDICATOR_OVR, OnUpdateOVR) ⇐ //}}AFX_MSG_MAP END_MESSAGE_MAP() Then, in OnUpdateOVR(), we set the indicators state to match our m_OVR variable. void CMainFrame::OnUpdateOVR(CCmdUI* pCmdUI) { pCmdUI->Enable(m_OVR); ⇐ } All thats left is to toggle the m_OVR variable as the user presses the Ins key, which we do now.
Using Accelerator Keys with IndicatorsTo toggle the m_OVR variable each time the user presses the Ins key, we add a new accelerator key to the accelerator table, ID_OVR, using the Accelerator Editor. Connect that accelerator to the Ins key (VK_Insert). Finally, use ClassWizard to connect ID_OVR to a function, OnOVR(); in that function, we can toggle m_OVR as follows: void CMainFrame::OnOVR() { m_OVR = !m_OVR; } Now when the user presses the Ins key, this function is called. It toggles m_OVR, which in turn toggles the OVR string in the overstrike indicator in the status bar, as shown in Figure 4.14. MainFrm.h and MainFrm.cpp appear in Listing 4.2, and ComboView.h and ComboView.cpp appear in Listing 4.3. Listing 4.2 MainFrm.h and MainFrm.cpp // MainFrm.h : interface of the CMainFrame class // ///////////////////////////////////////////////////////////////////////////// #if !defined(AFX_MAINFRM_H__D36DAA69_9E10_11D1_887F_D42B07C10710__INCLUDED_) #define AFX_MAINFRM_H__D36DAA69_9E10_11D1_887F_D42B07C10710__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CToolBar2 : public CToolBar { public: CComboBox m_Combo; }; class CMainFrame : public CFrameWnd { protected: // create from serialization only CMainFrame(); DECLARE_DYNCREATE(CMainFrame) boolean m_OVR; CToolBar2 m_wndToolBar; // Attributes public: // Operations public: int GetItemNumber() {return m_wndToolBar.m_Combo.GetCurSel();} // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMainFrame) virtual BOOL PreCreateWindow(CREATESTRUCT& cs); //}}AFX_VIRTUAL // Implementation public: virtual ~CMainFrame(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // control bar embedded members CStatusBar m_wndStatusBar; // Generated message map functions protected: //{{AFX_MSG(CMainFrame) afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnUpdateOVR(CCmdUI* pCmdUI); afx_msg void OnOVR(); // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_MAINFRM_H__D36DAA69_9E10_11D1_887F_D42B07C10710__INCLUDED_) // MainFrm.cpp : implementation of the CMainFrame class // #include stdafx.h #include Combo.h #include MainFrm.h #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code ! ON_WM_CREATE() ON_UPDATE_COMMAND_UI(ID_INDICATOR_OVR, OnUpdateOVR) ON_COMMAND(ID_OVR, OnOVR) //}}AFX_MSG_MAP END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, ID_INDICATOR_OVR, }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { m_OVR = false; // TODO: add member initialization code here } CMainFrame::~CMainFrame() { } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0(Failed to create toolbar\n); return -1; // fail to create } m_wndToolBar.SetButtonInfo(0, IDC_COMBO, TBBS_SEPARATOR, 150); m_wndToolBar.SetButtonInfo(1, ID_SEPARATOR, TBBS_SEPARATOR, 20); CRect rCombo; m_wndToolBar.GetItemRect(0, &rCombo); rCombo.bottom += 100; m_wndToolBar.m_Combo.Create(CBS_DROPDOWNLIST | WS_VISIBLE, rCombo, &m_wndToolBar, IDC_COMBO); CString Item; Item = Circle; m_wndToolBar.m_Combo.AddString(Item); Item = Rectangle; m_wndToolBar.m_Combo.AddString(Item); Item = Rounded Rectangle; m_wndToolBar.m_Combo.AddString(Item); if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0(Failed to create status bar\n); return -1; // fail to create } // TODO: Delete these three lines if you don t want the toolbar to // be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers void CMainFrame::OnUpdateOVR(CCmdUI* pCmdUI) { pCmdUI->Enable(m_OVR); } void CMainFrame::OnOVR() { m_OVR = !m_OVR; }
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |