duilib控件與屬性說明

轉自 https://blog.csdn.net/thanklife/article/details/80091826

不知不覺中使用Duilib已經兩週有餘了,在使用過程當中遇到了不少問題,最終本身也有了一丟丟的認識看法,特記錄並分享。javascript

控件

控件及繼承關係圖php

【Window】窗口html

【Control】除Window外其餘全部組件都繼承自Controljava

佈局:git

【Container】github

【VerticalLayout】垂直佈局:其內元素按照豎直方式排列瀏覽器

【HorizontalLayout】水平佈局:其內元素按照水平方式排列bash

【TileLayout】平鋪佈局:例如360工具箱。屬性columns[列數,如(4)]與itemsize[子項固定大小,如(128,128)]不能同時使用。ide

【TabLayout】標籤頁,配合Option使用。函數

【ChildLayout】

功能控件:

【ActiveX】

【WebBrowser】瀏覽器

【Label】標籤

【Button】按鈕

【Text】文本

【Progress】--【Slider】進度條--進度條按鈕

【Edit】輸入框,支持showhtml。

【RichEdit】輸入框,不支持showhtml。支持多行,直接打回車便可。

【Option】標籤

【ScrollBar】滾動條

【GifAnimGif】動畫

【Combo】下拉框

【List--ListHeader】--【ListHeaderItem】--【ListLabelElement】--【ListTextElement】--【ListContainerElement】列表

【TreeView】--【TreeNode】樹

屬性

詳細屬性查看官方Duilib文件夾內的"屬性列表.xml"。

【name】控件名字,同一窗口內必須惟一。建議命名格式"控件類型名稱",例如:"btnClose";"optionMain"。

【bkcolor】背景顏色。如:0xFFFFFFFF。通常的顏色RGB表示都是六位,這裏的八位中的前兩位爲FF,後面六位與普通RGB顏色編碼相同。RGB顏色參考

【bkcolor2】背景漸變色2,和bkcolor配合使用,如(0xFFFFFF00)。

【bkcolor3】背景漸變色3,和bkcolor、bkcolor2配合使用,如(0xFFFF00FF)"。

【bkimage】背景圖片,如(bk.bmp或file='aaa.jpg' res='' restype='0' dest='0,0,0,0' source='0,0,0,0' corner='0,0,0,0' mask='#FF0000' fade='255' hole='false' xtiled='false' ytiled='false')。

【width】控件預設的寬度,如(100)。

【height】控件預設的高度,如(30)。

【text】顯示文本,如(測試文本)。

【tooltip】鼠標懸浮提示,如(請在這裏輸入你的密碼)。

【enabled】是否能夠響應用戶操做,如(true)。

【mouse】本控件是否能夠響應鼠標操做,如(true)。

【mousechild】本控件的子控件是否能夠響應用戶操做,如(true)。

【visible】是否可見,如(true)。

【menu】是否須要右鍵菜單。

【float】【pos】是否採用絕對定位以及位置(左上右下)。如float="true" pos="10,10,10,10"。只有float爲true時,pos纔有效,只有float爲true,無pos時內容不顯示。更多用法見補充的佈局技巧

【padding】外邊距,如(2,2,2,2)。邊距不屬於此控件。

【inset】容器的內邊距,如(2,2,2,2)。邊距屬於此控件。

【bordercolor】邊框顏色,如(0xFF000000)。

【focusbordercolor】得到焦點時邊框的顏色,如(0xFFFF0000)。

【align】文字對齊方式。在option中能夠取left、right、center、top、button,如(center)。

【textpadding】文字顯示的邊距,如(2,2,2,2)。

【endellipsis】句末顯示不全是否使用...代替,如(true)"/>

【showhtml】是否使用類html富文本繪製,如(false)。

【vscrollbar】是否使用豎向滾動條,如(true)。只有內容超出容器後纔會顯示滾動條。
本身須要先定義vscrollbar滾動條

    
    
    
    
  1. <Default name="VScrollBar" value="" />
<Default name="VScrollBar" value="" />

【hscrollbar】是否使用橫向滾動條,如(true)。只有內容超出容器後纔會顯示滾動條。

    
    
    
    
  1. <Default name="HScrollBar" value="" />
<Default name="HScrollBar" value="" />

