`EM_SETCHARFORMAT` (wParam=`SCF_SELECTION`) 設定選取文字格式。`CHARFORMAT` 結構 `dwMask=CFM_OFFSET`,`yOffset` (twips, 1/1440 吋) 正值=上標、負值=下標、0=正常。
enum TCharacterFormat { CFM_Superscript, CFM_Subscript, CFM_Normal };
void MySetCharFormat(TRichEdit *RE, TCharacterFormat fmt)
{
CHARFORMAT cf = {0};
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_OFFSET;
switch (fmt) {
case CFM_Superscript: cf.yOffset = 60; break; // ~1/24 吋
case CFM_Subscript: cf.yOffset = -60; break;
case CFM_Normal: cf.yOffset = 0; break;
}
RE->Perform(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
}
// 使用:先選取文字,再呼叫
MySetCharFormat(RichEdit1, CFM_Superscript); // 上標
MySetCharFormat(RichEdit1, CFM_Subscript); // 下標