jambi

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

对CMFCPropertyGridProperty SetValue时容易报错,这种情况一般是Property和value的类型不匹配造成的。

在创建property的时候,指定了数据类型,如果setvalue时的数据类型与创建时的不一致便会报错。

CMFCPropertyGridProperty的数据类型为_variant_t,它是一个包含了各种数据类型的集合,MSDN中的描述如下:_variant_t对象封装了VARIANT数据类型。 该类管理资源分配和释放,并根据需要对VariantInit和VariantClear进行函数调用。MSDN链接

所以我们在创建property的时候一定要用_variant_t构造函数确定property的数据类型。构造函数的MSDN链接

 

Remarks

 

 

 

  • _variant_t( )   Constructs an empty _variant_t object, VT_EMPTY.

  • _variant_t( VARIANT& varSrc )   Constructs a _variant_t object from a copy of the VARIANT object. The variant type is retained.

  • _variant_t( VARIANT* pVarSrc )   Constructs a _variant_t object from a copy of the VARIANT object. The variant type is retained.

  • _variant_t( _variant_t& var_t_Src )   Constructs a _variant_t object from another _variant_t object. The variant type is retained.

  • _variant_t( VARIANT& varSrc, bool fCopy )   Constructs a _variant_t object from an existing VARIANT object. If fCopy is false, theVARIANT object is attached to the new object without making a copy.

  • _variant_t( short sSrc, VARTYPE vtSrc = VT_I2 )   Constructs a _variant_t object of type VT_I2 or VT_BOOL from a short integer value. Any other VARTYPE results in an E_INVALIDARG error.

  • _variant_t( long lSrc, VARTYPE vtSrc = VT_I4 )   Constructs a _variant_t object of type VT_I4VT_BOOL, or VT_ERROR from a long integer value. Any other VARTYPE results in an E_INVALIDARG error.

  • _variant_t( float fltSrc )   Constructs a _variant_t object of type VT_R4 from a float numerical value.

  • _variant_t( double dblSrc, VARTYPE vtSrc = VT_R8 )   Constructs a _variant_t object of type VT_R8 or VT_DATE from a double numerical value. Any other VARTYPE results in an E_INVALIDARG error.

  • _variant_t( CY& cySrc )   Constructs a _variant_t object of type VT_CY from a CY object.

  • _variant_t( _bstr_t& bstrSrc )   Constructs a _variant_t object of type VT_BSTR from a _bstr_t object. A new BSTR is allocated.

  • _variant_t( wchar_t *wstrSrc )   Constructs a _variant_t object of type VT_BSTR from a Unicode string. A new BSTR is allocated.

  • _variant_t( char* strSrc )   Constructs a _variant_t object of type VT_BSTR from a string. A new BSTR is allocated.

  • _variant_t( bool bSrc )   Constructs a _variant_t object of type VT_BOOL from a bool value.

  • _variant_t( IUnknown* pIUknownSrc, bool fAddRef = true )   Constructs a _variant_t object of type VT_UNKNOWN from a COM interface pointer. If fAddRef is true, then AddRef is called on the supplied interface pointer to match the call to Release that will occur when the_variant_t object is destroyed. It is up to you to call Release on the supplied interface pointer. If fAddRef is false, this constructor takes ownership of the supplied interface pointer; do not call Release on the supplied interface pointer.

  • _variant_t( IDispatch* pDispSrc, bool fAddRef = true )   Constructs a _variant_t object of type VT_DISPATCH from a COM interface pointer. If fAddRef is true, then AddRef is called on the supplied interface pointer to match the call to Release that will occur when the_variant_t object is destroyed. It is up to you to call Release on the supplied interface pointer. If fAddRef is false, this constructor takes ownership of the supplied interface pointer; do not call Release on the supplied interface pointer.

  • _variant_t( DECIMAL& decSrc )   Constructs a _variant_t object of type VT_DECIMAL from a DECIMAL value.

  • _variant_t( BYTE bSrc )   Constructs a _variant_t object of type VT_UI1 from a BYTE value.

 

下面是一个标准用法,创建时指定property类型为short,设置数值时指定property类型为short.

// 创建property
CMFCPropertyGridProperty* pProp = new CMFCPropertyGridProperty(_T("透明度"), _variant_t((short)100, VT_I2), _T("填充透明度0-100"));
    pProp->EnableSpinControl(TRUE, 0, 100);
    pGroup3->AddSubItem(pProp);

//设置property数值
sProp->SetValue(_variant_t((short)(sp->mFillTransparency * 100), VT_I2));

 

posted on   jambi  阅读(2035)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
 
点击右上角即可分享
微信分享提示