每打开一个文档后,由于光标总是指在第一行第一列,我想在
状态栏指示该状态,所以我在CMyDoc的OnNewDocument()函数里面加了如下
的语句:
BOOL CPLEditorDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE; ((CEditView*)m_viewList.GetHead())->SetWindowText(NULL); // TODO: add reinitialization code here
// (SDI documents will reuse this document)
//下面是我加的
CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd();
CStatusBar* pbar=&pFrame->m_wndStatusBar;
if(pbar)
{
pbar->SetPaneText(1, "line: 1");
pbar->SetPaneText(2, "col: 0");
}
return TRUE;
}执行时老是出现运行错误,我该如何改正这个错误,或是有什么其他方法
解决上面的问题?敬请指点!
状态栏指示该状态,所以我在CMyDoc的OnNewDocument()函数里面加了如下
的语句:
BOOL CPLEditorDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE; ((CEditView*)m_viewList.GetHead())->SetWindowText(NULL); // TODO: add reinitialization code here
// (SDI documents will reuse this document)
//下面是我加的
CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd();
CStatusBar* pbar=&pFrame->m_wndStatusBar;
if(pbar)
{
pbar->SetPaneText(1, "line: 1");
pbar->SetPaneText(2, "col: 0");
}
return TRUE;
}执行时老是出现运行错误,我该如何改正这个错误,或是有什么其他方法
解决上面的问题?敬请指点!
((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
m_viewList.GetHead()返回NULL?
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};只需要简单的替换成static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_SEPARATOR,
ID_SEPARATOR, };
就行了。当然还可以继续加
不过你这样获得状态栏指针太麻烦了:CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd();
CStatusBar* pbar=&pFrame->m_wndStatusBar;还得将CMainFrame 的 m_wndStatusBar改成public的。其实可以改成:CStatusBar* pbar = (CStatusBar*)AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);另外可以通过GetPaneInfo 和 SetPaneInfo 来改变状态栏的特性。具体就不罗嗦了!good luck
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_SEPARATOR,
ID_SEPARATOR, };
我当然改过了,而且我该肯定下面的这句没有错:
((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
因为我的CMyView继承自CEditView, 这一句是MFC自动生成的。我想大家是没有搞清楚我的错误是什么,我的程序编译没有错误,
当执行时就会弹出一个对话框,上面显示: Debug Assertion Failed!
....
....
(Press Retry to debug the application) 终止 重试 忽略就是很常见的那种运行时错误!
在用MFC编程时,窗口的构造有一定的过程,比如说一个基于对话框的程序,
我们必须在InitDialog()函数里面初始化对话框的子控件,而不能在它的构造函数里面做初始化工作,为什么?因为在构造函数执行时,子控件还不存在,如果强行初始化就会出现和上面一样的错误,所以我估计上面的出错原因应该是类似的,关键是我不知道如何绕道而行来解决这个问题,大家不会没有
遇到过吧!
里面加了如下的语句:
.....
CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd();
CStatusBar* pbar=&pFrame->m_wndStatusBar;
if(pbar)
pbar->SetPaneText(0, "aaaaaaa");
.....
就可以得到和我一样的错误,麻烦你试一下,看看能不能解决这个问题!
...
m_pMainWnd = pMainFrame;而状态栏在MainFrame类中,主窗口已经存在了,(m_pMainWnd有值),CDocument的OnNewDocument调用在它之后,所以合法。
可是单文档只有在调用ProcessShellCommand时m_pMainWnd才有值,所以非法。建议你在ProcessShellCommand调用之后再
CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd();
比如在View类中?