Duilib实现类似电脑管家扫描目录效果

实现原理:

1.后台开线程遍历目录,遍历出一个文件路径在界面上更新显示(通过发消息通知主界面)

2.需要扩展一下Duilib控件,在此我扩展了CLabelUI,重写了PaintText函数

扩展控件的.h文件

 1 /*************************************************************************************************
 2 *
 3 * File Name    : UILabelEx.h
 4 *
 5 * Created      : 2016/11/11
 6 *
 7 * Author       : http://www.cnblogs.com/chechen/
 8 *
 9 * Model        : 
10 *
11 * Description  : [[CN]] 扩展Label控件 [[CN]]                                                  
12 *
13 **************************************************************************************************/
14 #ifndef _UILABELEX_H_
15 #define _UILABELEX_H_
16 
17 
18 class CLabelExUI : public CLabelUI
19 {
20 public:
21     CLabelExUI(void);
22     ~CLabelExUI(void);
23 
24     LPCTSTR GetClass() const;
25     LPVOID GetInterface(LPCTSTR pstrName);
26 
27     void PaintText(HDC hDC);  
28     void SetPos(RECT rc);  
29 
30 private:  
31     Color _MakeRGB(int a, Color cl);  
32     Color _MakeRGB(int r, int g, int b);  
33 };
34 #endif//_UILABELEX_H_

