CStatus类更新状态条上的现实内容 访问冲突 问题

来源:互联网 发布:热点wifi软件下载 编辑:程序博客网 时间:2024/06/05 19:49

    一直想使用状态条动态显示文字。程序采用SDI不带Doc/View支持的架构。m_wndStatusBar可以访问,而且我就在MainFrm.cpp内使用。但是,我在使用GetPaneText()或者SetPaneText()总是出现访问冲突错误,其他相关的方法也是如此。一直觉得奇怪,这些方法都是属于CStatusBar类的,怎么就不能用了,不能用的话,什么方法用来达到同样的目的?

   终于找到有关的资料,提到CStatusBar类创建后,面板的默认值是禁用的。真觉得vc++怎么故意在制造障碍。搜到相关的描述如下:

 

  By default, a CStatusBar pane is notenabled when the pane is created. To activate a pane, you must call theON_UPDATE_COMMAND_UI() macro for each pane on the status bar and updatethe panes. Because panes do not send WM_COMMAND messages, you cannotuse ClassWizard to activate panes; you must type the code manually. Forexample, suppose one pane has ID_INDICATOR_PAGE as its identifier andthat it contains the current page number in a document. To make theID_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();
}

Todisplay text in the panes, either call SetPaneText() or callCCmdUI::SetText() in the OnUpdate() function. For example, you mightwant to set up an integer variable m_nPage that contains the currentpage 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);
}

Thistechnique causes the page number to appear in the pane during idleprocessing in the same manner that the application updates otherindicators.

 

照着上面的描述,算是把问题解决了。状态栏内自已定义的面板可以动态显示文本了。不过还是觉得VC++跟VB比较起来,做同样的事情,代码书写繁琐多了,而且代码的错误提示让人坠入迷雾,如果不去搜索引擎,很难找到问题的根本原因!

原创粉丝点击