1

Tools : Visual Studio 2019, MFC, cpp

I'm looking for how to change the background color for the CFileDialog dialog box. I found this link ==> Q115087: HOWTO: Change the Background Color of a Common Dialog.

I have extracted this code and insert it all into my project then two files mydlg.h and mydlg.cpp. I replace the CFileDialog object with mydlg.

This the code included:

Header file ==> mydlg.h

// 
#include <dlgs.h>
#define BACKGROUNG_COLOR RGB(0, 0, 255)
 ////////////////////////////////////////////////////////////////////// 
// CMyDlg dialog
class CMyDlg : public CFileDialog
{
// Construction
public:
CMyDlg(CWnd* pParent = NULL);   // standard constructor
// Add a CBrush pointer to store the new background brush
CBrush m_pBkBrush;
// Dialog Data
//{{AFX_DATA(CMyDlg)
enum { IDD = FILEOPENORD };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// Implementation
protected:
virtual void DoDataExchange(CDataExchange* pDX);  // DDX/DDV support
// Generated message map functions
//{{AFX_MSG(CMyDlg)
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

Code file CMyDlg.cpp

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/): CFileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY)
{
//{{AFX_DATA_INIT(CMyDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CFileDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

////////////////////////////////////////////////////////////////////// 
// CMyDlg message handlers

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
m_pBkBrush.CreateSolidBrush(BACKGROUNG_COLOR);
switch (nCtlColor) {   // ==>  breakpoint here

case CTLCOLOR_STATIC:
{
// Set the static text to white on blue.
pDC->SetBkColor(BACKGROUNG_COLOR); return (m_pBkBrush);
}
case CTLCOLOR_DLG: return (m_pBkBrush);
default: return CFileDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}

How i call it

CMyDlg FileOpenDialog(TRUE,NULL,local_File,OFN_FILEMUSTEXIST |
OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,
OpenFilter,                     // filter
AfxGetMainWnd());               // the parent window 

CString local_string= Current_Dir();
FileOpenDialog.m_ofn.lpstrInitialDir = local_string;
    
status = Mess.LoadString(IDS_STRING191);
FileOpenDialog.m_ofn.lpstrTitle = Mess;

if (FileOpenDialog.DoModal() == IDOK)
{
pszSource = FileOpenDialog.m_ofn.lpstrFile;
return true;
}
return false;

Compilation OK

Observation the background color does not change When i put a stop point on the switch in the function OnCtlColor we do not pass there.

Have you an idea, can you help me? Thank you

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
G3D
  • 19
  • 1
  • Consider closing [your other version](https://stackoverflow.com/questions/68048651/how-to-change-the-background-color-of-a-cfiledialog) of this question, if you have abandoned it. – Drew Dormann Jun 21 '21 at 16:35
  • 2
    Please take some time to properly format your question. There's a *"Markdown Editing Help"* button in the top right corner of the question editor. – IInspectable Jun 21 '21 at 21:54
  • 1
    The reference you posted has old dates on it (1986-2002) Maybe that method works for old versions of `CFileDialog` (before Windows Vista) I doubt it works for the new file dialog because you only have limited indirect access to some of its procedures. So you have to write your own dialog from scratch. Or there might be a way to force MFC to use the old dialog (it's not pretty). Note, nobody will ever complain if you use the standard file dialog. – Barmak Shemirani Jun 22 '21 at 00:34

1 Answers1

1

Have you read this article How To Change the Background Color of a Common Dialog Q117778 - does not work where it says:

Changing the background colour of a standard File dialog seems to be possible but requires more steps than in case of a simple dialog.

If it is absolutely crucial to change the colour and no simpler solutions, then consider this summary:

  • Derive a class from CFileDialog.
  • In the constructor, get the value of m_ofn.lpfnHook and store to a variable. Write to m_ofn.lpfnHook an address of a new hook procedure. The new hook procedure will call the old one.
  • In the new hook procedure, intercept the WM_INITDIALOG message and do this: get the parent HWND and get the GWL_WNDPROC value (the old window procedure) of the parent and store it in a variable. Replace this procedure with a new window procedure. The new window procedure will call the old one.
  • In the new window procedure, intercept the WM_CTLCOLORDLG message and proceed as explained in documentation and above posts.

The linked conversation thread is dated 2009 which is more recent that your linked tutorial.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164