【bkimage】【normalimage】【hotimage】【focusedimage】【pushedimage】【selectedimage】【disabledimage】
【textcolor】【hottextcolor】【focusedtextcolor】【pushedtextcolor】【selectedtextcolor】【disabledtextcolor】
普通狀態--鼠標懸浮狀態--得到焦點時狀態--鼠標按下狀態--選中狀態--禁用狀態
【foreimage】option,process,slider具備的前景圖片。

實例

時間控件

    
    
    
    
<DateTime name="DateTimeDemo1" bkcolor="#FFE2E5EA"/>

按鈕

    
    
    
    
<Button name="closebtn" tooltip="關閉" float="true" pos="44,5,74,24" width="28" normalimage=" file='SysBtn\CloseNormal.bmp' " hotimage=" file='SysBtn\CloseFocus.bmp' " pushedimage=" file='SysBtn\CloseFocus.bmp' "/>

進度條

    
    
    
    
  1. <Progress name="ProgressDemo1" text="Progress" width="100" height="20" foreimage="Progress/progress_fore.png" min="0" max="100" value="50" hor="true" align="center"/>
  2. <Slider name="SliderDemo1" width="100" height="18" thumbsize="12,20" bkimage="file='Slider/slider_fore.bmp' mask='0xffff00ff'" thumbimage="file='Slider/SliderBar.png' mask='0xffffffff'"/>

下拉框

    
    
    
    
  1. <Combo name="ComboDemo1" width="100" height="30" normalimage="file='ComboBox/Combo_nor.bmp'" hotimage="file='ComboBox/Combo_over.bmp' " pushedimage="file='ComboBox/Combo_over.bmp' ">
  2. <ListLabelElement text="zdy" selected="true"/>
  3. <ListLabelElement text="zzz"/>
  4. </Combo>

編輯框

    
    
    
    
<Edit text="薑糖不語" font="2" textcolor="#FF00a0e9" bordersize="2" bordercolor="#FF000000" focusbordercolor="#FFFF0000"/>

圖片

    
    
    
    
<Container width="30" height="30" bkimage="file='SysBtn\setting_o.png' dest='10,5,30,25'"/>

橫線

    
    
    
    
<Control height="1" bkcolor="#FF000000"/>

導航欄與標籤頁

    
    
    
    
  1. //須要配合邏輯代碼實現
  2. <Option name="OptionDemo1" text="1" bkcolor="#FFC5D4F2" group="tabDemo" selected="true"/>
  3. <Option name="OptionDemo2" text="1" bkcolor="#FFFFDC78" group="tabDemo"/>
  4. <Option name="OptionDemo3" text="1" bkcolor="#FFBECEA1" group="tabDemo"/>
  5. <TabLayout name="tabTest" bkcolor="#FFB2D1E3">
  6. <HorizontalLayout>
  7. <Label text="Option1" bkcolor="#FFC5D4F2" align="center" />
  8. </HorizontalLayout>
  9. <HorizontalLayout>
  10. <Text text="Option2" bkcolor="#FFFFDC78" align="center" />
  11. </HorizontalLayout>
  12. <HorizontalLayout>
  13. <Button text="Option3" bkcolor="#FFBECEA1" align="center" />
  14. </HorizontalLayout>
  15. </TabLayout>

注意那個按鈕的實現。

    
    
    
    
  1. <Default name="TreeNode" value="width="9999""/>
  2. <TreeView vscrollbar="true" >
  3. <TreeNode height="24" folderattr="normalimage="file='SysBtn/list_icon_e.png' dest='6,6,18,18'" selectedimage="file='SysBtn/list_icon_f.png' dest='6,6,18,18'"" >
  4. <Text text="錯誤項" textcolor="#FFFF0000" font="3" float="true" pos="20,0,9999,9999" />
  5. <TreeNode text="xxx" >
  6. <Button text="修復" textcolor="#FF1ddb6e" width="50"/>
  7. </TreeNode>
  8. </TreeNode>
  9. </TreeView>

控件響應消息類型

原連接

消息 說明 Sender
click 鼠標點擊 CButtonUI
dropdown 下拉顯示 CComboUI
headerclick 點擊列標題 CListHeaderItemUI
itemactivate   CListElementUI、CListContainerElementUI
itemclick 單擊選項 CListLabelElementUI、CListContainerElementUI
itemselect 選擇選項 CComboUI、CListUI
killfocus 失去焦點 CControlUI
link   CTextUI、CListTextElementUI
menu   CButtonUI、CControlUI
return 回車 CEditWnd、CRichEditUI
scroll 滾動 CScrollBarUI
selectchanged 變動選項 COptionUI
setfocus 得到焦點 CControlUI
showactivex   CActiveXUI
textchanged 文本被改變 CEditWnd
tabselect 標籤頁被選中 CTabLayoutUI
timer   CControlUI
valuechanged 值發生變化 CSliderUI
windowinit 窗體初始化

