CMFCPropertyGridCtrl的使用方法

本文介绍如何在MFC应用程序中使用CMFCPropertyGridCtrl控件实现对光照属性的编辑,包括创建属性网格、设置属性及响应更改事件等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 因为我在子窗口中使用CMFCPropertyGridCtrl,属性中notify设为true,否则点击控件无响应

2.创建代码可以如下:

CRect r;
	m_mfcPropertyGridCtrl.GetHeaderCtrl().GetClientRect(&r);
	HDITEM a;
	a.cxy = r.Width()/3;
	a.mask = HDI_WIDTH;	
	m_mfcPropertyGridCtrl.GetHeaderCtrl().SetItem(0,&a);
	a.pszText = "属性";
	a.mask = HDI_TEXT;
	m_mfcPropertyGridCtrl.GetHeaderCtrl().SetItem(0,&a);//表头控件设置,一次设置一个属性

	CMFCPropertyGridProperty * group;
	CMFCPropertyGridProperty * pProp;
	group = new CMFCPropertyGridProperty(_T("位置")); 
	pProp = new CMFCPropertyGridProperty(_T("X"),(_variant_t)(GLfloat)0.0,_T(""));//一定要注意类型,一定要精确
	pProp->SetData(POS_X);//此处的值不能为0,重要!!!!
	group->AddSubItem(pProp); 
	pProp = new CMFCPropertyGridProperty(_T("Y"),(_variant_t)(GLfloat)0.0,_T(""));
	pProp->SetData(POS_Y);
	group->AddSubItem(pProp); 
	pProp = new CMFCPropertyGridProperty(_T("Z"),(_variant_t)(GLfloat)0.0,_T(""));
	pProp->SetData(POS_Z);
	group->AddSubItem(pProp);
	group->SetData(POS);
	m_mfcPropertyGridCtrl.AddProperty(group); 

	group = new CMFCPropertyGridProperty(_T("方向")); 
	pProp = new CMFCPropertyGridProperty(_T("X"),(_variant_t)(GLfloat)0.0,_T(""));
	pProp->SetData(DIR_X);
	group->AddSubItem(pProp); 
	pProp = new CMFCPropertyGridProperty(_T("Y"),(_variant_t)(GLfloat)0.0,_T(""));
	pProp->SetData(DIR_Y);
	group->AddSubItem(pProp); 
	pProp = new CMFCPropertyGridProperty(_T("Z"),(_variant_t)(GLfloat)0.0,_T(""));
	pProp->SetData(DIR_Z);
	group->AddSubItem(pProp); 	
	group->SetData(DIR);
	m_mfcPropertyGridCtrl.AddProperty(group);

	CMFCPropertyGridColorProperty* pColorProp;
	pColorProp = new  CMFCPropertyGridColorProperty(_T("散射光"), RGB(255, 255, 255));
	pColorProp->EnableOtherButton(_T("其他..."));
	pColorProp->EnableAutomaticButton(_T("默认"), ::GetSysColor(COLOR_3DFACE));
	pColorProp->SetData(DIFFUSE);
	m_mfcPropertyGridCtrl.AddProperty(pColorProp); 
	
	pColorProp = new CMFCPropertyGridColorProperty(_T("反射光"), RGB(255, 255, 255));
	pColorProp->EnableOtherButton(_T("其他..."));
	pColorProp->EnableAutomaticButton(_T("默认"), ::GetSysColor(COLOR_3DFACE));
	pColorProp->SetData(SPECULAR);
	m_mfcPropertyGridCtrl.AddProperty(pColorProp); 
	
	pColorProp = new CMFCPropertyGridColorProperty(_T("环境光"), RGB(0, 0, 0));
	pColorProp->EnableOtherButton(_T("其他..."));
	pColorProp->EnableAutomaticButton(_T("默认"), ::GetSysColor(COLOR_3DFACE));
	pColorProp->SetData(AMBIENT);
	m_mfcPropertyGridCtrl.AddProperty(pColorProp); 

	pColorProp = new CMFCPropertyGridColorProperty(_T("全局环境光"), RGB(1, 1, 1));
	pColorProp->EnableOtherButton(_T("其他..."));
	pColorProp->EnableAutomaticButton(_T("默认"), ::GetSysColor(COLOR_3DFACE));
	pColorProp->SetData(AMBIENTGLOBAL);
	m_mfcPropertyGridCtrl.AddProperty(pColorProp); 
	
	group = new CMFCPropertyGridProperty(_T("衰减")); 
	pProp = new CMFCPropertyGridProperty(_T("常数系数"),(_variant_t)(GLfloat)1,_T(""));
	pProp->SetData(CONSTANT_ATTENUATION);
	group->AddSubItem(pProp); 
	pProp = new CMFCPropertyGridProperty(_T("一次项系数"),(_variant_t)(GLfloat)0,_T(""));
	pProp->SetData(LINEAR_ATTENUATION);
	group->AddSubItem(pProp);
	pProp = new CMFCPropertyGridProperty(_T("二次项系数"),(_variant_t)(GLfloat)0,_T(""));
	pProp->SetData(QUADRATIC_ATTENUATION);
	group->AddSubItem(pProp); 
	m_mfcPropertyGridCtrl.AddProperty(group);

	group = new CMFCPropertyGridProperty(_T("聚光灯选项")); 
	pProp = new CMFCPropertyGridProperty(_T("聚光指数"),(_variant_t)(GLfloat)0.0,_T(""));
	pProp->SetData(EXPONENT);
	group->AddSubItem(pProp); 
	pProp = new CMFCPropertyGridProperty(_T("角度"),(_variant_t)(GLfloat)180,_T(""));
	pProp->SetData(CUTOFF);
	group->AddSubItem(pProp); 
	m_mfcPropertyGridCtrl.AddProperty(group);	
	
        pGroup = new CMFCPropertyGridProperty("VertexShader");
        	static const TCHAR szFilter[] = _T("Vertex Shader脚本文件(*.vert)|*.vert|所有文件(*.*)|*.*||");
        	pProp = new CMFCPropertyGridFileProperty(_T("路径"), TRUE, "", _T("vert"), 0, szFilter, _T("指定VertexShader脚本文件"));
        	pProp->SetData(VERTEXSHADER);
        	pGroup->AddSubItem(pProp);
        	m_mfcPropertyGridCtrl.AddProperty(pGroup);
        	
        	//我生成的SDI程序的APP类默认是从CWinApp继承,而且生成的时候是无法改变的。但是想要选择文件夹的功能能够生效,就必须从CWinAppEx继承,并且在适当的地方调用InitShellManager();
        // 	pProp = new CMFCPropertyGridFileProperty(_T("文件夹"), _T("c:\\"));
        // 	pGroup->AddSubItem(pProp);

