Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 10: Characteristics of a Window's Frame


9.4.2 String Initialization..............................................................................


So, to deaclare and initialize a CString variable, you can use one of the above
constructors. Here is an example:

void CExerciseView::OnDraw(CDC* pDC)
{
CExerciseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

CString Term("Information Superhighway");

pDC->TextOut(20, 20, Term);
}

The above constructors mainly allow you to supply a null-terminated string as the initial
value of the variable. In fact, you can first declare a null-terminated C string and pass it
as argument to the constructor to initialize the string. Here is an example:

void CExerciseView::OnDraw(CDC* pDC)
{
CExerciseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

const char *Ville("Antananarivo");
CString Capital(Ville);

pDC->TextOut(10, 20, Ville);
pDC->TextOut(10, 40, Capital);
}

You can also create a string using the String Table and specifying its value. To initialize a
CString variable for such a string, call the CString::LoadString() method. Its syntax is:

BOOL LoadString(UINT nID);

The nID argument is the identifier of the item created in the String Table

9.4.3 The String and its Length...................................................................


After initializing a string, it assumes a length that represents its number of characters. To
get the length of a string, you can call the CString::GetLength() method whose syntax
is:

int GetLength( ) const;

However you have declared and initialized the CString variable using one of the above
constructors, if you want to retrieve the value stored in the variable, you can call the
CString::GetBuffer() method. Its syntax is:

LPTSTR GetBuffer( int nMinBufLength );

The nMinBufLength argument specifies the minimum number of characters to consider
from the string. Here is an example:

void CExerciseView::OnDraw(CDC* pDC)
Free download pdf