Force BuildWindowCore member of a HwndHost derived class to be called

2.1k views Asked by At

I use a class derived from HwndHost to host a Win32 window. It is in turn used within a user control. That user control doesn't get shown (Visibility) unless the internal Win32 window gets successfully created. However, the BuildWindowCore method doesn't appear to be called unless the HwndHost window is visible, so I have a chicken & egg situation.

If a HwndHost derived class is not visible, is there another way to get it's BuildWindowCore method called?

2

There are 2 answers

0
Charles On BEST ANSWER

Well, a month has passed with no answers. Looks like I've stumped everyone including myself.

So, the answer as of .NET 4.0 is "No, there is no way to force BuildWindowCore to be called before the framework is ready to call it."

3
Jeap On

You can create your Win32 window yourself and just use HwndHost as a wrapper like in the example below.

ref class MyHost : HwndHost 
{
private:
   HWND  m_hWnd;
public:
   MyHost(HWND hWnd)
   {
      m_hWnd = hWnd;
   }
protected: 
  virtual HandleRef BuildWindowCore(HandleRef hwndParent) override 
  {
     // Simply re-parent the window
     SetParent(m_hWnd, (HWND) hwndParent.Handle.ToPointer());
     return HandleRef(this, (IntPtr) m_hWnd);
  } 

  virtual void DestroyWindowCore(HandleRef hwnd) override
  {
     ::DestroyWindow(m_hWnd);
  }
};