CString
書式設定とメッセージ ボックスの表示
オブジェクトの書式設定と解析 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
、パラメーターはrString
元の値から変更されません。それ以外の場合、rString
指定したインデックスに対して部分文字列を抽出できなかった場合、パラメーターは空の文字列に設定NULL
されます。
例
// 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"
の任意の"%2"
インスタンスに対してlpsz2
指す文字列を、指定されたテンプレート文字列リソースで置nIDS
き換えます。
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"
、を指"MYFILE.TXT"
し、lpsz1
指している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
[はい] ボタンが選択されました。
メッセージ ボックスに [キャンセル] ボタンがある場合は、 IDCANCEL
ESC キーを押すか、[キャンセル] ボタンを選択した場合に値が返されます。 メッセージ ボックスに [キャンセル] ボタンがない場合、Esc キーを押しても効果はありません。
この関数 AfxFormatString1
は、 AfxFormatString2
メッセージ ボックスに表示されるテキストの書式設定に役立ちます。
解説
このオーバーロードされた関数の最初の形式は、メッセージ ボックスで指す lpszText
テキスト文字列を表示し、ヘルプ コンテキストを記述するために使用 nIDHelp
します。 ヘルプ コンテキストは、ユーザーがヘルプ キー (通常は F1 キー) を押したときに関連するヘルプ トピックにジャンプするために使用されます。
関数の 2 番目の形式では、ID nIDPrompt
を持つ文字列リソースを使用してメッセージ ボックスにメッセージを表示します。 関連付けられているヘルプ ページは、次の nIDHelp
値を使用して見つかります。 既定値 nIDHelp
(-1) が使用されている場合は、 nIDPrompt
文字列リソース ID がヘルプ コンテキストに使用されます。 ヘルプ コンテキストの定義の詳細については、テクニカル ノート 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);
関連項目
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示