CStringオブジェクトの書式設定と解析には、いくつかの関数が用意されています。 これらの関数は、 CString オブジェクトを操作する必要があるときはいつでも使用できますが、メッセージ ボックス テキストに表示される文字列を書式設定する場合に特に便利です。
この関数のグループには、メッセージ ボックスを表示するためのグローバル ルーチンも含まれています。
CString 関数
| 名前 | 説明 |
|---|---|
AfxExtractSubString |
特定のソース文字列から 1 文字で区切られた部分文字列を抽出します。 |
AfxFormatString1 |
指定された文字列を、文字列テーブルに含まれる文字列の書式指定文字 "%1" に置き換えます。 |
AfxFormatString2 |
文字列テーブルに含まれる文字列の書式指定文字 "%1" と "%2" に 2 つの文字列を置き換えます。 |
AfxMessageBox |
メッセージ ボックスを表示します。 |
要件
ヘッダー afxwin.h
AfxExtractSubString
このグローバル関数を使用して、特定のソース文字列から部分文字列を抽出できます。
BOOL AFXAPI AfxExtractSubString (
CString& rString,
LPCTSTR lpszFullString,
int iSubString,
TCHAR chSep = '\n');
パラメーター
rString
個々の部分文字列を受け取る CString オブジェクトへの参照。
lpszFullString
抽出する文字列のフルテキストを含む文字列。
iSubString
lpszFullStringから抽出する部分文字列の 0 から始まるインデックス。
chSep
部分文字列を区切るために使用される区切り文字。
戻り値
TRUE 関数が指定されたインデックスで部分文字列を正常に抽出した場合。それ以外の場合は FALSE。
解説
この関数は、既知の 1 文字が各部分文字列を区切る場合に、ソース文字列から複数の部分文字列を抽出する場合に便利です。 この関数は、呼び出されるたびに、 lpszFullString パラメーターの先頭から検索します。
この関数は、lpszFullStringが NULL に設定されているか、指定された区切り文字が 1 回以上出現iSubString検出されずにlpszFullStringの末尾に達した場合、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によって識別されるテンプレート文字列リソース内の"%1"文字のインスタンスに対して、lpsz1が指す文字列を置き換えます。
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
lpsz1が指す文字列を"%1"文字のインスタンスに置き換え、nIDSで識別されるテンプレート文字列リソースで、"%2"文字のインスタンスに対してlpsz2が指す文字列を置き換えます。
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。
戻り値
メッセージ ボックスを表示する十分なメモリがない場合は 0。それ以外の場合、次のいずれかの値が返されます。
IDABORT[中止] ボタンが選択されました。IDCANCEL[キャンセル] ボタンが選択されました。IDIGNORE[無視] ボタンが選択されました。IDNO[いいえ] ボタンが選択されました。IDOK[OK] ボタンが選択されました。IDRETRY[再試行] ボタンが選択されました。IDYES[はい] ボタンが選択されました。
メッセージ ボックスに [キャンセル] ボタンがある場合は、esc キーを押すか、[キャンセル] ボタンを選択した場合、 IDCANCEL 値が返されます。 メッセージ ボックスに [キャンセル] ボタンがない場合、Esc キーを押しても効果はありません。
AfxFormatString1関数とAfxFormatString2関数は、メッセージ ボックスに表示されるテキストの書式設定に役立ちます。
解説
このオーバーロードされた関数の最初の形式は、メッセージ ボックスに lpszText が指すテキスト文字列を表示し、 nIDHelp を使用してヘルプ コンテキストを記述します。 ヘルプ コンテキストは、ユーザーがヘルプ キー (通常は F1 キー) を押したときに関連するヘルプ トピックにジャンプするために使用されます。
関数の 2 番目の形式では、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);