65.9K
CodeProject 正在变化。 阅读更多。
Home

如何在基于 CWnd 的窗口上使用 Windows::Forms::UserControl

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.27/5 (5投票s)

2004年10月22日

viewsIcon

37683

本文介绍了如何在基于 MFC 的类中使用基于 .NET UserControl 的类。

引言

我尝试在一个现有的 MFC 应用程序中使用基于 .NET 的 UserControl。起初,一切似乎都很容易。
但是,当我尝试向我的 MFC 对话框添加一个成员时,我不得不意识到,
事情变得复杂了。首先,我开始编写一个有用的类,允许我
向 MFC 应用程序添加任何 .NET UserControl 的成员。

template <T>CdotNETCtl
{
public:
    CdotNETCtl(
void)
    {
    };

    ~CdotNETCtl(
void)
    {
    };
            
   
boolCreate(CWnd *pWndParent)
    {
       
boolbRetal =false;

       
try
       
{
            m_pCtl = T::Create();
            CWnd* pCtl =
newCWnd();
            m_pCtl->Show();
            pCtl->Attach((HWND)m_pCtl->GetHandle());
            pCtl->SetOwner(pWndParent);
            pCtl->SetParent(pWndParent);
            pCtl->Detach();
            删除pCtl;
            bRetal =
true;
        }
       
catch(...)
        {
        }
       
returnbRetal;
    };

    boolCreate(CWnd *pWndParent,intX,intY,intnewWidth,intnewHeight)
    {
       
boolbRetal =false;

        try
       
{
            m_pCtl = T::Create();
            CWnd* pCtl =
newCWnd();
            m_pCtl->Show();
            pCtl->Attach((HWND)m_pCtl->GetHandle());
            pCtl->SetOwner(pWndParent);
            pCtl->SetParent(pWndParent);
            pCtl->Detach();
            删除pCtl;
            m_pCtl->Left = X;
            m_pCtl->Top = Y;
            m_pCtl->Width = newWidth;
            m_pCtl->Height = newHeight;

            bRetal =
true;
        }
       
catch(...)
        {
        }
       
returnbRetal;
    };

    T*operator->()
    {
       
if(!m_pCtl)
        {
           
throw;
        }
       
returnm_pCtl;
    }
私有的:
    gcroot<T*> m_pCtl;
};

© . All rights reserved.