Re: how to show char "\t" in eVC3 by rockone
rockone
Wed Dec 01 00:05:35 CST 2004
I implement the function TabbedTextOut() under eVC++3.
But I don't think it is enough good.
Any suggestion or tip would of great help.
>>
>>
/////////////////////////////////////////////////////////////////////////////////////////////
// all input parameters are same to TabbedTextOut,
// but return type is void, not LONG.
/////////////////////////////////////////////////////////////////////////////////////////////
void CMyView::myTabbedTextOut(HDC hDC,// handle to DC
int X, // x-coord of start
int Y, // y-coord of start
LPCTSTR lpString, // character string
int nCount,// number of characters
int nTabPositions,// number of tabs in array
CONST LPINT lpnTabStopPositions,// array of tab positions
int nTabOrigin // start of tab expansion
)
{
CString sTabString = lpString;
int nNextTabCharPos = sTabString.Find(L"\t");
if( nTabOrigin > 0) // if first tab character postion is not
{ // the start of tab expansion
int nTabCount = 0;
do{
nNextTabCharPos = sTabString.Find(L"\t", nNextTabCharPos+1);
nTabCount++;
if(nNextTabCharPos == -1)
break;
} while (nTabCount != nTabOrigin);
}
// Initialize the rectangle in which the text is to be formatted
RECT Rect;
Rect.left = X;
Rect.top = Y;
Rect.bottom = Rect.top + 20; // hard-code
Rect.right = X + lpnTabStopPositions[nTabOrigin];
LPRECT lpRect = &Rect;
CString sTemp = _T("");
for(int i=nTabOrigin; i<nTabPositions; i++)
{
sTemp.Empty(); // empty the string before using
if( nNextTabCharPos != -1 )
{
sTemp = sTabString.Left(nNextTabCharPos+1);
sTabString = sTabString.Right(nCount - nNextTabCharPos-1);
DrawText(hDC, sTemp, sTemp.GetLength(), lpRect,
DT_EXPANDTABS | DT_NOCLIP);
}
// get the length of the remanent character string
nCount = nCount - sTemp.GetLength();
// if there is no remanent character string
// or the character string doesn't contain tab characters
if(nCount == 0 || nNextTabCharPos == -1)
{
break;
}
nNextTabCharPos = sTabString.Find(L"\t");
Rect.left = X + lpnTabStopPositions[i];
if(i<nTabPositions-1)
{
Rect.right = X + lpnTabStopPositions[i+1];
lpRect = &Rect;
}
}
if(nCount != 0)// writes the remanent character string
{ // (that maybe not contain tab characters)
ExtTextOut(hDC, Rect.left, Rect.top, 0, NULL,
sTabString, sTabString.GetLength(), NULL);
}
}