Skip to content

SWindow 控件基类

Warning

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

You can read it through google translate.

SWindow 是 SOUI 控件系统的基础,绝大多数控件都继承自该类。它提供了控件的基本属性和行为。

类信息

  • 类名SWindow
  • 控件标签window
  • 基类
  • SObject
  • SMsgHandleState
  • TObjRefImpl2<IObjRef,SWindow>

基本属性

标识属性

属性名 类型 默认值 说明
id 字符串 - Object ID
name 字符串 - Object Name

外观属性

属性名 类型 默认值 说明
skin 字符串 - 皮肤
ncskin 字符串 - 边框皮肤
class 字符串 - 样式
alpha [0-255] - 透明度,数值在0-255之间
colorBkgnd color - 背景颜色,不指定skin时有效
colorBorder color - 边框颜色,需要和margin一起使用

状态属性

属性名 类型 默认值 说明
enable bool 1 是否可用(是:1
visible/show bool 1 是否可见(是:1
display bool 1 隐藏占位(是:1
cache bool 0 画布缓存(是:1
msgTransparent bool 0 消息穿透(是:1
focusable bool 1 焦点(是:1
clipClient bool 0 裁剪客户区(是:1
blendBackground bool 1 背景融合(是:1

布局属性

属性名 类型 默认值 说明
pos 字符串 - 位置,“
offset 字符串 - 偏移,根据pos确定位置后再做偏移,单位为控件大小,格式为水平偏移系数,垂直偏移系数
size 字符串 - 尺寸,-1 窗口自适应内容大小,-2 使用父窗口的宽/高
margin 字符串 - 边框大小,和colorBorder或者ncSkin配合使用,格式为左,上,右,下
padding 字符串 - 内边距,和inset属性相同,格式为左,上,右,下
maxWidth 数字 - 最大宽度,自动计算大小时,窗口的最大宽度

交互属性

属性名 类型 默认值 说明
tip 字符串 - 提示
data 字符串 - 用户数据
cursor 字符串 - 光标,可选值:arrow,ibeam,wait,cross,uparrow,size,sizenwse,sizenesw,sizewe,sizens,sizeall,no,hand,help
float bool 0 float(是:1
layout 字符串 - layout,可选值:锚点布局:soui,Grid布局:gridLayout,线性水平:hbox,线性垂直:vbox
gravity 字符串 - 子控件的对齐方式,可选值:左:left,上:top,中:center,右:right,下:bottom
layout_gravity 字符串 - 相对于父控件的对齐方式,可选值:左:left,上:top,中:center,右:right,下:bottom

文本属性

属性名 类型 默认值 说明
text 字符串 - text value
align 字符串 - 水平对齐,可选值:left,center,right
valign 字符串 - 垂直对齐,可选值:top,middle,bottom
font 字符串 - 文本字体
fontHover 字符串 - Hover字体
fontPush 字符串 - Push字体
fontDisable 字符串 - Disable字体
colorText color - 文本颜色
colorTextHover color - Hover颜色
colorTextPush color - Push颜色
colorTextDisable color - Disable颜色
dotted bool 0 省略号显示文本(是:1
drawFocusRect bool 0 焦点虚框(是:1

使用示例

基本窗口

<window pos="10,10,200,100" 
        name="basic_window"
        skin="skin_window"
        text="基本窗口"/>

带文本和样式的窗口

<window pos="10,110,200,200" 
        name="styled_window"
        skin="skin_styled_window"
        text="样式窗口"
        colorText="#333333"
        font="size:14,bold:1"
        align="center"
        valign="middle"/>

带边框和透明度的窗口

<window pos="10,210,200,300" 
        name="border_window"
        ncskin="skin_border"
        margin="2,2,2,2"
        colorBorder="#CCCCCC"
        alpha="200"
        text="带边框窗口"/>

布局容器窗口

<window pos="220,10,410,200" 
        name="layout_window"
        layout="hbox"
        gravity="center">
    <button text="按钮1" size="80,30"/>
    <window weight="1"/> <!-- 占位窗口 -->
    <button text="按钮2" size="80,30"/>
</window>

事件处理

作为控件基类,SWindow支持以下基本事件:

事件名 EventID 说明
EVT_MOUSE_BEGIN EventMouse::EventID 鼠标事件开始
EVT_MOUSE_END EventMouse::EventID 鼠标事件结束
EVT_KEY_BEGIN EventKey::EventID 键盘事件开始
EVT_KEY_END EventKey::EventID 键盘事件结束
EVT_SETFOCUS EventSetFocus::EventID 获得焦点事件
EVT_KILLFOCUS EventKillFocus::EventID 失去焦点事件
// 事件处理示例
EVENT_MAP_BEGIN()
    EVENT_NAME_HANDLER(L"basic_window", EventMouse::EventID, OnMouseEvent)
EVENT_MAP_END()

void OnMouseEvent(IEvtArgs *pEvt)
{
    // 处理鼠标事件
}

代码操作

// 查找窗口控件
SWindow *pWindow = FindChildByName2<SWindow>(L"basic_window");

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

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

// 显示/隐藏控件
pWindow->ShowWindow(SW_SHOW);  // 显示
pWindow->ShowWindow(SW_HIDE);  // 隐藏

// 启用/禁用控件
pWindow->EnableWindow(FALSE);  // 禁用
pWindow->EnableWindow(TRUE);   // 启用

// 设置焦点
pWindow->SetFocus();

// 获取控件状态
BOOL bVisible = pWindow->IsVisible();
BOOL bEnabled = pWindow->IsWindowEnabled();

最佳实践

  1. 合理使用皮肤:通过 skinncskin 属性定制控件外观
  2. 布局管理:使用 layoutgravitylayout_gravity 属性实现灵活布局
  3. 状态控制:通过 enablevisibledisplay 属性控制控件状态
  4. 性能优化:适当使用 cache 属性提升渲染性能
  5. 用户体验:使用 tip 属性提供操作提示

常见问题

Q: 控件显示异常怎么办?

A: 检查 possize 属性是否设置正确,确保控件在父窗口范围内。

Q: 控件无法响应事件怎么办?

A: 确保 msgTransparent 属性设置为 0,并且控件处于启用状态。

Q: 文本显示不完整怎么办?

A: 检查 maxWidth 属性是否限制了控件宽度,或使用 dotted 属性启用省略号显示。

相关控件