上個教程說到了TreeView的文字不能垂直居中的問題,而我們用LabelUI其實是可以垂直居中的,為什么不說是TreeView的bug,而說是Label控件的bug呢?因為影響TreeView垂直居中的就是Label,可以發現LabelUI的【屬性列表.XML】里有valign屬性,而代碼里卻找不到,是因為valign屬性被合並到align屬性里去了,只要設置align="center"就可以水平垂直都居中,但是想要垂直居中,水平左對齊啥的,就犯難了,因此這里需要將兩個屬性分開,valign管垂直,align管水平,這樣想要怎么組合都OK啦。
將CLabelUI::SetAttribute函數里if( _tcscmp(pstrName, _T("align")) == 0 ) 那一段代碼改成下面這樣即可。(記得重新編譯duilib哦~)
if( _tcscmp(pstrName, _T("align")) == 0 ) { if( _tcsstr(pstrValue, _T("left")) != NULL ) { m_uTextStyle &= ~(DT_CENTER | DT_RIGHT | DT_SINGLELINE); m_uTextStyle |= DT_LEFT; } if( _tcsstr(pstrValue, _T("center")) != NULL ) { m_uTextStyle &= ~(DT_LEFT | DT_RIGHT ); m_uTextStyle |= DT_CENTER; } if( _tcsstr(pstrValue, _T("right")) != NULL ) { m_uTextStyle &= ~(DT_LEFT | DT_CENTER | DT_SINGLELINE); m_uTextStyle |= DT_RIGHT; } } else if( _tcscmp(pstrName, _T("valign")) == 0 ) { if( _tcsstr(pstrValue, _T("top")) != NULL ) { m_uTextStyle &= ~(DT_BOTTOM | DT_VCENTER); m_uTextStyle |= (DT_TOP | DT_SINGLELINE); } if( _tcsstr(pstrValue, _T("vcenter")) != NULL ) { m_uTextStyle &= ~(DT_TOP | DT_BOTTOM ); m_uTextStyle |= (DT_VCENTER | DT_SINGLELINE); } if( _tcsstr(pstrValue, _T("bottom")) != NULL ) { m_uTextStyle &= ~(DT_TOP | DT_VCENTER); m_uTextStyle |= (DT_BOTTOM | DT_SINGLELINE); } }
嗯,實現了垂直居中的效果后,現在貌似和迅雷一模一樣啦?
NO,NO,NO,下一節將會繼續介紹~O(∩_∩)O~