为什么我的OnSize函数执行了三次?请教一下

_Elegant 2012-09-04 09:10:36
[事情是这样的:我将窗口划分为3个小窗口,每个小窗口分别显示同样的对话框A和B。问题出现在创建每个小窗口的时候都会调用三次OnSize函数,不知道因为什么原因。


BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
CRect rect;
GetClientRect(&rect);
if (!m_wndSplitter.CreateStatic(this,1,3))
return FALSE;

if (!m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CFireView),CSize(rect.Width()*20/45,rect.Height()),pContext))
{
m_wndSplitter.DestroyWindow();
return FALSE;
}
if (!m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CFireView),CSize(rect.Width()*20/45,rect.Height()),pContext))
{
m_wndSplitter.DestroyWindow();
return FALSE;
}
if (!m_wndSplitter.CreateView(0,2,RUNTIME_CLASS(CFireView),CSize(rect.Width()/3,rect.Height()),pContext))
{
m_wndSplitter.DestroyWindow();
return FALSE;
}
return TRUE;
//return CFrameWnd::OnCreateClient(lpcs, pContext);
}


int CFireView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFormView::OnCreate(lpCreateStruct) == -1)
return -1;
if (!theApp.pMain[theApp.m_theview] && theApp.m_theview < VIEWNUM)
{
theApp.pMain[theApp.m_theview] = new CMainAction() ;
theApp.pMain[theApp.m_theview]->Create(IDD_MAINACTION ,this) ;
theApp.pMain[theApp.m_theview]->ShowWindow(SW_SHOW) ;

theApp.pRight[theApp.m_theview] = new CRightView() ;
theApp.pRight[theApp.m_theview]->Create(IDD_RIGHTVIEW ,this) ;
theApp.pRight[theApp.m_theview]->ShowWindow(SW_SHOW) ;
}
// TODO: Add your specialized creation code here

return 0;
}


void CFireView::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
CRect rect ,rect1 ,rect2 ;
GetClientRect(&rect) ;
if (theApp.m_theview >= 3)
{
theApp.m_theview = 0 ;
}

//左边窗口位置设定
rect1 = rect ;
rect1.right = rect1.left + 420 ;
theApp.pMain[theApp.m_theview]->MoveWindow(rect1) ;

//右边窗口位置设定
rect2 = rect1 ;
rect2.left = rect1.right ;
rect2.right = rect2.left + 355 ;
theApp.pRight[theApp.m_theview]->MoveWindow(rect2) ;

++ theApp.m_theview ;//在运行这里第二次之后就会出现错误
...全文
305 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
_Elegant 2012-09-04
  • 打赏
  • 举报
回复
感谢!!!!!!
Eleven 2012-09-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
引用 2 楼 的回复:
OnSize里先判断HWND窗口句柄是否为NULL
因为OnSize会被调用多次

问题是我刚建完第一个分割窗口就开始多次调用onsize函数了,导致theApp.pMain[theApp.m_theview](此时theApp.m_theview值为1)为空,再次调用theApp.pMain[theApp.m_theview]->MoveWindow(rect1)……
[/Quote]
不是说了吗?MoveWindow之前你需要先判断该窗口是否已经创建好了,调用CWnd::GetSafeHwnd()看看返回值是否不为NULL,不是NULL,你还可以调用MoveWindow
_Elegant 2012-09-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
OnSize里先判断HWND窗口句柄是否为NULL
因为OnSize会被调用多次
[/Quote]
问题是我刚建完第一个分割窗口就开始多次调用onsize函数了,导致theApp.pMain[theApp.m_theview](此时theApp.m_theview值为1)为空,再次调用theApp.pMain[theApp.m_theview]->MoveWindow(rect1);就出错了。
_Elegant 2012-09-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
void CFireView::OnSize(UINT nType, int cx, int cy)
在界面启动时,OnSize调用多次 正常的
解决办法不要用theApp.m_theview 变量来动态制定窗口
直接
theApp.pMain[0]->MoveWindow(rect1) ;//左边窗口
//右边窗口
theApp.pRight[theApp.m_theview]->M……
[/Quote]
本来顺序应该这样的:
m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CFireView),CSize(rect.Width()*20/45,rect.Height()),pContext);
CFireView::OnCreate(LPCREATESTRUCT lpCreateStruct);
CFireView::OnSize(UINT nType, int cx, int cy);
m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CFireView),CSize(rect.Width()*20/45,rect.Height()),pContext);
CFireView::OnCreate(LPCREATESTRUCT lpCreateStruct);
CFireView::OnSize(UINT nType, int cx, int cy);
·······
现在成了这样子:
m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CFireView),CSize(rect.Width()*20/45,rect.Height()),pContext);
CFireView::OnCreate(LPCREATESTRUCT lpCreateStruct);
CFireView::OnSize(UINT nType, int cx, int cy);
CFireView::OnSize(UINT nType, int cx, int cy);
CFireView::OnSize(UINT nType, int cx, int cy);
我把 ++ theApp.m_theview 注释掉后第一个窗口可以正确显示出来,但是后面两个是空的。
gameslq 2012-09-04
  • 打赏
  • 举报
回复
void CFireView::OnSize(UINT nType, int cx, int cy)
在界面启动时,OnSize调用多次 正常的
解决办法不要用theApp.m_theview 变量来动态制定窗口
直接
theApp.pMain[0]->MoveWindow(rect1) ;//左边窗口
//右边窗口
theApp.pRight[theApp.m_theview]->MoveWindow(rect2) ;
Eleven 2012-09-04
  • 打赏
  • 举报
回复
OnSize里先判断HWND窗口句柄是否为NULL
因为OnSize会被调用多次
_Elegant 2012-09-04
  • 打赏
  • 举报
回复
theApp.m_theview表示视图号。不是在++ theApp.m_theview ;这里出错。是在运行第二次onsize函数时 theApp.pMain[theApp.m_theview]指向地址为空。

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