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
Originally from an electronics background, I moved into software in 1996, partly as a result of being made redundant, and partly because I was very much enjoying the small amount of coding (in-at-the-deep-end-C) that I had been doing!
I swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this I moved to the company for which I currently work, which specialises in Configuration Management software, and currently program mainly in C/C++, for Windows. I have been gradually moving their legacy C code over to use C++ (with STL, MFC, ATL, and WTL). I have pulled in other technologies (Java, C#, VB, COM, SOAP) where appropriate, especially when integrating with third-party products.
In addition to that, I have overseen the technical side of the company website (ASP, VBScript, JavaScript, HTML, CSS), and have also worked closely with colleagues working on other products (Web-based, C#, ASP.NET, SQL, etc).
For developing, I mainly use Visual Studio 2010, along with an in-house-designed editor based on Andrei Stcherbatchenko's
syntax parsing classes, and various (mostly freeware) tools. For website design, I use Dreaweaver CS3.
When not developing software, I enjoy listening to and playing music, playing electric and acoustic guitars and mandolin.