CString 格式设置和消息框显示

提供了许多函数对 CString 对象进行格式设置和分析。 每当你必须操作 CString 对象时,都可以使用这些函数,但它们特别适合对将要在消息框文本中显示的字符串进行格式设置。

此函数组还包括用于显示消息框的全局例程。

CString 函数

名称 描述
AfxExtractSubString 从给定源字符串中提取由单个字符分隔的子字符串。
AfxFormatString1 使用给定字符串替换字符串表中包含的字符串中的格式字符“%1”。
AfxFormatString2 使用两个字符串替换字符串表中包含的字符串中的格式字符“%1”和“%2”。
AfxMessageBox 显示消息框。

要求

页眉afxwin.h

AfxExtractSubString

此全局函数可用于从给定源字符串中提取子字符串。

BOOL AFXAPI AfxExtractSubString (
    CString& rString,
    LPCTSTR lpszFullString,
    int iSubString,
    TCHAR chSep  = '\n');

参数

rString
对将接收单个子字符串的 CString 对象的引用。

lpszFullString
字符串,包含要从中提取内容的字符串全文。

iSubString
要从 lpszFullString 中提取的子字符串的从零开始的索引。

chSep
用于分隔子字符串的分隔符。

返回值

如果函数在提供的索引处成功提取了子字符串,则为 TRUE;否则为 FALSE

备注

使用已知的单个字符分隔每个子字符串时,此函数可用于从源字符串中提取多个子字符串。 每次调用此函数时,此函数都会从 lpszFullString 参数的开头进行搜索。

如果已将 lpszFullString 设置为 NULL,或者此函数到达了 lpszFullString 的末尾但未找到 iSubString+1 个指定分隔符,则此函数会返回 FALSE。 如果 lpszFullString 已设置为 NULL,则不会修改 rString 参数的原始值;否则,如果无法为指定索引提取子字符串,则会将 rString 参数设置为空字符串。

示例

// The following example extracts a series of name, value pairs from a
// given source string:

// Input string consisting of a number of name, value pairs
LPCTSTR lpszSource = _T("\"Name\"=\"John Smith\"\n")
_T("\"Company\"=\"Contoso, Ltd\"\n\"Salary\"=\"25,000\"");

CString strNameValue; // an individual name, value pair

int i = 0; // substring index to extract
while (AfxExtractSubString(strNameValue, lpszSource, i))
{
   // Prepare to move to the next substring
   i++;

   CString strName, strValue; // individual name and value elements

   // Attempt to extract the name element from the pair
   if (!AfxExtractSubString(strName, strNameValue, 0, _T('=')))
   {
      // Pass an error message to the debugger for display
      OutputDebugString(_T("Error extracting name\r\n"));
      continue;
   }

   // Attempt to extract the value element from the pair
   if (!AfxExtractSubString(strValue, strNameValue, 1, _T('=')))
   {
      // Pass an error message to the debugger for display
      OutputDebugString(_T("Error extracting value element\r\n"));
      continue;
   }

   // Pass the name, value pair to the debugger for display
   CString strOutput = strName + _T(" equals ") + strValue + _T("\r\n");
   OutputDebugString(strOutput);
}

要求

页眉afxwin.h

AfxFormatString1

nIDS 标识的模板字符串资源中,将 lpsz1 指向的字符串替换为字符 "%1" 的任何实例。

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

参数

rString
对在执行替换后将包含生成字符串的 CString 对象的引用。

nIDS
将对其执行替换的模板字符串的资源 ID。

lpsz1
一个字符串,将替换模板字符串中的格式字符 "%1"

注解

新构成的字符串存储在 rString 中。 例如,如果字符串表中的字符串为 "File %1 not found",并且 lpsz1 等效于 "C:\MYFILE.TXT",则 rString 将包含字符串 "File C:\MYFILE.TXT not found"。 此函数对为发送到消息框和其他窗口的字符串设置格式很有用。

如果格式字符 "%1" 多次出现在字符串中,则将执行多次替换。

示例

