CreateControl too "early"

Discussion in 'MFC' started by Mijin, Mar 7, 2011.

  1. Mijin

    Mijin New Member

    Joined:
    Mar 7, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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);
    }
    
     
  2. Mijin

    Mijin New Member

    Joined:
    Mar 7, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Obviously [noparse]"COleControlSite::DetachWindow" [/noparse]should be
    Code:
    COleControlSite::DetachWindow
     
  3. Mijin

    Mijin New Member

    Joined:
    Mar 7, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice