前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MFC之COleVariant[通俗易懂]

MFC之COleVariant[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-09 12:02:01
1.3K0
发布2022-09-09 12:02:01
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

COleVariant 本质上是一个枚举,用同一种类型来表达不同的子类型。如同boost中的variant。

例子

代码语言:javascript
复制
COleVariant var(3.6f);
float v = var.fltVal;
CString str("testCOleVariant");
COleVariant var2(str);
CString cpStr(var2.bstrVal);

例子

代码语言:javascript
复制
CString CMFCPropertyGridProperty::m_strFormatChar = _T("%c");
CString CMFCPropertyGridProperty::m_strFormatShort = _T("%d");
CString CMFCPropertyGridProperty::m_strFormatLong = _T("%ld");
CString CMFCPropertyGridProperty::m_strFormatUShort = _T("%u");
CString CMFCPropertyGridProperty::m_strFormatULong = _T("%u");
CString CMFCPropertyGridProperty::m_strFormatFloat = _T("%f");
CString CMFCPropertyGridProperty::m_strFormatDouble = _T("%lf");

CString CMFCPropertyGridProperty::FormatProperty()
{
	ASSERT_VALID(this);
	ASSERT_VALID(m_pWndList);

	COleVariant& var = m_varValue;

	CString strVal;

	.............

	switch (var.vt)
	{
	case VT_BSTR:
		strVal = var.bstrVal;
		break;

	case VT_I2:
		strVal.Format(m_strFormatShort, (short)var.iVal);
		break;

	case VT_I4:
	case VT_INT:
		strVal.Format(m_strFormatLong, (long)var.lVal);
		break;

	case VT_UI1:
		if ((BYTE)var.bVal != 0)
		{
			strVal.Format(m_strFormatChar, (TCHAR)(BYTE)var.bVal);
		}
		break;

	case VT_UI2:
		strVal.Format( m_strFormatUShort, var.uiVal);
		break;

	case VT_UINT:
	case VT_UI4:
		strVal.Format(m_strFormatULong, var.ulVal);
		break;

	case VT_R4:
		strVal.Format(m_strFormatFloat, (float)var.fltVal);
		break;

	case VT_R8:
		strVal.Format(m_strFormatDouble, (double)var.dblVal);
		break;

	case VT_BOOL:
		strVal = var.boolVal == VARIANT_TRUE ? m_pWndList->m_strTrue : m_pWndList->m_strFalse;
		break;

	default:
		// Unsupported type
		strVal = _T("*** error ***");
	}

	return strVal;
}

COleVariant框架内容

代码语言:javascript
复制
class COleVariant : public tagVARIANT
{
	COleVariant(LPCTSTR lpszSrc);
	COleVariant(LPCTSTR lpszSrc, VARTYPE vtSrc); // used to set to ANSI string
	COleVariant(CString& strSrc);

	COleVariant(BYTE nSrc);
	COleVariant(short nSrc, VARTYPE vtSrc = VT_I2);
	COleVariant(long lSrc, VARTYPE vtSrc = VT_I4);
	COleVariant(const COleCurrency& curSrc);

	COleVariant(float fltSrc);
	COleVariant(double dblSrc);
	COleVariant(const COleDateTime& timeSrc);
....................................
	const COleVariant& operator=(float fltSrc);
	const COleVariant& operator=(double dblSrc);
	const COleVariant& operator=(const COleDateTime& dateSrc);

	const COleVariant& operator=(const CByteArray& arrSrc);
	const COleVariant& operator=(const CLongBinary& lbSrc);
};

	void Clear();
	void ChangeType(VARTYPE vartype, LPVARIANT pSrc = NULL);
	void Attach(VARIANT& varSrc);
	VARIANT Detach();
   void GetByteArrayFromVariantArray(CByteArray& bytes);
void SetString(LPCTSTR lpszSrc, VARTYPE vtSrc); // used to set ANSI string
	operator LPVARIANT();
	operator LPCVARIANT() const
/
struct tagVARIANT
    {    union 
        {
        struct __tagVARIANT
            {
	VARTYPE vt;
	.......................
            union 
                {
                LONGLONG llVal;
                LONG lVal;
                BYTE bVal;
                SHORT iVal;
                FLOAT fltVal;
                DOUBLE dblVal;
                ...........
                DATE date;
                BSTR bstrVal;
                IUnknown *punkVal;
	 IDispatch *pdispVal;
	..............
	}
	
COleVariant::COleVariant(float fltSrc)
		{ vt = VT_R4; fltVal = fltSrc; }
_AFXDISP_INLINE COleVariant::COleVariant(CString& strSrc)
		{ vt = VT_EMPTY; *this = strSrc; }
	
const COleVariant& COleVariant::operator=(const CString& strSrc)
{
	// Free up previous VARIANT
	Clear();

	vt = VT_BSTR;
	bstrVal = strSrc.AllocSysString();

	return *this;
	}
	
typedef unsigned short VARTYPE;

/*
 * VARENUM usage key,
 *
 * * [V] - may appear in a VARIANT
 * * [T] - may appear in a TYPEDESC
 * * [P] - may appear in an OLE property set
 * * [S] - may appear in a Safe Array
 *
 *
 *  VT_EMPTY            [V]   [P]     nothing
 *  VT_NULL             [V]   [P]     SQL style Null
 *  VT_I2               [V][T][P][S]  2 byte signed int
 *  VT_I4               [V][T][P][S]  4 byte signed int
 *  VT_R4               [V][T][P][S]  4 byte real
 *  VT_R8               [V][T][P][S]  8 byte real
 *  VT_CY               [V][T][P][S]  currency
 *  VT_DATE             [V][T][P][S]  date
 *  VT_BSTR             [V][T][P][S]  OLE Automation string
 *  VT_DISPATCH         [V][T]   [S]  IDispatch *
 *  VT_ERROR            [V][T][P][S]  SCODE
 *  VT_BOOL             [V][T][P][S]  True=-1, False=0
 *  VT_VARIANT          [V][T][P][S]  VARIANT *
 *  VT_UNKNOWN          [V][T]   [S]  IUnknown *
 *  VT_DECIMAL          [V][T]   [S]  16 byte fixed point
 .......................................

enum VARENUM
    {	VT_EMPTY	= 0,
	VT_NULL	= 1,
	VT_I2	= 2,
	VT_I4	= 3,
	VT_R4	= 4,
	VT_R8	= 5,
	VT_CY	= 6,
	VT_DATE	= 7,
	VT_BSTR	= 8,
	.........................................
	    } ;

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/161039.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档