Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / WTL

A fast and lightweight cell control

Rate me:
Please Sign up or sign in to vote.
4.42/5 (31 votes)
11 Mar 2008CPOL1 min read 90.9K   4.5K   81  
A fast and lightweight cell control for displaying tabular data. The cell is a custom control derived from ATL::CWindow.
// MyCell - version 1.0
// Written by Yanxueming <yanxm2003@hotmail.com>
// Copyright (C) 2006-2007
// All rights reserved.
//
// The code and information is provided "as-is" without
// warranty of any kind, either expressed or implied.
#pragma once
#import <MSXML3.dll> named_guids raw_interfaces_only
namespace mycell{
	class XmlExcelBase
	{
	protected:
		bool bUserFormatted_;
		XmlExcelBase():bUserFormatted_(false)
		{
		};
	public:
		BOOL ReadIntFromAttribute(MSXML2::IXMLDOMElement* pE,BSTR bstrAttrName,int& val)const
		{
			CComVariant v;
			pE->getAttribute(bstrAttrName,&v);
			if(VT_BSTR==v.vt && SUCCEEDED(v.ChangeType(VT_I4))){
				val=v.lVal;
				return TRUE;
			}
			return FALSE;
		}
		BOOL ReadDoubleFromAttribute(MSXML2::IXMLDOMElement* pE,BSTR bstrAttrName,double& val)const
		{
			CComVariant v;
			pE->getAttribute(bstrAttrName,&v);
			if(VT_BSTR==v.vt && SUCCEEDED(v.ChangeType(VT_R8))){
				val=v.dblVal;
				return TRUE;
			}
			return FALSE;
		}
		//���Ҫ����lpszText��\r\n
		void AppendTextNode(MSXML2::IXMLDOMDocument* pDoc,MSXML2::IXMLDOMNode* pElement,BSTR lpszText)
		{
			if(bUserFormatted_){
				CComPtr<MSXML2::IXMLDOMText> pTextNode;
				pDoc->createTextNode(lpszText,&pTextNode);
				pElement->appendChild(pTextNode,NULL);
			}
		}
		//���pEû��������pE�Ƴ���
		void RemoveEmptyAttributeNode(MSXML2::IXMLDOMElement* pParentE,MSXML2::IXMLDOMElement* pE)
		{
				CComPtr<MSXML2::IXMLDOMNamedNodeMap> pNM;
				pE->get_attributes(&pNM);
				long len;
				pNM->get_length(&len);
				if(0==len){
					pParentE->removeChild(pE,NULL);
				}
		}
		//���pEû���ӽڵ���pE�Ƴ���
		void RemoveEmptyChildNode(MSXML2::IXMLDOMElement* pParentE,MSXML2::IXMLDOMElement* pE)
		{
				CComPtr<MSXML2::IXMLDOMNamedNodeMap> pNM;
				VARIANT_BOOL bHasChild;
				pE->hasChildNodes(&bHasChild);
				if(!bHasChild){
					pParentE->removeChild(pE,NULL);
				}
		}

