Introduction
I have often found it useful to be able to attach data to a control in the same way as I would attach my own data to list items in a CListCtrl
or CComboBox
. For instance, I may want to indicate that a control contains a user name, or that it contains a file name, so that code which automatically builds commands etc from controls can process the contents of the control accordingly.
The CCtrlDataSupport
class presented here allows this support to be added to any control. (In fact it can be added to any class whether or not it is a control.)
OK, so the class is not exactly rocket science - it just adds a data member to your class, and a few functions to set and get the data - but I find I make a lot of use of this.
How to use
To use the class you add it to the list of classes from which your control is derived. So to add the support to a CComboBox you would have something like:
class CComboBoxData : public CComboBox, public CCtrlDataSupport
{
};
(Clearly the downside of this is that you then have to use CComboBoxData
instead of just using CComboBox
, but I rarely use the MFC classes as they are.)
The Class
The class to add control data is below:
class CCtrlDataSupport
{
public:
CCtrlDataSupport() : m_dwData(0) { };
LPVOID GetCtrlDataPtr() const { return m_lpData; }
void SetCtrlDataPtr(LPVOID lpData) { m_lpData = lpData; }
DWORD GetCtrlData() const { return m_dwData; }
void SetCtrlData(DWORD dwData) { m_dwData = dwData; }
private:
union { LPVOID m_lpData; DWORD m_dwData; };
};
History
Version 1 - 19 Feb 2003