共用方式為


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 此函式都會從參數的開頭搜尋。

如果 設定為 NULL 或 函式到達 結尾 lpszFullString ,但找不到 iSubString 指定分隔符號的 +1 次,則此函式會傳回 FALSElpszFullString rString如果 lpszFullString 設定為 NULL ,則參數將不會從其原始值修改;否則,如果無法擷取指定索引的子字串, 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

將 所指向 lpsz1 的字串取代為 所識別 nIDS 之範本字串資源中的任何字元 "%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
將執行替代之範本字串的字串資料表識別碼。

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
訊息的說明內容識別碼;0 表示將使用應用程式的預設 [說明] 內容。

nIDPrompt
用來參考字串資料表中字串的唯一識別碼。

傳回值

如果記憶體不足而無法顯示訊息方塊,則為零;否則,會傳回下列其中一個值:

  • IDABORT 已選取 [中止] 按鈕。

  • IDCANCEL 已選取 [取消] 按鈕。

  • IDIGNORE 已選取 [忽略] 按鈕。

  • IDNO 已選取 [無] 按鈕。

  • IDOK 已選取 [確定] 按鈕。

  • IDRETRY 已選取 [重試] 按鈕。

  • IDYES 已選取 [是] 按鈕。

如果訊息方塊有 [取消] 按鈕, IDCANCEL 則會在按下 ESC 鍵或選取 [取消] 按鈕時傳回值。 如果訊息方塊沒有 [取消] 按鈕,按下 ESC 鍵就沒有作用。

AfxFormatString1函式和 AfxFormatString2 可用於格式化出現在訊息方塊中的文字。

備註

此多載函式的第一種形式會顯示訊息方塊中所指向 lpszText 的文字字串,並使用 nIDHelp 來描述說明內容。 當使用者按下 [說明] 鍵 (通常是 F1) 時,會使用 [說明] 內容跳至相關聯的 [說明] 主題。

函式的第二種形式會使用識別碼為 nIDPrompt 的字串資源,在訊息方塊中顯示訊息。 相關聯的 [說明] 頁面可透過 的值 nIDHelp 找到。 如果使用 的預設值 nIDHelp (-1),字串資源識別碼 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