MFC OnSize事件调用时间

来源:互联网 发布:软件研发部门简介 编辑:程序博客网 时间:2024/06/07 09:42
在Ocx控件被加载时,第一次调用OnSize事件实在控件中的各个部件未加载前,此时如果操作控件中的控件会出现控制针异常,需要提前判断。

CTestDlg m_dlg;


int CTestCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO:  在此添加您专用的创建代码
m_Mgr->WriteLog("1");
m_dlg.Create(IDD_DLG, this);
m_Mgr->WriteLog("2");
return 0;
}


BOOL CTestDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();


// TODO:  在此添加额外的初始化
m_mgr->WriteLog("1.1");
vlcPlayer_ = new VLCWrapper();
vlcPlayer_->SetOutputWindow(this->GetDlgItem(IDC_STATIC_VLC)->m_hWnd);  
vlcPlayer_->SetEventHandler(&HandleVLCEvents, this);


return TRUE;  // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}






void CTestDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);


// TODO: 在此处添加消息处理程序代码
CString strLog;
strLog.Format("OnSize Change [cx:%d] [cy:%d]",cx,cy);
m_mgr->WriteLog(strLog);
RecalcLayout(cx,cy);
}


void CTestDlg::RecalcLayout(int cx, int cy)
{
if(cx==0 && cy==0)
{
CRect rect;
GetClientRect(&rect);
cx=rect.Width();
cy=rect.Height();
}
CWnd* _pWnd = this->GetDlgItem(IDC_STATIC1);
if(_pWnd != nullptr){
_pWnd->MoveWindow(CRect(1,1,cx-1, cy - 1));
}
}

日志如下:
2017-06-07 22:54:03  {----------------------OCX Init---------------------}
2017-06-07 22:54:03  {1}
2017-06-07 22:54:03  {OnSize Change [cx:467] [cy:359]}
2017-06-07 22:54:03  {1.1}
2017-06-07 22:54:04  {2}
2017-06-07 22:54:04  {OnSize Change [cx:100] [cy:50]}
2017-06-07 22:57:20  {------------------------------UnInit-----------------------}