Figure 1   FILESPEC.H

 #include <string.h>
#include <stdio.h>
#include <errno.h>
#include <io.h>

#ifndef BOOL
typedef int BOOL;
#endif

//////////////////
// Class to to navigate files that match a spec with wildcards
//
class CFileSpec : public _finddata_t {
      long            m_hfile;            // handle from findfirst

public:
       CFileSpec() {
            m_hfile = -1;
      }

      ~CFileSpec() {
            if (m_hfile>=0)
                  _findclose(m_hfile);
      }

      BOOL First(const char* filespec) {
            if (m_hfile>=0)
                  _findclose(m_hfile);
            return (m_hfile=_findfirst((char*)filespec, this)) != -1;
      }

      BOOL Next() {
            return m_hfile>=0 && _findnext(m_hfile, this)==0;
      }
};

Figure 2   DIRLIST.CPP

 ////////////////////////////////////////////////////////////////
// DIRLIST Copyright 1996 Microsoft Systems Journal. 
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// DIRLIST illustrates how to fill a list box with long filenames.


#include "stdafx.h"
#include "resource.h"
#include "filespec.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

////////////////////////////////////////////////////////////////
// Dialog class
//
class CMyDialog : public CDialog {
      CListBox m_lb1;
      CListBox m_lb2;
public:
      CMyDialog();
      virtual BOOL OnInitDialog();
      DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
END_MESSAGE_MAP()

CMyDialog::CMyDialog() : CDialog(IDD_DIALOG1)
{
}

////////////////
// Initialize dialog: Subclass the edit controls
//
BOOL CMyDialog::OnInitDialog()
{
      BOOL bRet = CDialog::OnInitDialog();
      m_lb1.SubclassDlgItem(IDC_LIST1, this);
      m_lb2.SubclassDlgItem(IDC_LIST2, this);

      // Initialize list box #1 using DlgDirList
      char dir[128] = "*.*";
      DlgDirList(dir, IDC_LIST1, 0, DDL_READWRITE);

      // Initialize list box #2 using CFileSpec
      CFileSpec spec;
      for (BOOL more=spec.First("*.*"); more; more=spec.Next()) {
            if (spec.attrib & (_A_SUBDIR|_A_HIDDEN|_A_SYSTEM))
                  continue;
            m_lb2.AddString(spec.name);
      }
      return bRet;
}

////////////////////////////////////////////////////////////////
// Application class 

Figure 4   COLDLG1

DLGCOLOR.CPP

 ////////////////////////////////////////////////////////////////
// DLGCOLOR Copyright 1996 Microsoft Systems Journal. 
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// DLGCOLOR illustrates how to implement edit controls that change
// color when they receive focus.
// This implementation uses MFC message reflection.

#include "stdafx.h"
#include "resource.h"
#include "dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

////////////////
// Standard application class 
//
class CMyApp : public CWinApp {
public:
   CMyApp();
   virtual BOOL InitInstance();
   //{{AFX_MSG(CMyApp)
   //}}AFX_MSG
   DECLARE_MESSAGE_MAP()
};

CMyApp NEAR theApp;

BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
   //{{AFX_MSG_MAP(CMyApp)
   //}}AFX_MSG_MAP
END_MESSAGE_MAP()

CMyApp::CMyApp()
{
}

BOOL CMyApp::InitInstance()
{
   CMyDialog dlg;
   m_pMainWnd = &dlg;
   dlg.DoModal();
   return FALSE;
}

DITCLR.H

 #include "stdafx.h"

//////////////////
// Edit control that changes colors when it has focus.
//
class CColorEdit : public CEdit {
public:
   CColorEdit() { }
   afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
   afx_msg void OnSetFocus(CWnd* pOldWnd);
   afx_msg void OnKillFocus(CWnd* pNewWnd);
   DECLARE_MESSAGE_MAP()
};

EDITCLR.CPP