3.定义更改消息

ON_REGISTERED_MESSAGE(AFX_WM_PROPERTY_CHANGED, OnPropertyChanged) //使用这一句必须使用新版MFC,或者包含#include <afxcontrolbars.h>
LRESULT MeshPropertyLightSet::OnPropertyChanged (WPARAM,LPARAM lParam)
{  
	CMFCPropertyGridProperty* pProp = (CMFCPropertyGridProperty*) lParam; 
	int DataID = (int) pProp->GetData ();    
	CString strName = pProp->GetName();  //被改变的参数名 
	COleVariant Value = pProp->GetValue(); //改变之后的值 
	COleVariant OldValue = pProp->GetOriginalValue();  //改变之前的值  
	  
	int lightid = GetSelectLight();
	if (lightid <0||lightid>=m_pModelView->m_glscene.m_Lights.size())
	{
		return 0;
	}
	GLLight & light = m_pModelView->m_glscene.m_Lights[lightid];
	SHORT ll;
	COLORREF color;
	switch(DataID)
	{
	case POS_X:
		light.Position[0] = Value.fltVal;
		break;
	case POS_Y:
		light.Position[1] = Value.fltVal;
		break;
	case POS_Z:
		light.Position[2] = Value.fltVal;
		break;
	case DIR_X:
		light.Direction[0] = Value.fltVal;
		break;
	case DIR_Y:
		light.Direction[1] = Value.fltVal;
		break;
	case DIR_Z:
		light.Direction[2] = Value.fltVal;
		break;
	case DIFFUSE:
		color = ((CMFCPropertyGridColorProperty*)pProp)->GetColor();
		light.Diffuse[0] = GetRValue(color)/255.0;
		light.Diffuse[1] = GetGValue(color)/255.0;
		light.Diffuse[2] = GetBValue(color)/255.0;
		break;
	case SPECULAR:
		color = ((CMFCPropertyGridColorProperty*)pProp)->GetColor();
		light.Specular[0] = GetRValue(color)/255.0;
		light.Specular[1] = GetGValue(color)/255.0;
		light.Specular[2] = GetBValue(color)/255.0;
		break;
	case AMBIENT:
		color = ((CMFCPropertyGridColorProperty*)pProp)->GetColor();
		light.Ambient[0] = GetRValue(color)/255.0;
		light.Ambient[1] = GetGValue(color)/255.0;
		light.Ambient[2] = GetBValue(color)/255.0;
		break;
	case AMBIENTGLOBAL:
		color = ((CMFCPropertyGridColorProperty*)pProp)->GetColor();
		light.AmbientGlobal[0] = GetRValue(color)/255.0;
		light.AmbientGlobal[1] = GetGValue(color)/255.0;
		light.AmbientGlobal[2] = GetBValue(color)/255.0;
		break;
	case CONSTANT_ATTENUATION:
		light.Constant_Attenuation = Value.fltVal;
		break;
	case LINEAR_ATTENUATION:
		light.Linear_Attenuation = Value.fltVal;
		break;
	case QUADRATIC_ATTENUATION:
		light.Quadratic_Attenuation = Value.fltVal;
		break;
	case EXPONENT:
		light.Exponent = Value.fltVal;
		break;
	case CUTOFF:
		light.Cutoff = Value.fltVal;
		break;

	default:
		break;
	}

	return 0;


}

