我就是做wtl的。例子的话,我写个最简单的吧 class Base : public CWindowImpl<Base> { public: BEGIN_MSG_MAP(Base) MSG_WM_LBUTTONDOWN(OnLButtonDown) END_MSG_MAP() private: void OnLButtonDown(UINT,CPoint){} }; class Label : public Base { BEGIN_MSG_MAP(Label) CHAIN_MSG_MAP(Base) 你转到源代码可以看实现代码,其实就是switch。这么做之后,Label也可以响应OnLButtonDown MSG_WM_PAINT(OnPaint) END_MSG_MAP() private: void OnPaint(CDCHandle){} };
wtl有个很大的优点:消息嵌入
CHAIN_MSG_MAP(BaseClass)
class Base : public CWindowImpl<Base>
{
public:
BEGIN_MSG_MAP(Base)
MSG_WM_LBUTTONDOWN(OnLButtonDown)
END_MSG_MAP()
private:
void OnLButtonDown(UINT,CPoint){}
};
class Label : public Base
{
BEGIN_MSG_MAP(Label)
CHAIN_MSG_MAP(Base) 你转到源代码可以看实现代码,其实就是switch。这么做之后,Label也可以响应OnLButtonDown
MSG_WM_PAINT(OnPaint)
END_MSG_MAP()
private:
void OnPaint(CDCHandle){}
};
CHAIN_MSG_MAP(Base) 你转到源代码可以看实现代码,其实就是switch。这么做之后,Label也可以响应OnLButtonDown --> 这是什么意思
MSG_WM_PAINT(OnPaint)
END_MSG_MAP()
这不就是我上面说的,要通过继承和嵌入来完成,而不是成员变量。
class CMainDlg
{
...
CMyUI m_uiDlg;
...
};class CMyUI
{
/* 怎么做? */
}
template <class T>
class PaintImpl
{
public:
BEGIN_MSG_MAP(PaintImpl)
MESSAGE_HANDLE(WM_PAINT,OnPaint)
END_MSG_MAP()
private:
LRESULT OnPaint(UINT,WPARAM,LPARAM,BOOL&)
{
T* t = (T*) this;
CPaintDC dc(t->m_hWnd);
dc.TextOut(10,10,"Just try");
}
};
class CMainDlg: public CDialogImpl<CMainDlg>, public PaintImpl<CMainDlg>
{
public:
BEGIN_MSG_MAP(CMainDlg)
CHAIN_MSG_MAP(PaintImpl<CMainDlg>)
END_MSG_MAP()
};
ok。