.cpp

  1 #include "StdAfx.h"
  2 #include "UILabelEx.h"
  3 
  4 
  5 CLabelExUI::CLabelExUI(void)
  6 {
  7 }
  8 
  9 
 10 CLabelExUI::~CLabelExUI(void)
 11 {
 12 }
 13 
 14 
 15 LPCTSTR CLabelExUI::GetClass() const
 16 {
 17     return TEXT("LabelExUI");
 18 }
 19 
 20 
 21 LPVOID CLabelExUI::GetInterface(LPCTSTR pstrName)
 22 {
 23     if( _tcscmp(pstrName, TEXT("LabelEx")) == 0 ) return static_cast<CLabelExUI*>(this);
 24     return CLabelUI::GetInterface(pstrName);
 25 }
 26 
 27 
 28 Color CLabelExUI::_MakeRGB(int a, Color cl)  
 29 {  
 30     return Color(a, cl.GetR(), cl.GetG(), cl.GetB());  
 31 }  
 32 
 33 
 34 Color CLabelExUI::_MakeRGB(int r, int g, int b)  
 35 {  
 36     return Color(255, r, g, b);  
 37 }  
 38 
 39 
 40 void CLabelExUI::SetPos(RECT rc)  
 41 {  
 42     __super::SetPos(rc);  
 43 }  
 44 
 45 
 46 void CLabelExUI::PaintText(HDC hDC)  
 47 {  
 48     if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();  
 49     if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();  
 50 
 51     RECT rc = m_rcItem;  
 52     rc.left += m_rcTextPadding.left;  
 53     rc.right -= m_rcTextPadding.right;  
 54     rc.top += m_rcTextPadding.top;  
 55     rc.bottom -= m_rcTextPadding.bottom;  
 56 
 57     if(!GetEnabledEffect())  
 58     {  
 59         if( m_sText.IsEmpty() ) return;  
 60         int nLinks = 0;  
 61         if( IsEnabled() ) {  
 62             if( m_bShowHtml )  
 63                 CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, m_dwTextColor,  
 64                 NULL, NULL, nLinks, DT_SINGLELINE | m_uTextStyle);  
 65             else 
 66             {
 67                  ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
 68                 ::SetBkMode(hDC, TRANSPARENT);
 69                 ::SetTextColor(hDC, RGB(GetBValue(m_dwTextColor), GetGValue(m_dwTextColor), GetRValue(m_dwTextColor)));
 70                 HFONT hOldFont = (HFONT)::SelectObject(hDC, m_pManager->GetFont(m_iFont));
 71                 
 72                 SIZE size;
 73                 ::GetTextExtentPoint32(hDC,m_sText.GetData(),m_sText.GetLength(),&size);
 74 
 75                 UINT uStyle = 0;  
 76                 if(size.cx > rc.right - rc.left)
 77                 {
 78                     uStyle = DT_PATH_ELLIPSIS|DT_WORD_ELLIPSIS|DT_END_ELLIPSIS;
 79                 }
 80                 else
 81                 {
 82                     uStyle = DT_CENTER | DT_VCENTER | DT_SINGLELINE;
 83                 }
 84                 ::DrawText(hDC, m_sText, -1, &rc, uStyle | DT_NOPREFIX);
 85                 ::SelectObject(hDC, hOldFont);
 86             }
 87         }  
 88         else {  
 89             if( m_bShowHtml )  
 90                 CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, m_dwDisabledTextColor,   
 91                 NULL, NULL, nLinks, DT_SINGLELINE | m_uTextStyle);  
 92             else  {
 93                  ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
 94                 ::SetBkMode(hDC, TRANSPARENT);
 95                 ::SetTextColor(hDC, RGB(GetBValue(m_dwTextColor), GetGValue(m_dwTextColor), GetRValue(m_dwTextColor)));
 96                 HFONT hOldFont = (HFONT)::SelectObject(hDC, m_pManager->GetFont(m_iFont));
 97                 
 98                 SIZE size;
 99                 ::GetTextExtentPoint32(hDC,m_sText.GetData(),m_sText.GetLength(),&size);
100 
101                 UINT uStyle = 0;  
102                 if(size.cx > rc.right - rc.left)
103                 {
104                     uStyle = DT_PATH_ELLIPSIS|DT_WORD_ELLIPSIS|DT_END_ELLIPSIS;
105                 }
106                 else
107                 {
108                     uStyle = DT_CENTER | DT_VCENTER | DT_SINGLELINE;
109                 }
110                 ::DrawText(hDC, m_sText, -1, &rc, uStyle | DT_NOPREFIX);
111                 ::SelectObject(hDC, hOldFont);
112 
113                 //CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, m_dwDisabledTextColor,   
114                 //m_iFont, /* DT_SINGLELINE | */m_uTextStyle); ///< @todo  
115             }
116                 
117         }  
118     }  
119     else  
120     {  
121         Font    nFont(hDC,m_pManager->GetFont(GetFont()));  
122 
123         Graphics nGraphics(hDC);  
124         nGraphics.SetTextRenderingHint(m_TextRenderingHintAntiAlias);  
125 
126         StringFormat format;  
127         format.SetAlignment((StringAlignment)m_hAlign);  
128         format.SetLineAlignment((StringAlignment)m_vAlign);  
129 
130         RectF nRc((float)rc.left,(float)rc.top,(float)rc.right-rc.left,(float)rc.bottom-rc.top);  
131         RectF nShadowRc = nRc;  
132         nShadowRc.X += m_ShadowOffset.X;  
133         nShadowRc.Y += m_ShadowOffset.Y;  
134 
135         int nGradientLength = GetGradientLength();  
136 
137         if(nGradientLength == 0)  
138             nGradientLength = (rc.bottom-rc.top);  
139 
140         LinearGradientBrush nLineGrBrushA(Point(GetGradientAngle(), 0),Point(0,nGradientLength),_MakeRGB(GetTransShadow(),GetTextShadowColorA()),_MakeRGB(GetTransShadow1(),GetTextShadowColorB() == -1?GetTextShadowColorA():GetTextShadowColorB()));  
141         LinearGradientBrush nLineGrBrushB(Point(GetGradientAngle(), 0),Point(0,nGradientLength),_MakeRGB(GetTransText(),GetTextColor()),_MakeRGB(GetTransText1(),GetTextColor1() == -1?GetTextColor():GetTextColor1()));  
142 
143         if(GetEnabledStroke() && GetStrokeColor() > 0)  
144         {  
145             LinearGradientBrush nLineGrBrushStroke(Point(GetGradientAngle(),0),Point(0,rc.bottom-rc.top+2),_MakeRGB(GetTransStroke(),GetStrokeColor()),_MakeRGB(GetTransStroke(),GetStrokeColor()));  
146 
147 #ifdef _UNICODE  
148             nRc.Offset(-1,0);  
149             nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);  
150             nRc.Offset(2,0);  
151             nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);  
152             nRc.Offset(-1,-1);  
153             nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);  
154             nRc.Offset(0,2);  
155             nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);  
156             nRc.Offset(0,-1);  
157 #else  
158             USES_CONVERSION;  
159             wstring mTextValue = A2W(m_TextValue.GetData());  
160 
161             nRc.Offset(-1,0);  
162             nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);  
163             nRc.Offset(2,0);  
164             nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);  
165             nRc.Offset(-1,-1);  
166             nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);  
167             nRc.Offset(0,2);  
168             nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);  
169             nRc.Offset(0,-1);  
170 #endif  
171 
172         }  
173 #ifdef _UNICODE  
174         if(GetEnabledShadow() && (GetTextShadowColorA() > 0 || GetTextShadowColorB() > 0))  
175             nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nShadowRc,&format,&nLineGrBrushA);  
176 
177         nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushB);  
178 #else  
179         USES_CONVERSION;  
180         wstring mTextValue = A2W(m_TextValue.GetData());  
181 
182         if(GetEnabledShadow() && (GetTextShadowColorA() > 0 || GetTextShadowColorB() > 0))  
183             nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nShadowRc,&format,&nLineGrBrushA);  
184 
185         nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushB);  
186 #endif  
187 
188     }  
189 }  
190   