部分方法實現

    
    
    
    
  1. SetText();
  2. SetColor();

控件建立

增長控件:pNode->Add(pText);
TreeNode增長TreeNode:pNode->AddAt(pNodeLeaf, 0);

標籤頁

相關xml:

    
    
    
    
  1. <!--導航欄-->
  2. <HorizontalLayout height="113" bkcolor="#FF267FFC" bkcolor2="#FF0782FB">
  3. <HorizontalLayout width="920">
  4. <Option name="AdSafe1" text="淨網首頁" align="right" textcolor="#FFFFFFFF" font="1" textpadding="0,0,20,0" normalimage="file='Option\Option1m.png' dest='36,36,76,76'" hotimage="file='Option\Option1w.png' dest='36,36,76,76'" pushedimage="file='Option\Option1w.png' dest='36,36,76,76'" selectedimage="file='Option\Option1w.png' dest='36,36,76,76'" group="AdSafeOption" selected="true"/>
  5. <Option name="AdSafe2" text="深度優化" align="right" textcolor="#FFFFFFFF" font="1" textpadding="0,0,20,0" normalimage="file='Option\Option2m.png' dest='36,36,76,76'" hotimage="file='Option\Option2w.png' dest='36,36,76,76'" pushedimage="file='Option\Option2w.png' dest='36,36,76,76'" selectedimage="file='Option\Option2w.png' dest='36,36,76,76'" group="AdSafeOption" />
  6. <Option name="AdSafe3" text="彈窗抓取" align="right" textcolor="#FFFFFFFF" font="1" textpadding="0,0,20,0" normalimage="file='Option\Option3m.png' dest='36,36,76,76'" hotimage="file='Option\Option3w.png' dest='36,36,76,76'" pushedimage="file='Option\Option3w.png' dest='36,36,76,76'" selectedimage="file='Option\Option3w.png' dest='36,36,76,76'" group="AdSafeOption" />
  7. <Option name="AdSafe4" text="攔截記錄" align="right" textcolor="#FFFFFFFF" font="1" textpadding="0,0,20,0" normalimage="file='Option\Option4m.png' dest='36,36,76,76'" hotimage="file='Option\Option4w.png' dest='36,36,76,76'" pushedimage="file='Option\Option4w.png' dest='36,36,76,76'" selectedimage="file='Option\Option4w.png' dest='36,36,76,76'" group="AdSafeOption" />
  8. <Option name="AdSafe5" text="移動淨網版" align="right" textcolor="#FFFFFFFF" font="1" textpadding="0,0,20,0" normalimage="file='Option\Option5m.png' dest='32,36,62,76'" hotimage="file='Option\Option5w.png' dest='32,36,62,76'" pushedimage="file='Option\Option5w.png' dest='32,36,62,76'" selectedimage="file='Option\Option5w.png' dest='32,36,62,76'" group="AdSafeOption" />
  9. </HorizontalLayout>
  10. <HorizontalLayout />
  11. </HorizontalLayout>
  12. <!--窗體-->
  13. <TabLayout name="AdSafeContent" bkcolor="#FFFFFFFF">
  14. <!-- <ChildLayout xmlfile="PageOne.xml" />
  15. <ChildLayout xmlfile="PageTwo.xml" />
  16. <ChildLayout xmlfile="PageThree.xml" />
  17. <ChildLayout xmlfile="PageFour.xml" />
  18. <ChildLayout xmlfile="PageFive.xml" /> -->
  19. <Include source="PageOne.xml" />
  20. <Include source="PageTwo.xml" />
  21. <Include source="PageThree.xml" />
  22. <Include source="PageFour.xml" />
  23. <Include source="PageFive.xml" />
  24. </TabLayout>

