一、CMFCPropertyGridCtrl的消息处理
头文件中定义
afx_msg LRESULT OnPropertyChanged(WPARAM wParam, LPARAM lParam);
在cpp中定义消息映射
ON_REGISTERED_MESSAGE(AFX_WM_PROPERTY_CHANGED, OnPropertyChanged)
在cpp中定义函数主体
lParam是Property属性项, wParam是ctrl的id
LRESULT CUartConfigPropWnd::OnPropertyChanged(WPARAM wParam, LPARAM lParam)
{
CMFCPropertyGridProperty* pItem = (CMFCPropertyGridProperty*)lParam;
return 0;
}
设置字体
// 在头文件中定义变量
CFont m_fntPropList;
// 在cpp文件中设置
::DeleteObject(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_ctrlPropGridTHInfo.SetFont(&m_fntPropList);
常用设置
EnableHeaderCtrl(FALSE);
EnableDescriptionArea(TRUE);
SetVSDotNetLook();
MarkModifiedProperties(FALSE);
设置属性控件的颜色
m_wndPropList.SetCustomColors(
RGB(50, 50, 50),// COLORREF clrBackground,
RGB(230, 230, 230),// COLORREF clrText,
RGB(40, 40, 40),// COLORREF clrGroupBackground,
RGB(200, 200, 200),// COLORREF clrGroupText,
RGB(50, 50, 50),// COLORREF clrDescriptionBackground,
RGB(200, 200, 200),// COLORREF clrDescriptionText,
RGB(70, 70, 70) // COLORREF clrLine
);
如何设置属性页第一列的宽度
// 方法一
HDITEM item;
item.cxy=120;
item.mask=HDI_WIDTH;
m_propertyGrid.GetHeaderCtrl().SetItem(0, &item);
// 方法二
CRect rect;
m_wndPropList.GetClientRect(&rect);
m_wndPropList.SendMessage(WM_SIZE, (WPARAM)SIZE_RESTORED, MAKELPARAM(rect.Width(), rect.Height())); // 中对齐
设置行高
// 无法直接设置行高,可以通过设置字体来调节行高
m_font.CreatePointFont(160, _T("宋体"));
m_wndPropList.SetFont(&m_font);
二、CMFCPropertyGridProperty的下拉列表的当前选项的读取与设置
先在头文件中定义
int GetComboSel(CMFCPropertyGridProperty* pProp, CString& strText);
int GetComboSel(CMFCPropertyGridProperty* pProp);
void SetComboSel(CMFCPropertyGridProperty* pProp, int nSel);
在cpp中实现
int CPropertiesWnd::GetComboSel(CMFCPropertyGridProperty* pProp, CString& strText)
{
int nSel = -1;
int nCount = pProp->GetOptionCount();
if (nCount > 0){
CString strItemText;
CString strPropValue = CString(pProp->GetValue());
for (int i = 0; i < nCount; i++){
strItemText = CString(pProp->GetOption(i));
if (strItemText == strPropValue){
nSel = i;
strText = strItemText;
break;
}
}
}
return nSel;
}
int CPropertiesWnd::GetComboSel(CMFCPropertyGridProperty* pProp)
{
int nSel = -1;
int nCount = pProp->GetOptionCount();
if (nCount > 0){
CString strItemText;
CString strPropValue = CString(pProp->GetValue());
for (int i = 0; i < nCount; i++){
strItemText = CString(pProp->GetOption(i));
if (strItemText == strPropValue){
nSel = i;
break;
}
}
}
return nSel;
}
void CPropertiesWnd::SetComboSel(CMFCPropertyGridProperty* pProp, int nSel)
{
if (nSel >= 0){
int nCount = pProp->GetOptionCount();
if (nCount > nSel){
pProp->SetValue(pProp->GetOption(nSel));
}
}
}
创建属性表项:
CMFCPropertyGridFileProperty* pFileProp = new CMFCPropertyGridFileProperty(_T("文件"), TRUE, _T(""), _T("bin"), 0, _T("二进制文件(*.bin)|*.bin|所有文件(*.*)|*.*||"), _T("我的bin文件"));
设置参数值
//整型数据创建方式:
CMFCPropertyGridProperty pProp = new CMFCPropertyGridProperty(_T("名称"), (_variant_t)0u, _T(""));
//整型数据设置方式:
int nValue = 100;
pProp->SetValue((_variant_t)nValue);