Click here to Skip to main content
15,868,141 members
Articles / Desktop Programming / WTL
Article

A WTL Dim Edit control

Rate me:
Please Sign up or sign in to vote.
4.20/5 (3 votes)
31 Aug 20013 min read 76.2K   1.2K   18   8
A WTL adaptation of James Twine's Dim Edit control.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
Mr. Howes has been programming since he was first introduced to the Apple II+ at the tender age of ten. After briefly dabbling with circuit design as a combined CS/EE major, he came to his senses and completed a pure CS degree at RIT

All these years later, the programming bug still bites him each day and causes him to go to work, where he writes software for a living. Now he is even learning Cocoa for the Mac!

In his copious amounts of "spare" time, Mr. Howes is learning the art of cabinet-making, flies radio-controlled helicopters and airplanes, and mountain cycles. Next summer he would like to learn how to kayak.

Linked In Page: http://www.linkedin.com/pub/3/54a/578

Comments and Discussions

 
GeneralThanks^_^ Pin
AnnaWang12-Sep-07 16:41
AnnaWang12-Sep-07 16:41 
GeneralRe: Thanks^_^ Pin
Paul A. Howes15-Sep-07 16:02
Paul A. Howes15-Sep-07 16:02 
Generala = a + a + a Pin
Uwe Keim1-Sep-01 4:58
sitebuilderUwe Keim1-Sep-01 4:58 
Your set-function that returns a reference is really a cool idea!

--
See me: www.magerquark.de
Want a job? www.zeta-software.de/jobs
GeneralRe: a = a + a + a Pin
Paul A. Howes1-Sep-01 5:07
Paul A. Howes1-Sep-01 5:07 
GeneralRe: a = a + a + a Pin
Tim Smith1-Sep-01 7:54
Tim Smith1-Sep-01 7:54 
GeneralRe: a = a + a + a Pin
Paul A. Howes1-Sep-01 8:25
Paul A. Howes1-Sep-01 8:25 
GeneralRe: a = a + a + a Pin
Jörgen Sigvardsson1-Sep-01 7:54
Jörgen Sigvardsson1-Sep-01 7:54 
GeneralRe: a = a + a + a Pin
Uwe Keim2-Sep-01 19:35
sitebuilderUwe Keim2-Sep-01 19:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.