转载:http://blog.csdn.net/chenlycly/article/details/24294639

参考QQ的效果,如下所示:

        DT_END_ELLIPSIS:对于显示的字符串,如果结束的字符串的范围不在矩形内,它会被截断并以省略号标识。

        DT_WORDBREAK:当一行中的字符将会延伸到由lpRect指定的矩形的边框时,此行自动地在字之间换行。

        DT_WORD_ELLIPSIS:截短不符合矩形的正文,并增加省略号。

        DT_PATH_ELLIPSIS:对于显示的字符串,会用省略号替换字符串中间的字符,以确保结果能在矩形范围内。 如果该字符串包含反斜杠()字符,它会尽可能的保留最后一个反斜杠之后的正文。

xml文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <Window size="750,500"  caption="0,0,0,60" sizebox="0,0,0,0" roundcorner="4,4">
 3   <!-- 窗口的初始尺寸(宽500,高350) 标题栏拖拽区域(高32)、可拖拽边框大小(这里添加sizebox后就可以拖拽边框调整大小了) -->
 4   <Font name="微软雅黑" size="13"/>
 5   <Font name="微软雅黑" size="16" />
 6   <Font name="微软雅黑" size="16" bold="true"/>
 7   <Font name="微软雅黑" size="13" underline="true"/>
 8   <Font name="宋体" size="13" />
 9   <Font name="微软雅黑" size="13" />
10 
11   <VerticalLayout bkcolor="#FFCCCCCC" bordersize="2" bordercolor="#FFE5DED8" >
12     <!-- 整个窗口的背景色 -->
13     <!-- 标题栏区 -->
14     <HorizontalLayout name="appbar" height="28"  bkcolor="#FF13A8E0">
15       <Control width="100"/>
16       <HorizontalLayout name="apptitle" >
17         <Label text="DuiLibTraverseFolderDemo        http://www.cnblogs.com/chechen/" align="center" textcolor="#FFF5F6F7"/>
18       </HorizontalLayout>
19       <Button name="minbtn" padding="4,0,0,0" width="24" height="24" tooltip="最小化" normalimage="file='sysbtn/Min.png'" hotimage="file='sysbtn/Min_hot.png'"/>
20       <Button name="maxbtn" padding="4,0,0,0" width="24" height="24" tooltip="最大化" normalimage="file='sysbtn/Max.png'" hotimage="file='sysbtn/Max_hot.png'"/>
21       <Button name="restorebtn" padding="4,0,0,0" width="24" height="24" tooltip="还原" visible="false" normalimage="file='sysbtn/Restore.png'" hotimage="file='sysbtn/Restore_hot.png'"/>
22       <Button name="closebtn" padding="4,0,0,0" width="24" height="24" tooltip="关闭" normalimage="file='sysbtn/Close.png'" hotimage="file='sysbtn/Close_hot.png'"/>
23     </HorizontalLayout>
24 
25     <HorizontalLayout bkcolor="#FFFFFFFF">
26 
27       <Label  text="正在操作,请稍后..."  align="center" font="2"/>
28       <LabelEx name="filepath" text="" float="true" pos="225,255,0,0" width="256"  height="20" font="0" />
29 
30     </HorizontalLayout>
31 
32   </VerticalLayout>
33 </Window>

 Demo:Duilib实现类似电脑管家扫描目录效果

原文地址:https://www.cnblogs.com/chechen/p/6060846.html