以下是引用片段: MFC对IOleObject::SetClientSite()的实现包含对虚函数COleControl::OnSetClientSite()调用。这时候可以根据m_pClientSite是否为空指针来判断控件是被加载还是被清除。 // CMyControl is derived from COleControl. void CMyControl::OnSetClientSite() { if (m_pClientSite) { // It doesn|t matter who the parent window is or what the size of // the window is because the control|s window will be reparented // and resized correctly later when it|s in-place activated. VERIFY (CreateControlWindow (::GetDesktopWindow(), CRect(0,0,0,0), CRect(0,0,0,0))); // Obtain URL from container moniker. CComPtr<IMoniker> spmk; LPOLESTR pszDisplayName; if (SUCCEEDED(m_pClientSite->GetMoniker( OLEGETMONIKER_TEMPFORUSER, OLEWHICHMK_CONTAINER, &spmk))) { if (SUCCEEDED(spmk->GetDisplayName( NULL, NULL, &pszDisplayName))) { USES_CONVERSION; CComBSTR bstrURL; bstrURL = pszDisplayName; ATLTRACE('The current URL is %s\n', OLE2T(bstrURL)); CoTaskMemFree((LPVOID)pszDisplayName); } } else DestroyWindow(); COleControl::OnSetClientSite(); } |