一直想使用状态条动态显示文字。程序采用SDI不带Doc/View支持的架构。m_wndStatusBar可以访问,而且我就在MainFrm.cpp内使用。但是,我在使用GetPaneText()或者SetPaneText()总是出现访问冲突错误,其他相关的方法也是如此。一直觉得奇怪,这些方法都是属于CStatusBar类的,怎么就不能用了,不能用的话,什么方法用来达到同样的目的?
终于找到有关的资料,提到CStatusBar类创建后,面板的默认值是禁用的。真觉得vc++怎么故意在制造障碍。搜到相关的描述如下:
By default, a CStatusBar pane is not enabled when the pane is created. To activate a pane, you must call the ON_UPDATE_COMMAND_UI() macro for each pane on the status bar and update the panes. Because panes do not send WM_COMMAND messages, you cannot use ClassWizard to activate panes; you must type the code manually. For example, suppose one pane has ID_INDICATOR_PAGE as its identifier and that it contains the current page number in a document. To make the ID_INDICATOR_PAGE pane display text, add the following to a header file (probably the MAINFRM.H file):
afx_msg void OnUpdatePage(CCmdUI *pCmdUI);
//Add the following to the application message map:
ON_UPDATE_COMMAND_UI(ID_INDICATOR_PAGE, OnUpdatePage)
//Add the following to a source code file (probably MAINFRM.CPP):
void CMainFrame::OnUpdatePage(CCmdUI *pCmdUI)
{
pCmdUI->Enable();
}
To display text in the panes, either call SetPaneText() or call CCmdUI::SetText() in the OnUpdate() function. For example, you might want to set up an integer variable m_nPage that contains the current page number. Then, the OnUpdatePage() function might read as follows:
void CMainFrame::OnUpdatePage(CCmdUI *pCmdUI)
{
pCmdUI->Enable();
char szPage[16];
wsprintf((LPSTR)szPage, "Page %d", m_nPage);
pCmdUI->SetText((LPSTR)szPage);
}
This technique causes the page number to appear in the pane during idle processing in the same manner that the application updates other indicators.
照着上面的描述,算是把问题解决了。状态栏内自已定义的面板可以动态显示文本了。不过还是觉得VC++跟VB比较起来,做同样的事情,代码书写繁琐多了,而且代码的错误提示让人坠入迷雾,如果不去搜索引擎,很难找到问题的根本原因!