可以自定义MFC窗口/对话框是类模板实例化吗? [英] Can a custom MFC window/dialog be a class template instantiation?
问题描述
MFC在创建对话框时使用了一些特殊的宏,在我的快速测试中,我遇到了一些奇怪的错误,试图编译一个模板对话框类。
There's a bunch of special macros that MFC uses when creating dialogs, and in my quick tests I'm getting weird errors trying to compile a template dialog class. Is this likely to be a big pain to achieve?
这是我试过的:
template <class W>
class CMyDlg : public CDialog
{
typedef CDialog super;
DECLARE_DYNAMIC(CMyDlg <W>)
public:
CMyDlg (CWnd* pParent); // standard constructor
virtual ~CMyDlg ();
// Dialog Data
enum { IDD = IDD_MYDLG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
private:
W *m_pWidget; //W will always be a CDialog
};
IMPLEMENT_DYNAMIC(CMyDlg<W>, super) <-------------------
template <class W>
CMyDlg<W>::CMyDlg(CWnd* pParent)
: super(CMyDlg::IDD, pParent)
{
m_pWidget = new W(this);
}
我得到了一大堆错误,但主要似乎是: p>
I get a whole bunch of errors but main one appears to be:
错误C2955:'CMyDlg':使用类
模板需要模板参数
list
error C2955: 'CMyDlg' : use of class template requires template argument list
我试过使用一些特殊的模板版本的宏,但它没有帮助太多,其他错误改变,但这一个仍然存在。注意我的代码都在一个文件,因为C ++模板不喜欢.h / .cpp像正常。
I tried using some specialised template versions of macros but it doesn't help much, other errors change but this one remains. Note my code is all in one file, since C++ templates don't like .h/.cpp like normal.
我假设有人必须在
推荐答案
//Template-enabled expansion of IMPLEMENT_DYNAMIC(CMyDlg,super)
template <class W> CRuntimeClass* PASCAL CMyDlg<W>::_GetBaseClass(){ return RUNTIME_CLASS(super); }
template <class W> AFX_COMDAT const CRuntimeClass CMyDlg<W>::CMyDlg= {
"CMyDlg", sizeof(CMyDlg<W>), 0xFFFF, NULL,&CMyDlg<W>::_GetBaseClass, NULL, NULL };
template <class W> CRuntimeClass* PASCAL CMyDlg<W>::GetThisClass() { return _RUNTIME_CLASS(CMyDlg); }
template <class W> CRuntimeClass* CMyDlg<W>::GetRuntimeClass() const { return _RUNTIME_CLASS(CMyDlg); }
这篇关于可以自定义MFC窗口/对话框是类模板实例化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!