 #include "stdafx.h"
#include "editclr.h"

//////////////////
// You could make these user-customizable.
//
const COLORREF BK_COLOR = RGB(255,255,0); // yellow
const COLORREF FG_COLOR = RGB(255,0,0);   // red

BEGIN_MESSAGE_MAP(CColorEdit, CEdit)
   ON_WM_CTLCOLOR_REFLECT()
   ON_WM_SETFOCUS()  
   ON_WM_KILLFOCUS() 
END_MESSAGE_MAP()

/////////////////
// Handle WM_CTLCOLOR reflected from parent:
// If I have the focus, return the "highlight color."
// Must also set text and background colors, that's a Windows thing.
// 
HBRUSH CColorEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
   // If I have the focus, use fg/bg color; else do nothing
   //
   if (CWnd::GetFocus()==this) {
      // By making the brush static, only one is used for all
      // instances of CColorEdit. The compiler doesn't initialize it
      // until the first time CColorEdit::CtlColor is called
      //
      static CBrush s_brush(BK_COLOR);

      pDC->SetBkColor(BK_COLOR);    // required for edit controls
      pDC->SetTextColor(FG_COLOR);  // ditto
      return s_brush;               // return background brush
   } 
   return NULL;
}

//////////////////
// I got focus: force repaint
//
void CColorEdit::OnSetFocus(CWnd* pOldWnd)
{
   Invalidate();           // invalidate client area
   UpdateWindow();         // force repaint now
   CEdit::OnSetFocus(pOldWnd);
}

//////////////////
// I lost focus: force repaint
//
void CColorEdit::OnKillFocus(CWnd* pNewWnd)
{
   Invalidate();           // invalidate client area
   UpdateWindow();         // force repaint now
   CEdit::OnKillFocus(pNewWnd);
}

DLG.H

 #include "stdafx.h"
#include "resource.h"
#include "editclr.h"

////////////////////////////////////////////////////////////////
// Dialog class
//
class CMyDialog : public CDialog {
   CColorEdit  m_edit1;       // three edit controls
   CColorEdit  m_edit2;
   CColorEdit  m_edit3;
public:
   CMyDialog();
   virtual BOOL OnInitDialog();
   DECLARE_MESSAGE_MAP()
};

DLG.CPP

 #include "stdafx.h"
#include "resource.h"
#include "dlg.h"

BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
END_MESSAGE_MAP()

CMyDialog::CMyDialog() : CDialog(IDD_DIALOG1)
{
}

////////////////
// Initialize dialog: Subclass the edit controls
//
BOOL CMyDialog::OnInitDialog()
{
   BOOL bRet = CDialog::OnInitDialog();
   m_edit1.SubclassDlgItem(IDC_EDIT1, this);
   m_edit2.SubclassDlgItem(IDC_EDIT2, this);
   m_edit3.SubclassDlgItem(IDC_EDIT3, this);
   return bRet;
}

Figure 6   MFC Message Reflection Macros

 ON_WM_CTLCOLOR_REFLECT()
ON_WM_DRAWITEM_REFLECT()
ON_WM_MEASUREITEM_REFLECT()
ON_WM_DELETEITEM_REFLECT()
ON_WM_CHARTOITEM_REFLECT()
ON_WM_VKEYTOITEM_REFLECT()
ON_WM_COMPAREITEM_REFLECT()
ON_WM_HSCROLL_REFLECT()
ON_WM_VSCROLL_REFLECT()
ON_WM_PARENTNOTIFY_REFLECT()
ON_CONTROL_REFLECT(wNotifyCode, memberFxn)
ON_CONTROL_REFLECT_EX(wNotifyCode, memberFxn)
ON_NOTIFY_REFLECT(wNotifyCode, memberFxn)
ON_NOTIFY_REFLECT_EX(wNotifyCode, memberFxn) 
ON_UPDATE_COMMAND_UI_REFLECT(memberFxn)

Figure 7   COLDLG2

DLGCOLOR.CPP

 ////////////////////////////////////////////////////////////////
