Introduction
I tried to use a .NET based UserControl within an existing MFC application. First, things
seemed to be easy. But when I tried to add a member to my MFC dialog, I had to recognize,
that things were getting complicated. First I started to write a useful class allowing me
to add a member of any .NET UserControl to MFC applications.
template <class T> class CdotNETCtl
{
public:
CdotNETCtl(void)
{
};
~CdotNETCtl(void)
{
};
bool Create(CWnd *pWndParent)
{
bool bRetal = false;
try
{
m_pCtl = T::Create();
CWnd* pCtl = new CWnd();
m_pCtl->Show();
pCtl->Attach((HWND)m_pCtl->GetHandle());
pCtl->SetOwner(pWndParent);
pCtl->SetParent(pWndParent);
pCtl->Detach();
delete pCtl;
bRetal = true;
}
catch(...)
{
}
return bRetal;
};
bool Create(CWnd *pWndParent, int X, int Y, int newWidth, int newHeight)
{
bool bRetal = false;
try
{
m_pCtl = T::Create();
CWnd* pCtl = new CWnd();
m_pCtl->Show();
pCtl->Attach((HWND)m_pCtl->GetHandle());
pCtl->SetOwner(pWndParent);
pCtl->SetParent(pWndParent);
pCtl->Detach();
delete pCtl;
m_pCtl->Left = X;
m_pCtl->Top = Y;
m_pCtl->Width = newWidth;
m_pCtl->Height = newHeight;
bRetal = true;
}
catch(...)
{
}
return bRetal;
};
T* operator->()
{
if(!m_pCtl)
{
throw;
}
return m_pCtl;
}
private:
gcroot<T*> m_pCtl;
};