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
Have you read this article How To Change the Background Color of a Common Dialog Q117778 - does not work where it says:
The linked conversation thread is dated 2009 which is more recent that your linked tutorial.