// DLGCOLOR Copyright 1996 Microsoft Systems Journal. 
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// DLGCOLOR illustrates how to implement edit controls that change
// color when they receive focus.
// This implementation uses the dialog, not the edit control, to implement
// the colors.

#include "stdafx.h"
#include "resource.h"
#include "dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

////////////////
// Standard application class 
//
class CMyApp : public CWinApp {
public:
   CMyApp();
   virtual BOOL InitInstance();
   //{{AFX_MSG(CMyApp)
   //}}AFX_MSG
   DECLARE_MESSAGE_MAP()
};

CMyApp NEAR theApp;

BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
   //{{AFX_MSG_MAP(CMyApp)
   //}}AFX_MSG_MAP
END_MESSAGE_MAP()

CMyApp::CMyApp()
{
}

BOOL CMyApp::InitInstance()
{
   CMyDialog dlg;
   m_pMainWnd = &dlg;
   dlg.DoModal();
   return FALSE;
}

DLG.H

 #include "stdafx.h"
#include "resource.h"

////////////////
// Dialog class
//
class CMyDialog : public CDialog {
   void  RepaintIfEdit(CWnd* pWnd);
public:
   CMyDialog();
   afx_msg void   OnEnSetFocus(UINT nID);
   afx_msg void   OnEnKillFocus(UINT nID);
   afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
   DECLARE_MESSAGE_MAP()
};

DLG.CPP

 #include "stdafx.h"
#include "resource.h"
#include "dlg.h"

BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
   ON_WM_CTLCOLOR()
   ON_CONTROL_RANGE(EN_SETFOCUS,  0, 0xFFFFFFFF, OnEnSetFocus)
   ON_CONTROL_RANGE(EN_KILLFOCUS, 0, 0xFFFFFFFF, OnEnKillFocus)
END_MESSAGE_MAP()

CMyDialog::CMyDialog() : CDialog(IDD_DIALOG1)
{
}

//////////////////
// Handle EN_SETFOCUS notification. Repaint the edit control.
//
void CMyDialog::OnEnSetFocus(UINT nID)
{
   RepaintIfEdit(GetDlgItem(nID));
}

//////////////////
// Handle EN_KILLFOCUS notification. Repaint the edit control.
// Note: This could be the same actual function as OnEnSetFocus,
// but I separated them for clarity.
//
void CMyDialog::OnEnKillFocus(UINT nID)
{
   RepaintIfEdit(GetDlgItem(nID));
}

/////////////////
// Helper function to repaint control if it's an edit control.
// Required because other kinds of controls could use the 
// same notification ID as EN_SET/KILLFOCUS.
//
void CMyDialog::RepaintIfEdit(CWnd* pWnd)
{
   char classname[80]="";
   ::GetClassName(pWnd->GetSafeHwnd(), classname, sizeof(classname));
   if (strcmp(classname,"Edit")==0) { // if edit control:
      pWnd->Invalidate();     // invalidate edit control
      pWnd->UpdateWindow();   // force repaint now
   }
}

const COLORREF BK_COLOR = RGB(255,255,0); // yellow
const COLORREF FG_COLOR = RGB(255,0,0);   // red

//////////////////
// Handle WM_CTLCOLOR: If edit control, return appropriate color
//
HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{

   if (nCtlColor==CTLCOLOR_EDIT && 
      // Be sure to use HWND to check for window identity
      pWnd->m_hWnd==CWnd::GetFocus()->GetSafeHwnd()) {

      // By making the brush static, only one is used for all
      // instances of CColorEdit. The compiler doesn't initialize it
      // until the first time CColorEdit::CtlColor is called.
      //
      // For a user-customizable color, you could make this an application
      // class member (app global) and set it when the user customizes
      // the color.
      //
      static CBrush s_brush(BK_COLOR);
      pDC->SetBkColor(BK_COLOR);    // required for edit controls
      pDC->SetTextColor(FG_COLOR);  // ditto
      return s_brush;               // return background brush
   }
   return NULL;
}