Re: SetWindowPos in Onsize
"mosfet" <john.doe@anonymous.org> wrote in message
news:470c9b7d$0$12074$426a74cc@news.free.fr...
mosfet a ?crit :
Hi,
Why does the following code fails ?
I have a CFormView called CTestCxStaticView and inside a CStatic derived
control called m_ctlText.
The application starts to load and then unload without displaying my form
view.
When I comment the SetWindowPos it works ....
void CTestCxStaticView::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
//TRACE(_T("CFormView::OnSize(%d, %d, %d)\n"), nType, cx, cy);
m_ctlText.SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOMOVE|SWP_NOZORDER);
}
Ok I found. I did this :
void CTestCxStaticView::DoLayout()
{
CRect rc;
GetClientRect( &rc );
if ( ::IsWindow( m_hWnd ) && (::IsWindow(m_ctlText.GetSafeHwnd())) )
m_ctlText.SetWindowPos(NULL, 0, 0, rc.Width(), rc.Height(), SWP_NOZORDER);
}
// CTestCxStaticView message handlers
void CTestCxStaticView::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
DoLayout();
}
The first time CTestCxStaticView::OnSize() is called, the child controls
including m_ctlText have not been creatd yet, so in the first try you did
not check IsWindow(m_ctlText.GetSafeHwnd()) before calling SetWindowPos on
it and thus called it on a NULL HWND which is why the app terminated. But
in the second try (when you moved the code to DoLayout()) you added the
check and thus it did not crash.
-- David