Skip to content

富文本编辑框控件 (SRichEdit)

Warning

The current page still doesn't have a translation for this language.

You can read it through google translate.

富文本编辑框是一个功能强大的文本编辑控件,基于 Windows 的 RichEdit 控件实现。它支持富文本格式、样式设置、OLE 对象嵌入等高级功能。这是一个无窗口(Windowless)的实现,完全集成在 SOUI 的渲染体系中。

基本信息

  • 类名SRichEdit
  • 控件标签richedit
  • 基类SWindow
  • 功能:提供富文本编辑功能,支持文本样式、OLE对象等

属性说明

基本属性

属性名 类型 默认值 说明
text string - 编辑框文本内容
cueText string - Cue文本(提示文本)
cueColor color - Cue文本颜色
style int - richedit style
maxBuf int - 最大可编辑缓冲长度
rich bool 1 是否支持富文本(0-否,1-是)
vertical bool 0 是否支持文本竖排(0-否,1-是)
wordWrap bool 1 是否支持文本折行(0-否,1-是)
allowBeep bool 1 是否支持Beeping(0-否,1-是)
autoWordSel bool 1 双击时自动选择单词(0-否,1-是)
vcenter bool 0 单行文本时自动垂直居中(0-否,1-是)
rtf string - 初始化时显示的rtf文件资源
notifyChange bool 0 内容改变时发送EN_CHANGE通知消息(0-否,1-是)

滚动条属性

属性名 类型 默认值 说明
hscrollBar bool 0 显示水平滚动条(0-否,1-是)
vscrollBar bool 0 显示垂直滚动条(0-否,1-是)
autoHscroll bool 1 显示水平滚动条,内容不够时自动隐藏(0-否,1-是)
autoVscroll bool 1 显示垂直滚动条,内容不够时自动隐藏(0-否,1-是)

编辑属性

属性名 类型 默认值 说明
multiLines bool 0 多行显示(0-否,1-是)
readOnly bool 0 只读状态(0-否,1-是)
wantReturn bool 0 处理回车按键,在Dialog中该属性将导致Dialog的默认按钮接收不到Enter(0-否,1-是)
password bool 0 密码框输入(0-否,1-是)
passwordChar string - 密码输入时替换文本,只允许单字符
number bool 0 只允许输入数字(0-否,1-是)
enableDragdrop bool 0 是否允许文件拖放(0-否,1-是)
autoSel bool 1 获得焦点时自动选中所有文本(0-否,1-是)

外观属性

属性名 类型 默认值 说明
colorText color 0 文本颜色
colorBkgnd color FFFFFF 背景颜色
align string left 文本对齐方式
font string default 默认字体
margin-x int 2 水平边距
margin-y int 2 垂直边距

使用示例

基本富文本编辑框

<richedit name="reBasic" 
          pos="10,10,410,110" 
          multiLines="1"
          wantReturn="1"
          colorText="#333333"
          font="size:14"
          margin-x="5"
          margin-y="5"/>

只读富文本显示

<richedit name="reDisplay" 
          pos="10,120,410,220" 
          readOnly="1"
          rich="1"
          text="只读富文本内容"/>

带滚动条的多行编辑框

<richedit name="reMultiline" 
          pos="10,230,410,380" 
          multiLines="1"
          wordWrap="1"
          vscrollBar="1"
          autoVscroll="1"
          hscrollBar="1"
          autoHscroll="1"
          cueText="请输入多行文本..."/>

密码输入框

<richedit name="rePassword" 
          pos="10,390,200,420" 
          password="1"
          passwordChar="*"
          cueText="请输入密码"/>

RTF内容显示

<richedit name="reRTF" 
          pos="10,430,410,530" 
          readOnly="1"
          rich="1"
          rtf="rtf:help_document"/>

事件处理

富文本编辑框控件支持以下事件:

事件名 EventID 说明
EVT_RE_NOTIFY EventRENotify::EventID 富文本编辑通知事件
EN_CHANGE - 内容改变事件
EN_UPDATE - 内容更新事件
EN_SETFOCUS - 获得焦点事件
EN_KILLFOCUS - 失去焦点事件
// 事件处理示例
EVENT_MAP_BEGIN()
    EVENT_NAME_HANDLER(L"reBasic", EventRENotify::EventID, OnRENotify)
EVENT_MAP_END()

void OnRENotify(IEvtArgs *pEvt)
{
    EventRENotify *pRealEvt = sobj_cast<EventRENotify>(pEvt);
    // 处理富文本编辑事件
    switch(pRealEvt->iNotify)
    {
    case EN_CHANGE:
        // 内容改变
        break;
    case EN_SETFOCUS:
        // 获得焦点
        break;
    }
}

代码操作

// 查找富文本编辑框控件
SRichEdit *pRichEdit = FindChildByName2<SRichEdit>(L"reBasic");

// 设置文本
pRichEdit->SetWindowText(L"新文本");

// 获取文本
SStringT strText = pRichEdit->GetWindowText();

// 设置只读状态
pRichEdit->SetReadOnly(TRUE);

// 加载RTF内容
pRichEdit->LoadRtfFile(L"rtf:help_document");

// 获取选择文本
pRichEdit->GetSelText(strText);

最佳实践

  1. 合理配置滚动条:根据内容长度设置合适的 vscrollBarhscrollBar 属性
  2. 输入验证:使用 number 等属性限制输入类型
  3. 用户体验:为密码框设置 passwordChar 属性保护用户隐私
  4. 自动选择:在适当场景下使用 autoSel 提升用户体验
  5. 内容提示:通过 cueText 属性提供清晰的输入提示

常见问题

Q: 多行编辑框无法滚动怎么办?

A: 确保设置了 vscrollBarautoVscroll 属性。

Q: 富文本格式无法显示怎么办?

A: 检查是否设置了 rich 属性为 1。

Q: 密码框显示明文怎么办?

A: 检查是否设置了 passwordpasswordChar 属性。

相关控件