网站首页

家园论坛

老版论坛

家园博客

业界新闻

技术文档

下载中心

速查中心

图片中心

硬件资讯
上一篇:实现和IE浏览器交互的几种方法的介绍 下一篇:用Winsock实现语音全双工通信
如何在RichEdit中实现上标下标

来源:http://www.ccrun.com 作者:ccrun 添加日期:2005-12-3 19:56:32 点击次数:

近来有好多朋友在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);
}

 

如果您有其他的想法和建议,欢迎来信探讨:info@ccrun.com

 
设为首页 | 加入收藏 | 业务办理 | 友情链接 | 论坛版面 | 浙ICP备07502118号 |