I'm a noob to MFC, and a noob to this site I'm trying to add a windowless active X control to a CWnd dialog. The problem is, if I attach my control during OnInitDialog, the control is later detached via COleControlSite:etachWindow, and is therefore not shown. I can get it to work by creating my control later, when the parent control is getting a WM_SIZE message for the second time...but this is really hacky and I don't understand it. So...the below code works, but I'd rather not have the hack / understand what's going on Code: CParentControl::CParentControl() { m_pChild = NULL; } void CParentControl::OnInitDialog() { // This is successful, but Windows detaches the control soon after CreateChild(); } LRESULT CParentControl::OnSize (UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if (!m_pChild) { if (lParam == 0) { // This is successful, but Windows detaches the control soon after CreateChild(); } else { // This works, and creates a permanent control! But why? CreateChild(); } } } void CreateChild () { CWnd * me = CWnd::FromHandle(m_hWnd); // This always returns a success code... BOOL ret = m_pChild->CreateControl( clsid, NULL, WS_VISIBLE|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, CRect(10,10,300,300), me, 0); }
Obviously [noparse]"COleControlSite:etachWindow" [/noparse]should be Code: COleControlSite::DetachWindow
Got it working by using a second wrapper (an activeX control). The code for adding the child control then, looks like this: Code: void CreateChild () { m_axWnd.reset(new CAxWindow()); if (!m_axWnd.get()) { return; } CRect rc; GetClientRect(&rc); m_axWnd->Create(m_hWnd, rc, NULL, WS_CHILD|WS_VISIBLE); _bstr_t clsStr = my_CLSID; m_axWnd->CreateControl(clsStr); } ...and the control can be added OnInitDialog and it will be persistent.