相關代碼片斷:

    
    
    
    
  1. if (msg.sType == _T("selectchanged"))
  2. {
  3. CDuiString strName = msg.pSender->GetName();
  4. CTabLayoutUI* pControl = static_cast<CTabLayoutUI*>(m_PaintManager.FindControl(_T("AdSafeContent")));
  5. if (strName == _T("AdSafe1"))
  6. {
  7. pControl->SelectItem(0);
  8. }
  9. if (strName == _T("AdSafe2"))
  10. {
  11. pControl->SelectItem(1);
  12. }
  13. if (strName == _T("AdSafe3"))
  14. {
  15. pControl->SelectItem(2);
  16. }
  17. if (strName == _T("AdSafe4"))
  18. {
  19. pControl->SelectItem(3);
  20. }
  21. if (strName == _T("AdSafe5"))
  22. {
  23. pControl->SelectItem(4);
  24. }
  25. }
if (msg.sType == _T("selectchanged")){CDuiString strName = msg.pSender->GetName();CTabLayoutUI* pControl = static_cast<CTabLayoutUI*>(m_PaintManager.FindControl(_T("AdSafeContent")));if (strName == _T("AdSafe1")){pControl->SelectItem(0);}if (strName == _T("AdSafe2")){pControl->SelectItem(1);}if (strName == _T("AdSafe3")){pControl->SelectItem(2);}if (strName == _T("AdSafe4")){pControl->SelectItem(3);}if (strName == _T("AdSafe5")){pControl->SelectItem(4);}}

Combo

相關xml:

    
    
    
    
  1. <Combo name="comboUpdateFrequency" width="60" height="30" normalimage="file='ComboBox/Combo_nor.bmp' corner='5,2,30,2'" hotimage="file='ComboBox/Combo_over.bmp' corner='5,2,30,2'" pushedimage="file='ComboBox/Combo_over.bmp' corner='5,2,30,2'" >
  2. <ListLabelElement name="listDay" text=" 天天" selected="true"/>
  3. <ListLabelElement name="listWeek" text=" 每週" />
  4. <ListLabelElement name="listMonth" text=" 每個月" />
  5. </Combo>

相關代碼片斷:

    
    
    
    
  1. void SetUpdateFrequency(int iUpdateFrequency)
  2. {
  3. CComboUI* pComboUpdateFrequency = static_cast<CComboUI*>(m_PaintManager.FindControl(_T("comboUpdateFrequency")));
  4. pComboUpdateFrequency->SetInternVisible(true);
  5. if (iUpdateFrequency == 0)
  6. {
  7. pComboUpdateFrequency->SelectItem(0);
  8. }
  9. else if (iUpdateFrequency == 1)
  10. {
  11. pComboUpdateFrequency->SelectItem(1);
  12. }
  13. else if (iUpdateFrequency == 2)
  14. {
  15. pComboUpdateFrequency->SelectItem(2);
  16. }
  17. }
void SetUpdateFrequency(int iUpdateFrequency){CComboUI* pComboUpdateFrequency = static_cast<CComboUI*>(m_PaintManager.FindControl(_T("comboUpdateFrequency")));pComboUpdateFrequency->SetInternVisible(true);if (iUpdateFrequency == 0){pComboUpdateFrequency->SelectItem(0);}else if (iUpdateFrequency == 1){pComboUpdateFrequency->SelectItem(1);}else if (iUpdateFrequency == 2){pComboUpdateFrequency->SelectItem(2);}}

補充

陰影實現

在WinMain入口函數內添加

    
    
    
    
  1. CWndShadow::Initialize(hInstance);
CWndShadow::Initialize(hInstance);

在窗口類內定義一個成員變量

    
    
    
    
CWndShadow m_WndShadow;

在初始化窗口InitWindow函數內部進行實現

    
    
    
    
  1. m_WndShadow.Create(m_hWnd);
  2. m_WndShadow.SetSize(3);
  3. m_WndShadow.SetDarkness(200);
  4. //m_WndShadow.SetColor(0x0AFF00);
  5. m_WndShadow.SetPosition(0, 0);
m_WndShadow.Create(m_hWnd);m_WndShadow.SetSize(3);m_WndShadow.SetDarkness(200);//m_WndShadow.SetColor(0x0AFF00);m_WndShadow.SetPosition(0, 0);

關於【ChildLayout】

做用就是從一個xml文件中加載佈局來嵌入到ChildLayout佈局所在的地方,使用這個佈局通常只須要指定xmlfile屬性來設置xml文件位置就能夠了。他的意義在於能夠把繁雜的大量xml代碼分隔開。好比他和TabLayout佈局結合,讓TabLayout佈局包含5個ChildLayout佈局,而每一個ChildLayout佈局分別從5個xml文件加載本身的佈局文件,這樣就能夠分塊化的編寫佈局代碼。

