SOUI官方论坛

 找回密码
 立即注册
查看: 1814|回复: 3

使用SWindow::GetAttribute

[复制链接]
  • TA的每日心情
    奋斗
    2022-7-4 09:45
  • 签到天数: 28 天

    [LV.4]偶尔看看III

    59

    主题

    588

    帖子

    5481

    积分

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    5481

    突出贡献优秀版主

    发表于 2020-1-8 10:20:17 | 显示全部楼层 |阅读模式
    本帖最后由 setoutsoft 于 2020-1-8 10:22 编辑

    SOUI可以使用SetAttribute来改变窗口的属性,经常有人问起有没有GetAttribute。
    3.0.0.23前,SWindow::GetAttribute是protected的,继承的子类可以自己实现GetAttribute,不过目前看来用处不大。
    新版本将GetAttribute变成了public,并且调整了IAttrStorageFactory这个接口。用户可以通过给SApplication::SetAttrStrorageFactory来确实如何存储窗口的属性信息,以便后面使用它们。
    要使用3.0.0.23的GetAttribute,需要以下步骤:
    首先,自己实现一个IAttrStorageFactory,例如:

    1. class CAttrStorage: public TObjRefImpl<IAttrStorage>
    2. {
    3.         typedef SMap<SStringW,SStringW> ATTRMAP;
    4.         ATTRMAP m_mapAttr;

    5. public:

    6.         virtual void OnSetAttribute(const SStringW & strName, const SStringW & strValue,bool bHandled)
    7.         {
    8.                 m_mapAttr[strName]=strValue;
    9.         }

    10.         virtual SStringW OnGetAttribute(const SStringW & strName) const
    11.         {
    12.                 const ATTRMAP::CPair *p = m_mapAttr.Lookup(strName);
    13.                 if(p)
    14.                         return p->m_value;
    15.                 else
    16.                         return SStringW();
    17.         }
    18. };


    19. class CAttrStorageFactory : public TObjRefImpl<IAttrStorageFactory>
    20. {
    21. public:
    22.         virtual HRESULT CreateAttrStorage(IAttrStorage** ppAttrStorage) const
    23.         {
    24.                 *ppAttrStorage = new CAttrStorage;
    25.                 return S_OK;
    26.         }
    27. };
    复制代码
    上面的Factory使用一个简单的SMap来保存窗口的属性。
    如果只需要保留自定义的属性,可以在virtual void OnSetAttribute(const SStringW & strName, const SStringW & strValue,bool bHandled)这个方法中只保存bHandled为TRUE的属性。

    然后,在SApplication创建以后,调用以下代码:
    1.   CAttrStorageFactory * attrFac = new CAttrStorageFactory;
    2.                 m_theApp->SetAttrStorageFactory(attrFac);
    3.                 attrFac->Release();
    复制代码
    这样处理以后,系统会为每一个SWindow对象创建一个CAttrStorage对象用来保存属性表。
    在代码中即可使用SWindow::GetAttribute来获取属性。

    下面是demo中的布局XML,我给edit_output这个richedit指定了一个user_attr属性。
    1. <SOUI name="mainWindow" title="@string/title" bigIcon="ICON_LOGO:32" smallIcon="ICON_LOGO:16" margin="5,5,5,5"  resizable="1" wndType="appMain"
    2. appWnd="1"
    3. translucent="1"
    4. >
    5.   <root skin="_skin.sys.wnd.bkgnd" cache="1"  width="600" height="400" >
    6.     <caption pos="0,0,-0,30" show="1" font="adding:0">
    7.       <icon pos="10,8" src="ICON_LOGO:16"/>
    8.       <text pos="29,9">@string/title</text>
    9.       <imgbtn name="btn_close" skin="_skin.sys.btn.close"    pos="-45,0" tip="close" animate="1"/>
    10.       <imgbtn name="btn_max" skin="_skin.sys.btn.maximize"  pos="-83,0" animate="1" />
    11.       <imgbtn name="btn_restore" skin="_skin.sys.btn.restore"  pos="-83,0" show="0" animate="1" />
    12.       <imgbtn name="btn_min" skin="_skin.sys.btn.minimize" pos="-121,0" animate="1" />
    13.     </caption>
    14.     <window pos="5,[2,-5,-5" layout="vbox" interval="5">
    15.       <window layout="hbox" size="-2,-1" interval="5">
    16.         <edit name="edit_input" size="0,30" text="user_attr" weight="1"/>
    17.         <button name="btn_get_attr" text="GetAttribute" size="100,30"/>
    18.       </window>
    19.       <richedit name="edit_output" size="-2,0" readOnly="1" multilines="1" weight="1" user_attr="hello, I'm user defined attribute"/>
    20.     </window>
    21.   </root>
    22. </SOUI>
    复制代码
    然后我在btn_get_attr这个按钮的响应事件中使用如下方法来获取这个属性:

    1. void CMainDlg::OnGetAttr()
    2. {
    3.         SWindow *pInput =FindChildByID(R.id.edit_input);
    4.         SWindow *pOutput = FindChildByID(R.id.edit_output);
    5.         SStringW strAttr = pInput->GetWindowText();
    6.         SStringW strValue = pOutput->GetAttribute(strAttr);
    7.         if(strValue.IsEmpty()) strValue=L"undefined attribute";
    8.         pOutput->SetWindowText(strValue);
    9. }
    复制代码


    结果如下图:
    getAttrDemo.png

    代码见附件。
    GetAttrDemo.zip (34.16 KB, 下载次数: 5)


  • TA的每日心情
    开心
    2023-2-3 10:28
  • 签到天数: 29 天

    [LV.4]偶尔看看III

    9

    主题

    33

    帖子

    760

    积分

    04:00化神期

    Rank: 4

    积分
    760
    发表于 2020-1-8 10:31:44 | 显示全部楼层
    66666666666666666
  • TA的每日心情
    开心
    6 天前
  • 签到天数: 942 天

    [LV.10]以坛为家III

    580

    主题

    1340

    帖子

    2万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    28797
    发表于 2020-1-8 10:31:53 | 显示全部楼层
    老大出手就是不同,适合调整-恢复属性的场景

    该用户从未签到

    1

    主题

    35

    帖子

    43

    积分

    22:00筑基期

    Rank: 1

    积分
    43
    发表于 2020-4-16 18:33:47 | 显示全部楼层
    谢谢分享,看看
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|SOUI官方论坛

    GMT+8, 2024-5-4 22:39

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表