Introduction
As I was working on an application with a property sheet, I realized that James Twine's Dim Edit Control would be perfect for presenting some of the information. However, his control was written in MFC, and I was using WTL. And so, my WTL Dim Edit control was born!
I purposely didn't put in as many features as James has in his, because I am trying to keep the code behind my property pages as light as possible. However, I wanted to be a little more flexible about where the "dim text" string was stored, and how it was displayed. What I ended up with is a mixture of WTL and STL.
Implementation
The dim text string is stored using an STL std::basic_string < TCHAR >,
which allows the control to be compiled for ANSI or Unicode use. Also, doing it this way does not impose an arbitrary limit on the length of the dim text. Next, notice that the dim text is not actually stored as the "window" text, which means the control does not have to make sure that the correct string is stored for when the client application requests it.
Message handlers for WM_SETFOCUS
and WM_KILLFOCUS
are used to set a simple boolean flag for whether or not the dim text should be displayed in the control. The WM_PAINT
handler looks at this flag to decide if the dim text or the control's window text should be displayed. Windows is queried for the fonts and colors to use, so your users can use any theme they like, and the control will perform identically to the normal edit control.
Usage
To use this control in your WTL application, just add a normal edit control to your dialog or window, and then subclass it. Your code will look something like:
#include <DimEdit.h>
class CMainDlg : public CDialogImpl< CMainDlg >
{
public:
OnInitDialog( HWND hWnd, LPARAM lParam )
{
...
m_dimEdit.SubclassWindow( GetDlgItem( IDC_EDIT1 ) );
m_dimEdit.SetDimText( "This is the dim text" )
.SetDimColor( RGB( 192, 192, 192 ) );
}
private:
CDimEdit m_dimEdit;
};
Notes
If the pair of lines that set the text and color look a little strange to you, it is because any time I write my "get/set" function pairs, the set functions always return a reference to the object, itself. That allows this kind of function chaining. It's really no different than being able to write code like: a = a + a + a
. Over time, I have found it more convenient to be able to write code in this way.
One last note on WTL: As you look at the code in DimEdit.h and maindlg.h, you will see that the message map doesn't look quite normal. This is because I found that WTL has a message cracking system, very similar to the one that has been present in the windowsx.h header since the Windows 3.x days. Message crackers release you from the responsibility of casting the various parameters to a more useable form. The only problem with doing it this way is that the event handler wizard is based on the normal ATL message map, not the extended cracking version. You will have to add messages to the map and handler code by hand, but I do not find this to be much of a burden.
Conclusion
I hope you find this control useful! If you would like any additional functionality, leave a message in the forum at the bottom of this page, and I will take your thoughts into consideration.