近來有好多朋友在csdn問如何在RichEdit中實現上標下標,查了好多資料都沒找到。今天無意間在Delphi區閒逛,看到一份實現上下標的文章,於是整理成CB語言的貼出來。
以下代碼已測試通過。在Form中放置一個RichEdit,兩個Button,完整代碼如下:
//--------------------------------------------------------------------------- typedef enum
{
CFM_Superscript, // 上標
CFM_Subscript, // 下標
CFM_Normal // 普通文本
}TCharacterFormat;
void MySetCharFormat(TRichEdit *RichEdit, TCharacterFormat CharacterFormat)
{
// ccrun(老妖)根據Delphi超級猛料中的資料修改而成
// 歡迎光臨 C++ Builder 研究 http://www.ccrun.com
TCharFormat Format;
Format.cbSize = sizeof(Format);
Format.dwMask = CFM_OFFSET;
// Character offset, in twips, from the baseline.
// If the value of this member is positive,
// the character is a superscript;
// if it is negative, the character is a subscript.
switch(CharacterFormat)
{
case CFM_Superscript:
Format.yOffset = 60;
break;
case CFM_Subscript:
Format.yOffset = -60;
break;
case CFM_Normal:
Format.yOffset = 0;
break;
default:
break;
}
// The EM_SETCHARFORMAT message sets character formatting in a rich edit control.
// SCF_SELECTION: Applies the formatting to the current selection
RichEdit->Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(&Format));
}
//---------------------------------------------------------------------------
// 先選中Richedit中的部分文本,然後點擊此按鈕,選中文本將變成上標
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// 上標
MySetCharFormat(RichEdit1, CFM_Superscript);
}
//---------------------------------------------------------------------------
// 先選中Richedit中的部分文本,然後點擊此按鈕,選中文本將變成下標
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// 下標
MySetCharFormat(RichEdit1, CFM_Subscript);
}
如果您有其他的想法和建議,歡迎來信探討:[email protected]