Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 9 Strings Visual C++ and MFC Fundamentals


{
CExerciseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

CString Country("Cote d'Ivoire");
const char *Area = Country.GetBuffer(2);

pDC->TextOut(10, 10, Country);
pDC->TextOut(10, 30, Area);
}

If you want to specify the number of characters to consider when retrieving the buffer,
use the CString::GetBufferSetLength() method. Its syntax is:

LPTSTR GetBufferSetLength(int nNewLength);

The nNewLength argument specifies the new length of the buffer.

To declare a CString variable that has a single character, use the following constructor:

CString(TCHAR ch, int nRepeat = 1);

The character can be included between single-quotes. Here is an example:

void CExoView::OnDraw(CDC* pDC)
{
CExoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

CString Letter('A');

pDC->TextOut(20, 22, Letter);
}

If you want the character to be repeated more than once, you can pass a second argument
as nRepeat that holds the number of times that the ch character must be repeated. Here is
an example:

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

CString Letter('A');
pDC->TextOut(20, 20, Letter);

CString Character('$', 25);
pDC->TextOut(20, 40, Character);
Free download pdf