这篇是好久之前自己最初贴在cdsn上的帖子,现在也挪到这里算是开篇吧
这是根据原代码例子改的中文版界面,主要是在OnInitDialog里面的代码我都写了注释,有兴趣大家一起研究一下
BOOL CPropGridDlg::OnInitDialog()
{
// CDialog::OnInitDialog();
CPropertyGridDlgBase::OnInitDialog();
// 将\“关于...\”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
//////////////////////////////////////////////////////////////////////////
// 获得图片框矩形
CRect rc;
m_wndPlaceHolder.GetWindowRect( &rc );
// 转为窗口坐标
ScreenToClient( &rc );
// 建立属性表
if ( m_wndPropertyGrid.Create( rc, this, IDC_PROPERTY_GRID ) )
{
m_wndPropertyGrid.SetVariableItemsHeight(TRUE);
// 获取逻辑字体
LOGFONT lf;
GetFont()->GetLogFont( &lf );
// create document settings category.
// 建立分类
CXTPPropertyGridItem* pSettings = m_wndPropertyGrid.AddCategory(_T("Document Settings"));
// 设置TOOLTIP
pSettings->SetTooltip(_T("Document Settings Category"));
// add child items to category.
// 建立bool内容
CXTPPropertyGridItem* pItemSaveOnClose = pSettings->AddChildItem(new CXTPPropertyGridItemBool(_T("SaveOnClose"), TRUE));
// 建立字体内容
pSettings->AddChildItem(new CXTPPropertyGridItemFont(_T("WindowFont"), lf));
// 建立size内容
pSettings->AddChildItem(new CXTPPropertyGridItemSize(_T("WindowSize"), CSize(100, 100)));
// 展开
pSettings->Expand();
// 选择
pItemSaveOnClose->Select();
// create global settings category.
// 建立分类
CXTPPropertyGridItem* pGlobals = m_wndPropertyGrid.AddCategory(_T("Global Settings"));
// add child items to category.
// 建立只读字符串内容
CXTPPropertyGridItem* pItemGreeting = pGlobals->AddChildItem(new CXTPPropertyGridItem(_T("Greeting Text"), _T("Welcome to your application!")));
pItemGreeting->SetReadOnly(TRUE);
// 建立整数内容
pGlobals->AddChildItem(new CXTPPropertyGridItemNumber(_T("ItemsInMRUList"), 4));
// 设置说明
CXTPPropertyGridItem* pItemRate = pGlobals->AddChildItem(new CXTPPropertyGridItemNumber(_T("MaxRepeatRate"), 10));
pItemRate->SetDescription(_T("The rate in milliseconds that the text will repeat."));
// 建立color内容
pGlobals->AddChildItem(new CXTPPropertyGridItemColor(_T("ToolbarColor"), RGB(255, 192,128)));
//////////////////////////////////////////////////////////////////////////
// Version category.
// 建立分类
CXTPPropertyGridItem* pVersion = m_wndPropertyGrid.AddCategory(_T("Version"));
// add child items to category.
// 建立只读字符串内容
CXTPPropertyGridItem* pItemVersion = pVersion->AddChildItem(new CXTPPropertyGridItem(_T("AppVersion"), _T("1.0")));
pItemVersion->SetReadOnly(TRUE);
// 使用资源建立字符串内容
CXTPPropertyGridItem* pItemLanguage = pVersion->AddChildItem(new CXTPPropertyGridItem(ID_ITEM_VERSION_LANGUAGE, _T("English (United States)")));
// 展开分类
pVersion->Expand();
// 将combo连接到字符串内容中
// 测试结果 只要不是只读的字符串内容就可连接combo 步骤如下
// 获取item的Constraints
CXTPPropertyGridItemConstraints* pList = pItemLanguage->GetConstraints();
// 添加combo内容
pList->AddConstraint(_T("Neutral"));
pList->AddConstraint(_T("Arabic"));
pList->AddConstraint(_T("German"));
pList->AddConstraint(_T("Chinese(Taiwan)"));
pList->AddConstraint(_T("English (United Kingdom)"));
pList->AddConstraint(_T("English (United States)"));
pList->AddConstraint(_T("France"));
pList->AddConstraint(_T("Russian"));
pList->AddConstraint(_T("简体中文"));
pList->AddConstraint(_T("英文"));
pList->AddConstraint(_T("日文"));
// 设置combo为可编辑组合框
pItemLanguage->SetFlags(xtpGridItemHasComboButton | xtpGridItemHasEdit);
//////////////////////////////////////////////////////////////////////////
// Dynamic Options
// 建立分类
CXTPPropertyGridItem* pCategoryDynamic = m_wndPropertyGrid.AddCategory(_T("Dynamic Options"));
// 建立bool内容
// 这是第2种方式 强制转换指针方式
CXTPPropertyGridItemBool* pItemBool = (CXTPPropertyGridItemBool*)pCategoryDynamic->AddChildItem(
new CXTPPropertyGridItemBool(_T("Advanced"), FALSE));
// 设置ID
pItemBool->SetID(501);
// 设置checkbox样式
pItemBool->SetCheckBoxStyle();
// 建立bool内容checkbox样式并隐藏
pItemBool = (CXTPPropertyGridItemBool*)pCategoryDynamic->AddChildItem(
new CXTPPropertyGridItemBool(_T("Option 1"), FALSE));
pItemBool->SetHidden(TRUE);
pItemBool->SetCheckBoxStyle();
// 建立bool内容checkbox样式并隐藏
pItemBool = (CXTPPropertyGridItemBool*)pCategoryDynamic->AddChildItem(
new CXTPPropertyGridItemBool(_T("Option 2"), FALSE));
pItemBool->SetHidden(TRUE);
pItemBool->SetCheckBoxStyle();
// 建立bool内容checkbox样式并隐藏
pItemBool = (CXTPPropertyGridItemBool*)pCategoryDynamic->AddChildItem(
new CXTPPropertyGridItemBool(_T("Option 3"), FALSE));
pItemBool->SetHidden(TRUE);
pItemBool->SetCheckBoxStyle();
// 建立bool内容checkbox样式并隐藏和只读
pItemBool = (CXTPPropertyGridItemBool*)pCategoryDynamic->AddChildItem(
new CXTPPropertyGridItemBool(_T("Option 4"), TRUE));
pItemBool->SetHidden(TRUE);
pItemBool->SetCheckBoxStyle();
pItemBool->SetReadOnly();
// create standard items category.
// 建立分类
CXTPPropertyGridItem* pStandard = m_wndPropertyGrid.AddCategory(_T("Standard Items"));
// 建立字符串内容
pStandard->AddChildItem(new CXTPPropertyGridItem(_T("String item")));
// 建立多行字符串下拉框 帮助文件中没有
pStandard->AddChildItem(new CXTPPropertyGridItemMultilineString(_T("Multiline String item"), _T("1\r\n2")));
// 建立整数内容
pStandard->AddChildItem(new CXTPPropertyGridItemNumber(_T("Integer item")));
// 建立double内容并设置初始值和数据格式
pStandard->AddChildItem(new CXTPPropertyGridItemDouble(_T("Double item"),0,"%0.3f"));
// 建立颜色bool字体
pStandard->AddChildItem(new CXTPPropertyGridItemColor(_T("Color item")));
pStandard->AddChildItem(new CXTPPropertyGridItemBool(_T("Bool item")));
pStandard->AddChildItem(new CXTPPropertyGridItemFont(_T("Font item"), lf));
// mfc时间类COleDateTime
COleDateTime dates(1981, 1, 26, 0, 0, 0 );
// 使用COleDateTime建立时间内容
pStandard->AddChildItem(new CXTPPropertyGridItemDate(_T("Date item"), dates));
// 建立size内容
pStandard->AddChildItem(new CXTPPropertyGridItemSize(_T("Size item")));
// 建立enum内容
CXTPPropertyGridItem* pItem = pStandard->AddChildItem(new CXTPPropertyGridItemEnum(_T("Enum item"), 1));
// 添加enum记录到enum内容呈combo样式
pItem->GetConstraints()->AddConstraint(_T("Windows 98"), 1);
pItem->GetConstraints()->AddConstraint(_T("Windows 2000"), 2);
pItem->GetConstraints()->AddConstraint(_T("Windows XP"), 3);
// 建立flag内容 第2个参数"1+2"为初始值 即"Windows 98"和"Windows 2000"为真
// 且flag的元素数值需为1,2,4,8,16,32...
pItem = pStandard->AddChildItem(new CXTPPropertyGridItemFlags(_T("Flag item"), 1 + 2));
pItem->GetConstraints()->AddConstraint(_T("All Windows"), 1 + 2 + 4);
pItem->GetConstraints()->AddConstraint(_T("Windows 98"), 1);
pItem->GetConstraints()->AddConstraint(_T("Windows 2000"), 2);
pItem->GetConstraints()->AddConstraint(_T("Windows XP"), 4);
//////////////////////////////////////////////////////////////////////////
// 建立分类
CXTPPropertyGridItem* pButtons = m_wndPropertyGrid.AddCategory(_T("Standard Buttons"));
// 建立bool内容
pItem = pButtons->AddChildItem(new CXTPPropertyGridItemBool(_T("Combo Button")));
// 设置为combo样式
pItem->SetFlags(xtpGridItemHasComboButton);
// 建立字符串内容
pItem = pButtons->AddChildItem(new CXTPPropertyGridItem(_T("Expand Button")));
// 设置为可编辑并带有扩展按钮样式
pItem->SetFlags(xtpGridItemHasEdit | xtpGridItemHasExpandButton);
// 建立字符串内容
pItem = pButtons->AddChildItem(new CXTPPropertyGridItem(_T("2 Buttons")));
// 设置ID
pItem->SetID(510);
// 设置为可编辑并带有扩展按钮样式和combo
pItem->SetFlags(xtpGridItemHasEdit | xtpGridItemHasComboButton | xtpGridItemHasExpandButton);
// 添加combo内容
pItem->GetConstraints()->AddConstraint(_T("Windows 2000"), 1);
pItem->GetConstraints()->AddConstraint(_T("Windows 98"), 2);
// 建立字符串内容
pItem = pButtons->AddChildItem(new CXTPPropertyGridItem(_T("Text Button")));
// 添加按钮到字符串内容行尾
CXTPPropertyGridInplaceButton* pButton = pItem->GetInplaceButtons()->AddButton(new CXTPPropertyGridInplaceButton(1));
// 设置按钮文本
pButton->SetCaption(_T("Find"));
// 设置按钮宽度
pButton->SetWidth(100);
// 建立字符串内容
pItem = pButtons->AddChildItem(new CXTPPropertyGridItem(_T("Image Button")));
// 添加按钮到字符串内容行尾
pButton = pItem->GetInplaceButtons()->AddButton(new CXTPPropertyGridInplaceButton(1));
// 设置按钮图标索引
pButton->SetIconIndex(100);
// UINT数组 估计是一个临时存储单元用于添加图标到按钮
// 上面的100和下面的100以及设置图标语句中的btnFilter是相联系的
UINT btnFilter[] = {100};
// 设置图标
m_wndPropertyGrid.GetImageManager()->SetIcons(IDB_BITMAP_FILTER, btnFilter, 1, 0);
// 设置ToolTip在图标上
pButton->SetTooltip(_T("Set Filter for item"));
// 建立整形内容
pItem = pButtons->AddChildItem(new CXTPPropertyGridItemNumber(_T("Spin And Slider"), 60));
// 默认0-100暂时没有找到设置范围的方法
// 添加水平滑块连接到整形内容
pItem->AddSliderControl();
// 添加上下按钮连接到整形内容
pItem->AddSpinButton();
//////////////////////////////////////////////////////////////////////////
// 建立分类
CXTPPropertyGridItem* pMetrics = m_wndPropertyGrid.AddCategory(_T("Custom Metrics"));
// 建立字符串内容
pItem = pMetrics->AddChildItem(new CXTPPropertyGridItem(_T("Value Colors"), _T("")));
// 设置文字颜色 可以采用RGB宏或DWORD
// 文字和背景颜色会呈现混合效果
pItem->GetValueMetrics()->m_clrFore = 0x00ff00;
// 设置背景颜色
pItem->GetValueMetrics()->m_clrBack = RGB(255, 0, 255);
// 建立字符串内容
pItem = pMetrics->AddChildItem(new CXTPPropertyGridItem(_T("Caption Colors"), _T("")));
// 设置文字颜色
pItem->GetCaptionMetrics()->m_clrFore = 0xFF0000;
// 设置背景颜色
pItem->GetCaptionMetrics()->m_clrBack = RGB(235, 235, 235);
// 建立enum内容
pItem = pMetrics->AddChildItem(new CXTPPropertyGridItemEnum(_T("Images"), 2));
// 内加enum记录并带有图片
pItem->GetConstraints()->AddConstraint(_T("Green"), 0, 0);
pItem->GetConstraints()->AddConstraint(_T("Red"), 1, 1);
pItem->GetConstraints()->AddConstraint(_T("Yellow"), 2, 2);
pItem->GetConstraints()->AddConstraint(_T("Blue"), 3, 3);
// 设置enum内容的内容图片
pItem->GetValueMetrics()->m_nImage = 2;
// 设置enum内容的标题图片
pItem->GetCaptionMetrics()->m_nImage = 4;
// 设置mask颜色
m_wndPropertyGrid.GetImageManager()->SetMaskColor(0xC0C0C0);
// 设置图标
m_wndPropertyGrid.GetImageManager()->SetIcons(IDB_BITMAP_CONSTRAINTS, 0, 5, CSize(20, 14));
// 建立字符串内容
pItem = pMetrics->AddChildItem(new CXTPPropertyGridItem(_T("Variable Height"), _T("Item")));
// 建立内容块高度
pItem->SetHeight(32);
// 设置为combo样式
pItem->SetFlags(xtpGridItemHasComboButton);
// 建立多行字符串内容
// 貌似在多行中无法真正的多行编辑 没有找到让文本换行即支持文本回车的方式
pItem = pMetrics->AddChildItem(new CXTPPropertyGridItem(_T("MultiLine"), _T("Codejock Software\r\n428 Corunna Avenue\r\nOwosso, Michigan 48867 USA")));
// 设置能见得文本行数
pItem->SetMultiLinesCount(3);
// create custom items category.
// 建立分类
// 以下为自定义类型 代码见CustomItems.h
CXTPPropertyGridItem* pCustom = m_wndPropertyGrid.AddCategory(_T("Custom Items"));
// add child items to category.
// 建立icon内容
CXTPPropertyGridItem* pItemIcon = pCustom->AddChildItem(new CCustomItemIcon(_T("Icon"), m_hIcon));
pItemIcon->SetDescription(_T("This sample shows how to override draw function"));
// 建立DockPadding内容
// DockPadding为4个数的组合
CXTPPropertyGridItem* pItemDock = pCustom->AddChildItem(new CCustomItemChilds(_T("DockPadding"), CRect(100, 20, 400, 50)));
pItemDock->SetDescription(_T("This sample shows how to add item with childs"));
// 建立颜色内容
pCustom->AddChildItem(new CCustomItemColor(_T("CustomCombolist"), RGB(0xFF, 0x80, 0x40)));
// 建立打开对话框内容
pCustom->AddChildItem(new CCustomItemFileBox(_T("File Box")));
// 建立字符串内容
CXTPPropertyGridItem* pItemMaskEdit = pCustom->AddChildItem(new CXTPPropertyGridItem(_T("Mask Edit"), _T("Phone No: (816) 220-0000")));
// 设置字符串MASK
pItemMaskEdit->SetMask(_T("Phone No: (000) 000-0000"), _T("Phone No: (___) ___-____"));
// 建立字符串内容
CXTPPropertyGridItem* pItemPassword = pCustom->AddChildItem(new CXTPPropertyGridItem(_T("Password"), _T("Text")));
// 设置字符串Password
pItemPassword->SetPasswordMask();
// 建立日期内容
COleDateTime date(1981, 1, 26, 0, 0, 0 );
pCustom->AddChildItem(new CXTPPropertyGridItemDate(_T("Date"), date));
// 建立大写字母内容
pCustom->AddChildItem(new CCustomItemUpperCase(_T("UpperCase")));
// 建立ip地址内容
pCustom->AddChildItem(new CCustomItemIPAddress(_T("IP Address")));
// 建立PopupMenu内容
pCustom->AddChildItem(new CCustomItemMenu(_T("Popup Menu")));
// 建立字符串内容
pCustom->AddChildItem(new CCustomItemEdit(_T("Output"), _T("Debug")));
// add multi level tree node.
// 建立树形内容
CXTPPropertyGridItem* pCategoryOne = pCustom->AddChildItem(new CXTPPropertyGridItemCategory(_T("First Sub Category")));
CXTPPropertyGridItem* pCategoryTwo = pCategoryOne->AddChildItem(new CXTPPropertyGridItemCategory(_T("Second Sub Category 1")));
pCategoryTwo->AddChildItem(new CXTPPropertyGridItem(_T("Third Level 1"), _T("")));
pCategoryTwo->AddChildItem(new CXTPPropertyGridItem(_T("Third Level 2"), _T("")));
CXTPPropertyGridItem* pCategoryTwo2 = pCategoryOne->AddChildItem(new CXTPPropertyGridItemCategory(_T("Second Sub Category 2")));
pCategoryTwo2->AddChildItem(new CXTPPropertyGridItem(_T("Third Level 1"), _T("")));
// 建立树形内容
CXTPPropertyGridItem* pItemOne = pCustom->AddChildItem(new CXTPPropertyGridItem(_T("First Level"), _T("")));
CXTPPropertyGridItem* pItemTwo = pItemOne->AddChildItem(new CXTPPropertyGridItem(_T("Second Level"), _T("")));
CXTPPropertyGridItem* pItemThird = pItemTwo->AddChildItem(new CXTPPropertyGridItem(_T("Third Level"), _T("")));
pItemThird->AddChildItem(new CXTPPropertyGridItem(_T("Fourth Level 1"), _T("")));
pItemThird->AddChildItem(new CXTPPropertyGridItem(_T("Fourth Level 2"), _T("")));
// create custom items category.
// 建立分类
pCustom = m_wndPropertyGrid.AddCategory(_T("Custom Butons"));
// 建立上下按钮内容
CXTPPropertyGridItem* pItemSpin = pCustom->AddChildItem(new CCustomItemSpin(_T("SpinButton")));
pItemSpin->SetDescription(_T("This sample shows how to add new button type"));
// 建立水平滑块内容
pCustom->AddChildItem(new CCustomItemSlider(_T("Slider")));
// 建立CheckBox内容
CCustomItemCheckBox* pItemCheckBox = (CCustomItemCheckBox*)pCustom->AddChildItem(new CCustomItemCheckBox(_T("Check Box")));
pItemCheckBox->SetValue(_T("agree with conditions"));
pItemCheckBox->SetBool(TRUE);
// 建立自定义按钮
pCustom->AddChildItem(new CCustomItemButton(_T("Left Origin"), FALSE, TRUE));
pCustom->AddChildItem(new CCustomItemButton(_T("Right Origin"), FALSE, TRUE));
pCustom->AddChildItem(new CCustomItemButton(_T("Pointer"), TRUE, TRUE));
pCustom->AddChildItem(new CCustomItemButton(_T("Gradient"), TRUE, FALSE));
}
m_groupAppearance.SubclassDlgItem(IDC_GBOX_APPEAR, this);
m_groupSort.SubclassDlgItem(IDC_GBOX_SORT, this);
m_groupColor.SubclassDlgItem(IDC_GBOX_COLOR, this);
// Set control resizing.
SetResize(IDC_PROPERTY_GRID, SZ_TOP_LEFT, SZ_BOTTOM_RIGHT);
//
SetResize(IDC_GBOX_APPEAR, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_CHK_TOOLBAR, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_CHK_HELP, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_CHK_VERBS, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_CHK_DOUBLE, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_CHK_TABITEMS, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_CHK_HIGHLIGHT, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_COMBO_THEME, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_GBOX_SORT, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_SORT_CATEGORIES, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_SORT_ALPHABETICAL, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_SORT_NOSORT, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_GBOX_COLOR, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_CHK_CUSTOMCOLORS, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_BUTTON_SWITCHSTATE, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_COMBO_BORDER, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_CHECK_SHOWBUTTONS, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
SetResize(IDC_CHK_RIGHTTOLEFT, SZ_TOP_RIGHT, SZ_TOP_RIGHT);
// Load window placement
AutoLoadPlacement(_T("PropertyGridSample"));
m_cmbTheme.AddString(_T("xtpGridThemeDefault"));
m_cmbTheme.AddString(_T("xtpGridThemeNativeWinXP"));
m_cmbTheme.AddString(_T("xtpGridThemeOffice2003"));
m_cmbTheme.AddString(_T("xtpGridThemeCool"));
m_cmbTheme.AddString(_T("xtpGridThemeSimple"));
m_cmbTheme.AddString(_T("xtpGridThemeDelphi"));
m_cmbTheme.AddString(_T("xtpGridThemeWhidbey"));
m_cmbTheme.AddString(_T("xtpGridThemeOfficeXP"));
m_cmbTheme.AddString(_T("xtpGridThemeOffice2007"));
m_cmbTheme.SetCurSel(0);
m_cmbBorder.AddString(_T("xtpGridBorderNone"));
m_cmbBorder.AddString(_T("xtpGridBorderFlat"));
m_cmbBorder.AddString(_T("xtpGridBorderStaticEdge"));
m_cmbBorder.AddString(_T("xtpGridBorderClientEdge"));
m_cmbBorder.SetCurSel(3);
return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
}