EDN Admin
Well-known member
Hello Fellows,<br/> <br/> Im trying to use a CMFCPropertyGridCtrl into a dialog application. I replaced the CDialog base class with CDialogEx and then took some source given as example with any MDI new generate project applications. Here are the interesting bits under<br/> <br/>
<pre>// CPropertiesDialogDlg.h
class CPropertiesDialogDlg : public CDialogEx
{
...
CFont m_fntPropList;
CMFCPropertyGridCtrl m_wndPropList;
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
public:
void InitPropertyGrid(void);
void SetPropListFont(void);
...
}
// CPropertiesDialogDlg.cpp
...
void CPropertiesDialogDlg:oDataExchange(CDataExchange* pDX)
{
CDialog:oDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_wndPropList);
}
BOOL CPropertiesDialogDlg::OnInitDialog()
{
...
InitPropertyGrid();
...
}
BOOL CPropertiesDialogDlg:reCreateWindow(CREATESTRUCT& cs)
{
CRect rectDummy;
rectDummy.SetRectEmpty();
if (!m_wndPropList.Create(WS_VISIBLE | WS_CHILD, rectDummy, this, 2))
{
TRACE0("Failed to create Properties Grid n");
return -1; // fail to create
}
return CDialog:reCreateWindow(cs);
}
void CPropertiesDialogDlg::InitPropertyGrid(void)
{
SetPropListFont();
m_wndPropList.EnableHeaderCtrl(FALSE);
m_wndPropList.EnableDescriptionArea();
m_wndPropList.SetVSDotNetLook();
m_wndPropList.MarkModifiedProperties();
CMFCPropertyGridProperty* pGroup1 = new CMFCPropertyGridProperty(_T("Appearance"));
pGroup1->AddSubItem(new CMFCPropertyGridProperty(_T("3D Look"), (_variant_t) false, _T("Specifies the windows font will be non-bold and controls will have a 3D border")));
CMFCPropertyGridProperty* pProp = new CMFCPropertyGridProperty(_T("Border"), _T("Dialog Frame"), _T("One of: None, Thin, Resizable, or Dialog Frame"));
pProp->AddOption(_T("None"));
pProp->AddOption(_T("Thin"));
pProp->AddOption(_T("Resizable"));
pProp->AddOption(_T("Dialog Frame"));
pProp->AllowEdit(FALSE);
pGroup1->AddSubItem(pProp);
pGroup1->AddSubItem(new CMFCPropertyGridProperty(_T("Caption"), (_variant_t) _T("About"), _T("Specifies the text that will be displayed in the windows title bar")));
m_wndPropList.AddProperty(pGroup1);
CMFCPropertyGridProperty* pSize = new CMFCPropertyGridProperty(_T("Window Size"), 0, TRUE);
pProp = new CMFCPropertyGridProperty(_T("Height"), (_variant_t) 250l, _T("Specifies the windows height"));
pProp->EnableSpinControl(TRUE, 0, 1000);
pSize->AddSubItem(pProp);
pProp = new CMFCPropertyGridProperty( _T("Width"), (_variant_t) 150l, _T("Specifies the windows width"));
pProp->EnableSpinControl();
pSize->AddSubItem(pProp);
m_wndPropList.AddProperty(pSize);
CMFCPropertyGridProperty* pGroup2 = new CMFCPropertyGridProperty(_T("Font"));
LOGFONT lf;
CFont* font = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
font->GetLogFont(&lf);
lstrcpy(lf.lfFaceName, _T("Arial"));
pGroup2->AddSubItem(new CMFCPropertyGridFontProperty(_T("Font"), lf, CF_EFFECTS | CF_SCREENFONTS, _T("Specifies the default font for the window")));
pGroup2->AddSubItem(new CMFCPropertyGridProperty(_T("Use System Font"), (_variant_t) true, _T("Specifies that the window uses MS Shell Dlg font")));
m_wndPropList.AddProperty(pGroup2);
CMFCPropertyGridProperty* pGroup3 = new CMFCPropertyGridProperty(_T("Misc"));
pProp = new CMFCPropertyGridProperty(_T("(Name)"), _T("Application"));
pProp->Enable(FALSE);
pGroup3->AddSubItem(pProp);
CMFCPropertyGridColorProperty* pColorProp = new CMFCPropertyGridColorProperty(_T("Window Color"), RGB(210, 192, 254), NULL, _T("Specifies the default window color"));
pColorProp->EnableOtherButton(_T("Other..."));
pColorProp->EnableAutomaticButton(_T("Default"), ::GetSysColor(COLOR_3DFACE));
pGroup3->AddSubItem(pColorProp);
static TCHAR BASED_CODE szFilter[] = _T("Icon Files(*.ico)|*.ico|All Files(*.*)|*.*||");
pGroup3->AddSubItem(new CMFCPropertyGridFileProperty(_T("Icon"), TRUE, _T(""), _T("ico"), 0, szFilter, _T("Specifies the window icon")));
pGroup3->AddSubItem(new CMFCPropertyGridFileProperty(_T("Folder"), _T("c:\")));
m_wndPropList.AddProperty(pGroup3);
CMFCPropertyGridProperty* pGroup4 = new CMFCPropertyGridProperty(_T("Hierarchy"));
CMFCPropertyGridProperty* pGroup41 = new CMFCPropertyGridProperty(_T("First sub-level"));
pGroup4->AddSubItem(pGroup41);
CMFCPropertyGridProperty* pGroup411 = new CMFCPropertyGridProperty(_T("Second sub-level"));
pGroup41->AddSubItem(pGroup411);
pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("Item 1"), (_variant_t) _T("Value 1"), _T("This is a description")));
pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("Item 2"), (_variant_t) _T("Value 2"), _T("This is a description")));
pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("Item 3"), (_variant_t) _T("Value 3"), _T("This is a description")));
pGroup4->Expand(FALSE);
m_wndPropList.AddProperty(pGroup4);
}
void CPropertiesDialogDlg::SetPropListFont()
{
:eleteObject(m_fntPropList.Detach());
LOGFONT lf;
afxGlobalData.fontRegular.GetLogFont(&lf);
NONCLIENTMETRICS info;
info.cbSize = sizeof(info);
afxGlobalData.GetNonClientMetrics(info);
lf.lfHeight = info.lfMenuFont.lfHeight;
lf.lfWeight = info.lfMenuFont.lfWeight;
lf.lfItalic = info.lfMenuFont.lfItalic;
m_fntPropList.CreateFontIndirect(&lf);
m_wndPropList.SetFont(&m_fntPropList);
}
...[/code]
<br/> Has anybody been successful with this?<br/> <br/> The list of undesired symptoms are:<br/> - Assertion errors switching from one Property object to another (~every second object you click into)<br/> - Missing icons on the spin buttons<br/> - Property column invisible, you must click on the left side of the grid ctrl to be able to re-size it.<br/> <br/> Thanks in advance!<br/> <br/> -Damien
View the full article
<pre>// CPropertiesDialogDlg.h
class CPropertiesDialogDlg : public CDialogEx
{
...
CFont m_fntPropList;
CMFCPropertyGridCtrl m_wndPropList;
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
public:
void InitPropertyGrid(void);
void SetPropListFont(void);
...
}
// CPropertiesDialogDlg.cpp
...
void CPropertiesDialogDlg:oDataExchange(CDataExchange* pDX)
{
CDialog:oDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_wndPropList);
}
BOOL CPropertiesDialogDlg::OnInitDialog()
{
...
InitPropertyGrid();
...
}
BOOL CPropertiesDialogDlg:reCreateWindow(CREATESTRUCT& cs)
{
CRect rectDummy;
rectDummy.SetRectEmpty();
if (!m_wndPropList.Create(WS_VISIBLE | WS_CHILD, rectDummy, this, 2))
{
TRACE0("Failed to create Properties Grid n");
return -1; // fail to create
}
return CDialog:reCreateWindow(cs);
}
void CPropertiesDialogDlg::InitPropertyGrid(void)
{
SetPropListFont();
m_wndPropList.EnableHeaderCtrl(FALSE);
m_wndPropList.EnableDescriptionArea();
m_wndPropList.SetVSDotNetLook();
m_wndPropList.MarkModifiedProperties();
CMFCPropertyGridProperty* pGroup1 = new CMFCPropertyGridProperty(_T("Appearance"));
pGroup1->AddSubItem(new CMFCPropertyGridProperty(_T("3D Look"), (_variant_t) false, _T("Specifies the windows font will be non-bold and controls will have a 3D border")));
CMFCPropertyGridProperty* pProp = new CMFCPropertyGridProperty(_T("Border"), _T("Dialog Frame"), _T("One of: None, Thin, Resizable, or Dialog Frame"));
pProp->AddOption(_T("None"));
pProp->AddOption(_T("Thin"));
pProp->AddOption(_T("Resizable"));
pProp->AddOption(_T("Dialog Frame"));
pProp->AllowEdit(FALSE);
pGroup1->AddSubItem(pProp);
pGroup1->AddSubItem(new CMFCPropertyGridProperty(_T("Caption"), (_variant_t) _T("About"), _T("Specifies the text that will be displayed in the windows title bar")));
m_wndPropList.AddProperty(pGroup1);
CMFCPropertyGridProperty* pSize = new CMFCPropertyGridProperty(_T("Window Size"), 0, TRUE);
pProp = new CMFCPropertyGridProperty(_T("Height"), (_variant_t) 250l, _T("Specifies the windows height"));
pProp->EnableSpinControl(TRUE, 0, 1000);
pSize->AddSubItem(pProp);
pProp = new CMFCPropertyGridProperty( _T("Width"), (_variant_t) 150l, _T("Specifies the windows width"));
pProp->EnableSpinControl();
pSize->AddSubItem(pProp);
m_wndPropList.AddProperty(pSize);
CMFCPropertyGridProperty* pGroup2 = new CMFCPropertyGridProperty(_T("Font"));
LOGFONT lf;
CFont* font = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
font->GetLogFont(&lf);
lstrcpy(lf.lfFaceName, _T("Arial"));
pGroup2->AddSubItem(new CMFCPropertyGridFontProperty(_T("Font"), lf, CF_EFFECTS | CF_SCREENFONTS, _T("Specifies the default font for the window")));
pGroup2->AddSubItem(new CMFCPropertyGridProperty(_T("Use System Font"), (_variant_t) true, _T("Specifies that the window uses MS Shell Dlg font")));
m_wndPropList.AddProperty(pGroup2);
CMFCPropertyGridProperty* pGroup3 = new CMFCPropertyGridProperty(_T("Misc"));
pProp = new CMFCPropertyGridProperty(_T("(Name)"), _T("Application"));
pProp->Enable(FALSE);
pGroup3->AddSubItem(pProp);
CMFCPropertyGridColorProperty* pColorProp = new CMFCPropertyGridColorProperty(_T("Window Color"), RGB(210, 192, 254), NULL, _T("Specifies the default window color"));
pColorProp->EnableOtherButton(_T("Other..."));
pColorProp->EnableAutomaticButton(_T("Default"), ::GetSysColor(COLOR_3DFACE));
pGroup3->AddSubItem(pColorProp);
static TCHAR BASED_CODE szFilter[] = _T("Icon Files(*.ico)|*.ico|All Files(*.*)|*.*||");
pGroup3->AddSubItem(new CMFCPropertyGridFileProperty(_T("Icon"), TRUE, _T(""), _T("ico"), 0, szFilter, _T("Specifies the window icon")));
pGroup3->AddSubItem(new CMFCPropertyGridFileProperty(_T("Folder"), _T("c:\")));
m_wndPropList.AddProperty(pGroup3);
CMFCPropertyGridProperty* pGroup4 = new CMFCPropertyGridProperty(_T("Hierarchy"));
CMFCPropertyGridProperty* pGroup41 = new CMFCPropertyGridProperty(_T("First sub-level"));
pGroup4->AddSubItem(pGroup41);
CMFCPropertyGridProperty* pGroup411 = new CMFCPropertyGridProperty(_T("Second sub-level"));
pGroup41->AddSubItem(pGroup411);
pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("Item 1"), (_variant_t) _T("Value 1"), _T("This is a description")));
pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("Item 2"), (_variant_t) _T("Value 2"), _T("This is a description")));
pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("Item 3"), (_variant_t) _T("Value 3"), _T("This is a description")));
pGroup4->Expand(FALSE);
m_wndPropList.AddProperty(pGroup4);
}
void CPropertiesDialogDlg::SetPropListFont()
{
:eleteObject(m_fntPropList.Detach());
LOGFONT lf;
afxGlobalData.fontRegular.GetLogFont(&lf);
NONCLIENTMETRICS info;
info.cbSize = sizeof(info);
afxGlobalData.GetNonClientMetrics(info);
lf.lfHeight = info.lfMenuFont.lfHeight;
lf.lfWeight = info.lfMenuFont.lfWeight;
lf.lfItalic = info.lfMenuFont.lfItalic;
m_fntPropList.CreateFontIndirect(&lf);
m_wndPropList.SetFont(&m_fntPropList);
}
...[/code]
<br/> Has anybody been successful with this?<br/> <br/> The list of undesired symptoms are:<br/> - Assertion errors switching from one Property object to another (~every second object you click into)<br/> - Missing icons on the spin buttons<br/> - Property column invisible, you must click on the left side of the grid ctrl to be able to re-size it.<br/> <br/> Thanks in advance!<br/> <br/> -Damien
View the full article