Click here to Skip to main content
Licence 
First Posted 22 Oct 2004
Views 28,949
Bookmarked 7 times

How to use Windows::Forms::UserControl on a CWnd based

By | 22 Oct 2004 | Article
This article describes how to place .NET UserControl based classes on a MFC based class.

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;
};

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

datenkabel

Systems Engineer

Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalcatch(...) {} PinsitebuilderUwe Keim4:29 23 Oct '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 22 Oct 2004
Article Copyright 2004 by datenkabel
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid