2009/02/02

[MFC] AfxFormatString1() 과 AfxFormatString2()를 이용한 파라메터 스트링의 활용

void AfxFormatString1(
   CString& rString,
   UINT nIDS,
   LPCTSTR lpsz1
);

void AfxFormatString2(
   CString& rString,
   UINT nIDS,
   LPCTSTR lpsz1,
   LPCTSTR lpsz2
);

이 함수들은 AfxExtractSubString() 함수에 대한 정보를 찾다가 알게된 함수들이다.
AfxFormatString1 함수는 String Table 의 String 중 %1 을 특정 문자열로 치환 해주고 AfxFormatString2 함수는  %1,%2 를 특정 문자열로 치환해 준다.

예들 들어 String Table 에 다음과 같은 문자열이 정이 되어 있을때.
IDS_TEST "파일 %1을 찾을 수 없습니다."

CString strTest;
AfxFormatString1( strTest, IDS_TEST, _T("version.dat") );

strTest 는 "파일 version.dat 을 찾을 수 없습니다." 란 문자열이 들어가게 된다.

문자열만 치환 할 수 있고 개수도 한개 또는 두 개로 제한적이라서 활용 범위는 크지 않지만 가끔 유용하게 사용할 수 도 있을것 같다.

아래는 MSDN 사이트에서 발췌한 사용 예이다.(http://msdn.microsoft.com/en-us/library/f1w5d2h2.aspx)
void DisplayFileNotFoundMessage(LPCTSTR pszFileName, LPCTSTR pszDirectory)
{
CString strMsg;

// The IDS_FILENOTFOUND string resource contains "Error: File %1 not
// found in directory %2"
AfxFormatString2(strMsg, IDS_FILENOTFOUND2, pszFileName, pszDirectory);
// In the previous call, substitute the actual file and directory
// names into the message string
AfxMessageBox(strMsg); // Display the error message
}

Original Post : http://neodreamer-dev.tistory.com/245

No comments :

Post a Comment