C++ MFC - code execution failure without throwing runtime error on CDialog::OnSize event (GetWindowRect)

StackOverflow https://stackoverflow.com/questions/23675914

  •  23-07-2023
  •  | 
  •  

문제

I'm trying to access control size properties inside a CDialog::OnSize event handler. Code compiles and runs, but seems to fail at GetWindowRect. No run time error is thrown that I can see, but the code including and following the GetWindowRect call silently fails to run.

I have ran in release and debug mode, with breakpoints in appropriate positions (which are not hit GetWindowRect). I have also used code that modifies some member variables following GetWindowRect as an additional test, which I can also see is not running.

virtual void CMainFormDialog::OnSize(UINT nType, int cx, int cy) 
{
    ....

    auto pOutputEdit = (CEdit*) GetDlgItem(CE_OutputEdit);

    CRect pOutputEditRect;

    // No code is executed following this statement... 

    pOutputEdit->GetWindowRect(&pOutputEditRect);

    ....
}

Is there something wrong with the way I'm trying to access properties of my CEdit control here, and why does it fail silently?

Thanks.

도움이 되었습니까?

해결책

OnSize is likely called as part of the creation of the dialog, at a point where CE_OutputEditmay not yet have been created. CE_OutputEdit is created and bound as part of OnInitDialog. Check the return value of GetDlgItem and only perform whatever you need to do when it returns non NULL.

If needed use a variable that's set to true after you call CDialog::OnInitDialog and done other necessary initialization. Then use this variable in OnSize to determine if you should do your processing. Regardless of this, you should still check the return value of GetDlgItem

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top