// char16のindexとその文字を構成するchar16の個数のリスト作成
std::vector<std::pair<int32_t, int32_t>> getIndexList(const icu::UnicodeString& s) {
std::vector<std::pair<int32_t, int32_t>> c_c;
UErrorCode err;
icu::BreakIterator* bi = icu::BreakIterator::createCharacterInstance(
icu::Locale::getDefault(), err);
bi->setText(s);
int32_t current = bi->first();
while (current != icu::BreakIterator::DONE) {
int32_t prev = current;
current = bi->next();
if (current == UBRK_DONE) break;
c_c.emplace_back(prev, current - prev );
}
return c_c;
}
void WinPaint(HWND hWnd) {
// 元の文字列の定義
std::u16string u16s = u"aあغ山
經经";
////////////////////////////////////////
////////////////////////////////////////
icu::UnicodeString usr(u16s.c_str());
// char16のindexとその文字を構成するchar16の個数のリスト作成
std::vector<std::pair<int32_t, int32_t>> ilis = getIndexList(usr);
// 文字の置き換え
icu::UnicodeString B("B");
// 4文字目を「B」に書き換える
// UnicodeString::replace(先頭index,置換前の要素数,置換後の文字)
usr.replace(ilis[4].first, ilis[4].second, B);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////
//フォトの設定
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);
SetTextColor(hdc, RGB(0, 0, 0)); //文字の色を設定
SetBkColor(hdc, RGB(230, 230, 230)); //文字の背景色を設定
HFONT backup = (HFONT)SelectObject(hdc, hFont1); //Step2 フォントを適応
TEXTMETRIC tm;
GetTextMetrics(hdc, &tm);
int TH = 10;
////////////////////////////
//文字を一文字(書記素)ずつ表示
UErrorCode err;
icu::BreakIterator* bi = icu::BreakIterator::createCharacterInstance(
icu::Locale::getDefault(), err);
bi->setText(usr);
int32_t current = bi->first();
while (current != icu::BreakIterator::DONE) {
int32_t prev = current;
current = bi->next();
if (current == UBRK_DONE) break;
int32_t count = current - prev;//文字の長さ
icu::UnicodeString e;
usr.extract(prev, count, e);
// win32api のWCHARはutf16なのでただキャストするだけで変換できる
TextOutW(hdc, 10, TH, (WCHAR*)e.getBuffer(), e.length());//文字を書く
//改行
TH += tm.tmHeight+5;
}
SelectObject(hdc, backup);
EndPaint(hWnd, &ps);
}