		CComBSTR EXlLineStyleToString(EXlLineStyle ls)
		{
			CComBSTR str;
			switch(ls)
			{
			case xlLineStyleNone:
				break;
			case xlContinuous:str=L"Continuous";break;
			case xlDash:str=L"Dash";break;
			case xlDashDot:str=L"DashDot";break;
			case xlDashDotDot:str=L"DashDotDot";break;
			case xlDot:str=L"Dot";break;
			case xlDouble:str=L"Double";break;
			case xlSlantDashDot:
				break;
			}
			return str;
		}
		EXlLineStyle EXlLineStyleFromString(BSTR bstrLineStyle)
		{
			const CComBSTR str(bstrLineStyle);
			if(str==L"Continuous") return xlContinuous;
			if(str==L"Dash") return xlDash;
			if(str==L"DashDot") return xlDashDot;
			if(str==L"DashDotDot") return xlDashDotDot;
			if(str==L"Dot") return xlDot;
			if(str==L"Double") return xlDouble;
			//case xlSlantDashDot:
			//	break;
			//}
			return xlLineStyleNone;
		}
		CComBSTR EXlHAlignToStr(EXlHAlign ha)
		{
			CComBSTR bstrHAlignment(L"Center");
			switch(ha)
			{
			case xlHAlignCenter:bstrHAlignment=L"Center";break;// = 0,				//����				
			case xlHAlignCenterAcrossSelection:bstrHAlignment=L"CenterAcrossSelection";break;//,	//�����
			case xlHAlignDistributed:bstrHAlignment=L"Justify";break;//,			//���˶���
			case xlHAlignFill:bstrHAlignment=L"Fill";break;//,					//���
			case xlHAlignGeneral:bstrHAlignment=L"Top";break;//,				//����
			case xlHAlignJustify:bstrHAlignment=L"Distributed";break;//,				//��ɢ����
			case xlHAlignLeft:bstrHAlignment=L"Left";break;//,					//����������
			case xlHAlignRight:bstrHAlignment=L"Right";break;//				//���ң�������
			}
			return bstrHAlignment;
		}
		EXlHAlign EXlHAlignFromStr(BSTR _bstrHAlignment)
		{
			const CComBSTR bstrHAlignment(_bstrHAlignment);
			if(bstrHAlignment==L"Center") return xlHAlignCenter;
			if(bstrHAlignment==L"CenterAcrossSelection") return xlHAlignCenterAcrossSelection;	//�����
			if(bstrHAlignment==L"Justify") return xlHAlignDistributed;//,			//���˶���
			if(bstrHAlignment==L"Fill") return xlHAlignFill;//,					//���
			if(bstrHAlignment==L"Top") return xlHAlignGeneral;//,				//����
			if(bstrHAlignment==L"Distributed") return xlHAlignJustify;//,				//��ɢ����
			if(bstrHAlignment==L"Left") return xlHAlignLeft;//,					//����������
			if(bstrHAlignment==L"Right") return xlHAlignRight;//				//���ң�������
			return xlHAlignCenter;
		}

		CComBSTR EXlVAlignToStr(EXlVAlign va)
		{
			CComBSTR bstrVAlignment(L"Bottom");
			switch(va)
			{
			case xlVAlignBottom:bstrVAlignment=L"Bottom";break;// = 0,	//����
			case xlVAlignCenter:bstrVAlignment=L"Center";break;//,		//����
			case xlVAlignDistributed:bstrVAlignment=L"Justify";break;//,//���˶���
			case xlVAlignJustify:bstrVAlignment=L"Distributed";break;//,	//��ɢ����
			case xlVAlignTop:bstrVAlignment=L"Top";break;//		    //����
			}
			return bstrVAlignment;
		}
		EXlVAlign EXlVAlignFromStr(BSTR _bstrVAlignment)
		{
			const CComBSTR bstrVAlignment(_bstrVAlignment);
			if(bstrVAlignment==L"Bottom") return xlVAlignBottom;// = 0,	//����
			if(bstrVAlignment==L"Center") return xlVAlignCenter;//,		//����
			if(bstrVAlignment==L"Justify") return xlVAlignDistributed;//,//���˶���
			if(bstrVAlignment==L"Distributed") return xlVAlignJustify;//,	//��ɢ����
			if(bstrVAlignment==L"Top") return xlVAlignTop;//		    //����
			return xlVAlignCenter;
		}
		//ˮƽ�����ת��������
		int X_Point2Pixel(double nPoint)const
		{
			HDC hDC=GetDC(NULL);
			const double dpi=GetDeviceCaps(hDC,LOGPIXELSX);
			ReleaseDC(NULL,hDC);
			return int(nPoint*dpi)/72;
		}
		//��ֱ�����ת��������
		int Y_Point2Pixel(double nPoint)const
		{
			HDC hDC=GetDC(NULL);
			const double dpi=GetDeviceCaps(hDC,LOGPIXELSY);
			ReleaseDC(NULL,hDC);
			return int(nPoint*dpi)/72;
		}
		//
		//ˮƽ��������ת������
		double X_Pixel2Point(int nPixel)const
		{
			HDC hDC=GetDC(NULL);
			const double dpi=GetDeviceCaps(hDC,LOGPIXELSX);
			ReleaseDC(NULL,hDC);
			return (nPixel*72.0f)/dpi;
		}
		//��ֱ��������ת������
		double Y_Pixel2Point(int nPixel)const
		{
			HDC hDC=GetDC(NULL);
			const double dpi=GetDeviceCaps(hDC,LOGPIXELSY);
			ReleaseDC(NULL,hDC);
			return (nPixel*72.0f)/dpi;
		}
	};
}//namespace mycell

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
China China
My name is Yanxueming,i live in Chengdu China.Graduated from UESTC in 1999.

Comments and Discussions