SetWindowText时弹出异常,winocc.cpp Line: 246

SetWindowText时弹出错误,winocc.cpp Line: 246
本帖最后由 cr945211 于 2015-03-15 16:54:25 编辑
      创建了一个基于View的项目,主视图用于显示图形。另外添加了一个对话框(ProgramDlg),用于显示文本,由于一些功能要求,没有直接在对话框中添加编辑控件,而是另外添加了一个基于EditView的类ProgramWin,放在对话框中用于显示文本。
BOOL CProgramDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CRect clientRect;
    GetClientRect(&clientRect);
    CView* pNewView;
    pNewView = new CProgramWin;//ProgramWin为编辑区EditView
    // Creation of the view window
    afxTraceEnabled=FALSE;
    if(! pNewView->Create(NULL, NULL, WS_VISIBLE | WS_CHILD, CRect(clientRect.left,clientRect.top,clientRect.right-180,clientRect.bottom+90), this, 1000))//程序编辑区的大小
    {
        TRACE0( "Failed view creation\n" );
     }
    afxTraceEnabled=TRUE;

    pNewView->OnInitialUpdate();

return TRUE;  // return TRUE unless you set the focus to a control

}

对话框做成了悬浮停靠框,通过菜单命令OnGeneration()来显示文本。
void CProgramWin::OnGeneration()
{
        CString strText;
strPath=_T("projects\\main.cpp");
        CFile file(strPath,CFile::modeRead);
        char read[1000];
        file.Read(read,1000);
        for(int i=0;i<(file.GetLength());i++)
        {
            strText+=read[i];
        }
        file.Close();
SetWindowText(strText);
}
本来,将OnGeneration()放在了ProgramWin类中,可以在对话框中显示内容,但是只有点击对话框区域该菜单命令才有效,否则是灰色。所以,又将该菜单命令在View中添加了处理语句,显示该悬浮框,并指向CProgramWin中的OnGeneration()

void CProgramView::OnGeneration()
{
// TODO: 在此添加命令处理程序代码
CMainFrame* pMain=(CMainFrame*)AfxGetApp()->m_pMainWnd;
        pMain->m_ProPane.ShowPane(TRUE,FALSE,TRUE);//m_ProPane是定义在MainFrame.h中的CProPane   m_ProPane
pMain->m_wndProgram.OnGeneration();//m_wndProgram是定义在MainFrame.h中的CProgramWin   m_wndProgram
}

 但这是出现了错误,运行到SetWindowText()时出现问题SetWindowText时弹出异常,winocc.cpp Line: 246
看相关出现类似的问题,有大神说窗口类的构造函数在执行的时候,窗口还没有绘制好,还不能SetWindowText。但是我在断点调试的时候,在SetWindowText之前该对话框已经弹出来了。
请问现在问题怎么解决
------解决思路----------------------
你试试在setwindowtext前先setforegroundwindow激活ProgramWin
------解决思路----------------------
引用:
Quote: 引用:

你试试在setwindowtext前先setforegroundwindow激活ProgramWin

请问我怎么获得对话框的句柄?是获得ProgramDlg的句柄还是ProgramWin ?

首先我觉得你这个设计有问题
不过改设计显然已经不现实了
按着你的描述根据我的理解你应该获取ProgramDlg的句柄
至于怎么获取就要你自己想办法了
理论上来说ProgramDlg是子窗口所以无所谓获取句柄
因为窗口就是你创建的你肯定有窗口指针
------解决思路----------------------
在对话框中CView* pNewView;设成全局变量,将显示文本的程序放在对话框中, 然后
pNewView->SetWindowText(strText);