void DisplayFileNotFoundMessage(LPCTSTR pszFileName)
{
   CString strMessage;

   // The IDS_FILENOTFOUND string resource contains "Error: File %1 not found"
   AfxFormatString1(strMessage, IDS_FILENOTFOUND, pszFileName);
   // In the previous call, substitute the actual file name for the
   // %1 placeholder
   AfxMessageBox(strMessage);  // Display the error message
}

要求

页眉afxwin.h

AfxFormatString2

nIDS 标识的模板字符串资源中,将 lpsz1 指向的字符串替换为字符 "%1" 的任何实例,将 lpsz2 指向的字符串替换为字符 "%2" 的任何实例。

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

参数

rString
对在执行替换后将包含生成字符串的 CString 的引用。

nIDS
将对其执行替换的模板字符串的字符串表 ID。

lpsz1
一个字符串,将替换模板字符串中的格式字符 "%1"

lpsz2
一个字符串,将替换模板字符串中的格式字符 "%2"

备注

新构成的字符串存储在 rString 中。 例如,如果字符串表中的字符串为 "File %1 not found in directory %2",并且 lpsz1 指向 "MYFILE.TXT"lpsz2 指向 "C:\MYDIR",则 rString 将包含字符串 "File MYFILE.TXT not found in directory C:\MYDIR"

如果格式字符 "%1""%2" 多次出现在字符串中,则将执行多次替换。 它们不需按数字顺序排列。

示例

void DisplayFileNotFoundMessage(LPCTSTR pszFileName, LPCTSTR pszDirectory)
{
   CString strMessage;

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

要求

页眉afxwin.h

AfxMessageBox

在屏幕上显示消息框。

int AfxMessageBox(
    LPCTSTR lpszText,
    UINT nType = MB_OK,
    UINT nIDHelp = 0);

int AFXAPI AfxMessageBox(
    UINT nIDPrompt,
    UINT nType = MB_OK,
    UINT nIDHelp = (UINT) -1);

参数

lpszText
指向 CString 对象或以 null 结尾的字符串,其中包含要显示在消息框中的消息。

nType
消息框的样式。 将任何消息框样式应用于框。

nIDHelp
消息的帮助上下文 ID;0 表示将使用应用程序的默认帮助上下文。

nIDPrompt
一个独一无二的 ID,用于引用字符串表中的字符串。

返回值

如果因内存不足而无法显示消息框,则为零;否则返回以下值之一:

  • IDABORT:已选择“中止”按钮。

  • IDCANCEL:已选择“取消”按钮。

  • IDIGNORE:已选择“忽略”按钮。

  • IDNO:已选择“否”按钮。

  • IDOK:已选择“确定”按钮。

  • IDRETRY:已选择“重试”按钮。

  • IDYES:已选择“是”按钮。

如果消息框有“取消”按钮,则按下 ESC 键或选择“取消”按钮时,将返回 IDCANCEL 值。 如果消息框没有“取消”按钮,则按 ESC 键不起作用。

函数 AfxFormatString1AfxFormatString2 可用于设置消息框中显示的文本的格式。

注解

这个重载的函数的第一种形式显示消息框中 lpszText 所指向的文本字符串,并使用 nIDHelp 来描述帮助上下文。 当用户按下帮助键(通常为 F1)时,帮助上下文用于跳转到关联的帮助主题。

此函数的第二种形式使用 ID 为 nIDPrompt 的字符串资源在消息框中显示消息。 通过值 nIDHelp 找到关联的帮助页。 如果使用 nIDHelp 的默认值 (-1),则字符串资源 ID nIDPrompt 用于帮助上下文。 若要详细了解如何定义帮助上下文,请参阅技术说明 28

示例

// A simple message box, with only the OK button.
AfxMessageBox(_T("Simple message box."));

// A message box that uses a string from a string table
// with yes and no buttons and the stop icon.
// NOTE: nStringID is an integer that contains a valid id of
// a string in the current resource.
AfxMessageBox(nStringID, MB_YESNO | MB_ICONSTOP);

另请参阅

宏和全局函数
CStringT