How to change the background color of a CFiledialog (2)

294 Views Asked by At

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

1

There are 1 best solutions below

0
On

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.