本篇介绍CMFCPropertyGrid Control的使用,例子来自于vs2010中单文档视图中的Properties窗口,经过自己的网上搜索整理所得,望能够给新手以参考学习
下面说明详细步骤
1、新建一个对话框程序并命名(本例命名为MyProperty);
2、打开资源视图下的Dialog,添加一个CMFCPropertyGird Control 控件,命名ID为IDC_MFCPROPERTYGRID1,并放置合适位置(如图)
3、我们需要对这个属性图表控件进行插入新项,首先需要关联一个变量,其中关联变量可以手动关联,也可以通过类视图来关联,当然这里为了方便使用了类视图类进行关联(不明白手动关联的可以留言和我联系)
4、接下来我们就可以在初始化对话框函数里正式添加代码了,代码如下
// TODO: Add extra initialization here
//************
m_properyGrid.EnableHeaderCtrl(FALSE); //不设置表头
//m_properyGrid.EnableHeaderCtrl(TRUE,_T("属性"),_T("取值"));//设置表头
m_properyGrid.EnableDescriptionArea(TRUE);
m_properyGrid.SetVSDotNetLook(TRUE);
m_properyGrid.MarkModifiedProperties(TRUE);
//************
m_properyGrid.SetAlphabeticMode(FALSE);
m_properyGrid.SetShowDragContext(TRUE);
//*******第一行*******
CMFCPropertyGridProperty *pGroup1 = new CMFCPropertyGridProperty(_T("Appearance"));
pGroup1->AddSubItem(new CMFCPropertyGridProperty(_T("3D Look"),(_variant_t) false,
_T("Specifies the window's 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 window's title bar")));
m_properyGrid.AddProperty(pGroup1);
//*******第二行********
CMFCPropertyGridProperty *pSize = new CMFCPropertyGridProperty(_T("Window Size"),0,TRUE);
pProp = new CMFCPropertyGridProperty(_T("Height"),(_variant_t)250l,_T("Specifies the window's height"));
pProp->EnableSpinControl(TRUE,50,300);
pSize->AddSubItem(pProp);
pProp = new CMFCPropertyGridProperty(_T("Width"),(_variant_t)150l,_T("Specifies the window's width"));
pProp->EnableSpinControl(TRUE,50,200);
pSize->AddSubItem(pProp);
m_properyGrid.AddProperty(pSize);
/*pGroup12->AddSubItem(new CMFCPropertyGridProperty(_T("子项目2"),_T("25.36"),_T("This is a description")));*/
//********第三行********
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_properyGrid.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 const TCHAR 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_properyGrid.AddProperty(pGroup3);
//*********第五行*********
CMFCPropertyGridProperty* pGroup4 = new CMFCPropertyGridProperty(_T("Hierarchy"));
CMFCPropertyGridProperty* pGroup5 = new CMFCPropertyGridProperty(_T("First sub-level"));
pGroup4->AddSubItem(pGroup5);
CMFCPropertyGridProperty* pGroup6 = new CMFCPropertyGridProperty(_T("Second sub_level"));
pGroup5->AddSubItem(pGroup6);
pGroup6->AddSubItem(new CMFCPropertyGridProperty(_T("Item1"),(_variant_t)_T("Value 1"),_T("This is a description")));
pGroup6->AddSubItem(new CMFCPropertyGridProperty(_T("Item2"),(_variant_t)_T("Value 2"),_T("This is a description")));
pGroup6->AddSubItem(new CMFCPropertyGridProperty(_T("Item3"),(_variant_t)_T("Value 3"),_T("This is a description")));
pGroup6->Expand(FALSE);//不需全部展开
m_properyGrid.AddProperty(pGroup4);
m_properyGrid.AdjustLayout();
//**********************
看来已经可以了,我们运行一下····如下图
怎么?应该是两列,现在怎么会是一列呢?
这是因为后面一列遮住了前面一列的原因,在窗口中缩放出向有拖动就可以看见第一列了,如此我们在上述代码后面添加如下代码来改变窗口的位置
CRect rt;
GetClientRect(&rt);
m_properyGrid.SetWindowPos(NULL, rt.left, rt.top, rt.Width(), rt.Height(),SWP_NOACTIVATE | SWP_NOZORDER);
再运行一下,和前面的最终示意图一样了!好我们完成了一个简单的例子。
这里还有很多功能就不在列举了,要知道这个控件的功能很强大,我们只是例举了基础的功能,就像小说里面写的“功夫是学到了,就看你怎么运用”,O了,闪人····