看书上有一个SDI例子,是用来(静态)分割窗口的,并且可以使用菜单来切换两个视图(分别是CStringView显示字符串格式,CHexView显示相应的16进制)。切换视图的响应函数如下:enum EView{STRING = 1, HEX = 2};void CMainFrame::SwitchView(EView nView)
{
CView *pOldActiveView = GetActiveView();
CView *pNewActiveView = (CView*)GetDlgItem(nView); /*if (pNewActiveView == NULL)*/ {
switch(nView) {
case STRING:
pNewActiveView = (CView*)new CStringView();
break;
case HEX:
pNewActiveView = (CView*)new CHexView();
break;
}
} CCreateContext context;
context.m_pCurrentDoc = pOldActiveView->GetDocument(); pNewActiveView->Create(NULL, NULL, WS_BORDER, CFrameWnd::rectDefault, this, nView, &context);
pNewActiveView->OnInitialUpdate(); SetActiveView(pNewActiveView);
pNewActiveView->ShowWindow(SW_SHOW);
pOldActiveView->ShowWindow(SW_HIDE);
pOldActiveView->SetDlgCtrlID(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CStringView) ? STRING : HEX);
pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST); RecalcLayout();}
结果发现:程序运行后,首次点击菜单来切换两个视图的时候,可以实现效果。可是当第三次点击菜单的时候,程序就死掉了。调试一下,发现在 pNewActiveView->Create(NULL, NULL, WS_BORDER, CFrameWnd::rectDefault, this, nView, &context); 这句出错,跟踪进去是有一步获得窗口句柄为空导致的结果。
回来我把程序中一句(就是用/* */注释掉的那句/*if (pNewActiveView == NULL)*/)注释后,就可以一直正常的切换。那就是说if里面的语句执行,影响了后面的Create()函数。可是我不知道为什么?请指教!谢谢!!!
{
CView *pOldActiveView = GetActiveView();
CView *pNewActiveView = (CView*)GetDlgItem(nView); /*if (pNewActiveView == NULL)*/ {
switch(nView) {
case STRING:
pNewActiveView = (CView*)new CStringView();
break;
case HEX:
pNewActiveView = (CView*)new CHexView();
break;
}
} CCreateContext context;
context.m_pCurrentDoc = pOldActiveView->GetDocument(); pNewActiveView->Create(NULL, NULL, WS_BORDER, CFrameWnd::rectDefault, this, nView, &context);
pNewActiveView->OnInitialUpdate(); SetActiveView(pNewActiveView);
pNewActiveView->ShowWindow(SW_SHOW);
pOldActiveView->ShowWindow(SW_HIDE);
pOldActiveView->SetDlgCtrlID(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CStringView) ? STRING : HEX);
pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST); RecalcLayout();}
结果发现:程序运行后,首次点击菜单来切换两个视图的时候,可以实现效果。可是当第三次点击菜单的时候,程序就死掉了。调试一下,发现在 pNewActiveView->Create(NULL, NULL, WS_BORDER, CFrameWnd::rectDefault, this, nView, &context); 这句出错,跟踪进去是有一步获得窗口句柄为空导致的结果。
回来我把程序中一句(就是用/* */注释掉的那句/*if (pNewActiveView == NULL)*/)注释后,就可以一直正常的切换。那就是说if里面的语句执行,影响了后面的Create()函数。可是我不知道为什么?请指教!谢谢!!!
解决方案 »
- 在子对话框的按钮事件中用while(1){}等待主对话框全局变量的变化,怎么查询不到变化?
- 使用自绘按钮,按钮的响应速度变慢?
- 怎样检测操作系统是中文操作系统
- 24bit的bmp文件怎么通过调色板转换成灰度
- 创建出来的对话框指针为0x0000000000是怎么回事呀?(有代码,急)
- 请教高手:哪里有最新的关于内容过滤的资料啊?谢谢!
- 如何查看路由器中 各个手机的wifi信号强度
- vfw编程有关capSetVideoFormat()的问题?
- -----------请教ActiveX中关于变量和类的问题----------------
- 我想问各位是否曾何时觉得空虚觉得寂寞觉得冷呀?
- 界面重绘,看清楚问题再回答
- 重绘CListCtrl,重载了DrawItem函数,为啥在其中无法取到各行的文本?
{
CView *pOldActiveView = GetActiveView();
CView *pNewActiveView = (CView*)GetDlgItem(nView); if (pNewActiveView == NULL) {
switch(nView) {
case STRING:
pNewActiveView = (CView*)new CStringView();
break;
case HEX:
pNewActiveView = (CView*)new CHexView();
break;
}
CCreateContext context;
context.m_pCurrentDoc = pOldActiveView->GetDocument();
pNewActiveView->Create(NULL, NULL, WS_BORDER, CFrameWnd::rectDefault, this, nView, &context);
pNewActiveView->OnInitialUpdate();
} SetActiveView(pNewActiveView);
pNewActiveView->ShowWindow(SW_SHOW);
pOldActiveView->ShowWindow(SW_HIDE);
pOldActiveView->SetDlgCtrlID(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CStringView) ? STRING : HEX);
pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST); RecalcLayout();}