Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/forums/mfc/)
-   -   CreateControl too "early" (http://www.go4expert.com/forums/createcontrol-too-early-t25179/)

Mijin 7Mar2011 21:59

CreateControl too "early"
 
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::DetachWindow, 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);
}


Mijin 7Mar2011 22:01

Re: CreateControl too "early"
 
Obviously "COleControlSite::DetachWindow" should be
Code:

COleControlSite::DetachWindow

Mijin 8Mar2011 20:54

Re: CreateControl too "early"
 
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.


All times are GMT +5.5. The time now is 01:22.