enstring.cpp

来自「管理项目进度工具的原代码」· C++ 代码 · 共 141 行

CPP
141
字号
// EnString.cpp: implementation of the CEnString class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EnString.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CEnString::CEnString() : CString()
{
}

CEnString::CEnString(LPCTSTR lpszFormat, ... )
{
	if (lpszFormat)
	{
		ASSERT(AfxIsValidString(lpszFormat));

		va_list argList;
		va_start(argList, lpszFormat);
		CString::FormatV(lpszFormat, argList);
		va_end(argList);
	}
}

CEnString::CEnString(UINT nFormatID, ...)
{
	if (nFormatID)
	{
		CString strFormat;
		VERIFY(strFormat.LoadString(nFormatID) != 0);

		va_list argList;
		va_start(argList, nFormatID);
		CString::FormatV(strFormat, argList);
		va_end(argList);
	}
}

CEnString::~CEnString()
{

}

CSize CEnString::FormatDC(CDC* pDC, int nWidth, int nStyle)
{
	CRect rect(0, 0, nWidth, 20);
	UINT uFlags = DT_CALCRECT | DT_SINGLELINE | DT_MODIFYSTRING;

	// special case: ES_START
	if (nStyle == ES_START)
		FormatDCEx(pDC, nWidth, nStyle);
	else
	{
		switch (nStyle)
		{
			case ES_END:
				uFlags |= DT_END_ELLIPSIS;
				break;

			case ES_WORD:
				uFlags |= DT_WORD_ELLIPSIS;
				break;

			case ES_PATH:
				uFlags |= DT_PATH_ELLIPSIS;
				break;
		}

		::DrawText(pDC->GetSafeHdc(), GetBuffer(GetLength() + 4), -1, rect, uFlags);
		ReleaseBuffer();

		// if its still too big then do our internal version
		if (rect.Width() > nWidth)
			FormatDCEx(pDC, nWidth, nStyle);
	}

	return pDC->GetTextExtent(*this);
}

CSize CEnString::Draw(CDC* pDC, LPRECT lpRect, int nStyle)
{
	// we must do the format independently of DrawText because we override it
	CEnString sTemp(*this);
	sTemp.FormatDC(pDC, lpRect->right - lpRect->left, nStyle);

	pDC->DrawText(sTemp, lpRect, DT_SINGLELINE);

	return pDC->GetTextExtent(sTemp);
}

CSize CEnString::FormatDCEx(CDC* pDC, int nWidth, int nStyle)
{
	CString sFinalText = *this;

	CSize sizeText = pDC->GetTextExtent(sFinalText);
	CSize sizeEllipsis = pDC->GetTextExtent("...");

	bool bEndEllipsis = (nStyle == ES_END || nStyle == ES_WORD);
	
	// truncate string if too long adding ellipsis (...)
	if ((sizeText.cx + sizeEllipsis.cx) > nWidth)
	{
	    while ((sizeText.cx + sizeEllipsis.cx) > nWidth)
	    {
			// truncate another char
			if (bEndEllipsis)
			{
				sFinalText = sFinalText.Left(sFinalText.GetLength() - 1);
				sizeText = pDC->GetTextExtent(CString("..." + sFinalText));
			}
			else
			{
				sFinalText = sFinalText.Mid(1);
		        sizeText = pDC->GetTextExtent(CString(sFinalText + "..."));
			}

			if (sFinalText.IsEmpty())
				break;
		}
			
		if (bEndEllipsis)
			sFinalText += "...";
		else
			sFinalText = "..." + sFinalText;
	}

	*this = CEnString(sFinalText);

	return pDC->GetTextExtent(*this);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?