大家好,又见面了,我是你们的朋友全栈君。
获取MFC属性窗口CMFCPropertyGridProperty中某个item的值时,如果不小心写错了类型,就会导致获取的结果不正确,原因就是COleVariant其实继承自一个特殊的结构体tagVARIANT。
- COleVariant类声明
class COleVariant : public tagVARIANT
{
// Constructors
public:
COleVariant();
 ...
COleVariant(BYTE nSrc);
COleVariant(short nSrc, VARTYPE vtSrc = VT_I2);
COleVariant(long lSrc, VARTYPE vtSrc = VT_I4);
COleVariant(const COleCurrency& curSrc);
COleVariant(LONGLONG nSrc);
COleVariant(ULONGLONG nSrc);
COleVariant(float fltSrc);
COleVariant(double dblSrc);
COleVariant(const COleDateTime& timeSrc);
COleVariant(const CByteArray& arrSrc);
COleVariant(const CLongBinary& lbSrc);
COleVariant(LPCITEMIDLIST pidl);
public:
const COleVariant& operator=(BYTE nSrc);
const COleVariant& operator=(short nSrc);
const COleVariant& operator=(long lSrc);
const COleVariant& operator=(const COleCurrency& curSrc);
const COleVariant& operator=(LONGLONG nSrc);
const COleVariant& operator=(ULONGLONG nSrc);
const COleVariant& operator=(float fltSrc);
const COleVariant& operator=(double dblSrc);
const COleVariant& operator=(const COleDateTime& dateSrc);
// Implementation
public:
~COleVariant();
};
- tagVARIANT定义
struct tagVARIANT
{
union
{
struct __tagVARIANT
{
VARTYPE vt;
WORD wReserved1;
WORD wReserved2;
WORD wReserved3;
union
{
LONGLONG llVal;
LONG lVal;
BYTE bVal;
SHORT iVal;
FLOAT fltVal;
DOUBLE dblVal;
VARIANT_BOOL boolVal;
_VARIANT_BOOL bool;
SCODE scode;
CY cyVal;
DATE date;
BSTR bstrVal;
IUnknown *punkVal;
IDispatch *pdispVal;
SAFEARRAY *parray;
BYTE *pbVal;
SHORT *piVal;
LONG *plVal;
LONGLONG *pllVal;
FLOAT *pfltVal;
DOUBLE *pdblVal;
VARIANT_BOOL *pboolVal;
_VARIANT_BOOL *pbool;
SCODE *pscode;
CY *pcyVal;
DATE *pdate;
BSTR *pbstrVal;
IUnknown **ppunkVal;
IDispatch **ppdispVal;
SAFEARRAY **pparray;
VARIANT *pvarVal;
PVOID byref;
CHAR cVal;
USHORT uiVal;
ULONG ulVal;
ULONGLONG ullVal;
INT intVal;
UINT uintVal;
DECIMAL *pdecVal;
CHAR *pcVal;
USHORT *puiVal;
ULONG *pulVal;
ULONGLONG *pullVal;
INT *pintVal;
UINT *puintVal;
struct __tagBRECORD
{
PVOID pvRecord;
IRecordInfo *pRecInfo;
} __VARIANT_NAME_4;
} __VARIANT_NAME_3;
} __VARIANT_NAME_2;
DECIMAL decVal;
} __VARIANT_NAME_1;
} ;
可以看到tagVARIANT内部是一个union,union的大小是里面成员最大的一个的大小,union是共享内存的,在某个时刻只能有一个值是有效的。
因此在用COleVariant获取属性值时,最初的属性值设置为什么类型就要用tagVARIANT对应的成员去获取,如果short用double去获取,获取出来的结果是错误的。
// float类型
CMFCPropertyGridProperty* pPropItem1;
pPropItem1 = new CMFCPropertyGridProperty(_T("float类型"), (_variant_t)(float)12.3, _T(""));
COleVariant val1 = pPropItem1->GetValue();
float fValue = val1.fltVal;
// double类型
CMFCPropertyGridProperty* pPropItem2;
pPropItem2 = new CMFCPropertyGridProperty(_T("double类型"), (_variant_t)(double)212.3, _T(""));
COleVariant val2 = pPropItem2->GetValue();
double dValue = val2.dblVal;
// long类型
CMFCPropertyGridProperty* pPropItem3;
pPropItem3 = new CMFCPropertyGridProperty(_T("long类型"), (_variant_t)(long)100, _T(""));
COleVariant val3 = pPropItem3->GetValue();
long lValue = val3.lVal;
- union
union MyUnion
{
int n;
float f;
double d;
};
int main(int argc, _TCHAR* argv[])
{
MyUnion un;
un.n = 10; // 此时联合体中n为10
un.d = 3.14; // 此时联合体中d为3.14,而n为0
int n;
n = un.n;
return 0;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/161060.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...