4.获取数据

CMFCPropertyGridProperty* p;
	//p = m_mfcPropertyGridCtrl.GetProperty(0);//是第几个属性
	//p->GetSubItem(1);//第几个属性
	m_mfcPropertyGridCtrl.FindItemByData(POS);//根据数据调用
	m_mfcPropertyGridCtrl.FindItemByData(POS_X)->SetValue((_variant_t)light.Position[0]);
	m_mfcPropertyGridCtrl.FindItemByData(POS_Y)->SetValue((_variant_t)light.Position[1]);
	m_mfcPropertyGridCtrl.FindItemByData(POS_Z)->SetValue((_variant_t)light.Position[2]);
	 
	m_mfcPropertyGridCtrl.FindItemByData(DIR);
	m_mfcPropertyGridCtrl.FindItemByData(DIR_X)->SetValue((_variant_t)light.Direction[0]);
	m_mfcPropertyGridCtrl.FindItemByData(DIR_Y)->SetValue((_variant_t)light.Direction[1]);
	m_mfcPropertyGridCtrl.FindItemByData(DIR_Z)->SetValue((_variant_t)light.Direction[2]);

	COLORREF color;
	color = RGB(light.Diffuse[0]*255,light.Diffuse[1]*255,light.Diffuse[2]*255);
	((CMFCPropertyGridColorProperty*)m_mfcPropertyGridCtrl.FindItemByData(DIFFUSE))->SetColor(color);

	color = RGB(light.Specular[0]*255,light.Specular[1]*255,light.Specular[2]*255);
	((CMFCPropertyGridColorProperty*)m_mfcPropertyGridCtrl.FindItemByData(SPECULAR))->SetColor(color);
	
	color = RGB(light.Ambient[0]*255,light.Ambient[1]*255,light.Ambient[2]*255);
	((CMFCPropertyGridColorProperty*)m_mfcPropertyGridCtrl.FindItemByData(AMBIENT))->SetColor(color);

	color = RGB(light.AmbientGlobal[0]*255,light.AmbientGlobal[1]*255,light.AmbientGlobal[2]*255);
	((CMFCPropertyGridColorProperty*)m_mfcPropertyGridCtrl.FindItemByData(AMBIENTGLOBAL))->SetColor(color);

	m_mfcPropertyGridCtrl.FindItemByData(CONSTANT_ATTENUATION)->SetValue((_variant_t)light.Constant_Attenuation);
	m_mfcPropertyGridCtrl.FindItemByData(LINEAR_ATTENUATION)->SetValue((_variant_t)light.Linear_Attenuation);
	m_mfcPropertyGridCtrl.FindItemByData(QUADRATIC_ATTENUATION)->SetValue((_variant_t)light.Quadratic_Attenuation);

	m_mfcPropertyGridCtrl.FindItemByData(EXPONENT)->SetValue((_variant_t)light.Exponent);
	m_mfcPropertyGridCtrl.FindItemByData(CUTOFF)->SetValue((_variant_t)light.Cutoff);


转载于:https://my.oschina.net/syhgzz/blog/295444

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值