實際上有個比他更好用的標籤,就是Include標籤,Include不屬於佈局,但他的做用在佈局方面很是相似ChildLayout,指定他的Source屬性到某個xml文件就能夠了。相對ChildLayout,Include的優勢是能夠自動識別自定義控件,而ChildLayout不能夠!

佈局技巧

1.通常是垂直佈局,水平佈局嵌套使用。

2.使用【inset】屬性和【padding】。【inset】的邊框依舊屬於控件內部,而【padding】的邊框不屬於控件。

3.使用float。
float="0.5,0.5,0.5,0.5" pos="-10,-10,10,10"。在上一層控件的正中間。距離中間點左上右下都是10的矩形。

float="0.1,0.3,0.2,0.4" pos="0,0,0,0"。佔據上一層控件的水平0.1-0.2,垂直0.3-0.4的區域。

關於在邏輯代碼中SetPos無效的問題。

1.設置控件的Pos的時候控件必須已經Add到了某個Contain中,不然SetPos無效

2.若是必定想在以前設置的話,那麼請使用SetAttribute ,轉Duilib ApplyAttributeList和SetPos陷阱

Combo不顯示值的問題

須要pCombo->SetInternVisible(true); 具體實例見上面的combo實例

RECT類型示例

    
    
    
    
  1. RECT rc;
  2. rc.left = 20;
  3. rc.top = 0;
  4. rc.right = 9999;
  5. rc.bottom = 9999;
  6. CTextUI* pText = new CTextUI;
  7. pText->SetFloat(true);
  8. pText->SetPos(rc);
RECT rc;rc.left = 20;rc.top = 0;rc.right = 9999;rc.bottom = 9999;CTextUI* pText = new CTextUI;pText->SetFloat(true);pText->SetPos(rc);

TreeNode的bug

若是直接在TreeNode設置文字,那麼textcolor屬性無效,須要使用

    
    
    
    
itemattr="textcolor="#FFFF0000" font="3""

可是此屬性在鼠標懸浮後失效(若是是黑色的無影響),目前爲深刻研究解決方案。因此推薦採用TreeNode嵌套Text實現。

不顯示邊框陰影

通過我的使用發現:window必須roundcorner屬性才能實現陰影。

    
    
    
    
  1. <Window size="920,600" mininfo="920,600" roundcorner="5,5" caption="0,0,0,30" sizebox="4,4,4,4" >
  2. </Window>
<Window size="920,600" mininfo="920,600" roundcorner="5,5" caption="0,0,0,30" sizebox="4,4,4,4" ></Window>

關於ini文件的讀寫以及一個坑

相對路徑設置以下將跟cpp文件在同一目錄

    
    
    
    
  1. #define inisrc TEXT(".//config.ini")
#define inisrc TEXT(".//config.ini")

使用方式

    
    
    
    
  1. int iCloseStatus = GetPrivateProfileInt(TEXT("CloseStatus"), TEXT("status"), 1, inisrc);
  2. int iUpdateFrequency = GetPrivateProfileInt(TEXT("Update"), TEXT("frequency"), 1, inisrc);
  3. WritePrivateProfileString(TEXT("CloseStatus"), TEXT("status"), _T("0"), inisrc);
  4. WritePrivateProfileString(TEXT("Update"), TEXT("frequency"), _T("0"), inisrc);
int iCloseStatus = GetPrivateProfileInt(TEXT("CloseStatus"), TEXT("status"), 1, inisrc);int iUpdateFrequency = GetPrivateProfileInt(TEXT("Update"), TEXT("frequency"), 1, inisrc);WritePrivateProfileString(TEXT("CloseStatus"), TEXT("status"), _T("0"), inisrc);WritePrivateProfileString(TEXT("Update"), TEXT("frequency"), _T("0"), inisrc);

相關文件【config.ini】內容

    
    
    
    
  1. [ CloseStatus]
  2. status= 0
  3. [ Update]
  4. frequency= 0

實現效果圖

Demo源碼

做者:靜候那一米陽光 連接:https://www.jianshu.com/p/4474f7a9b8b6 來源:簡書 著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。
相關文章
相關標籤/搜索