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

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

实现原理:

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

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

扩展控件的.h文件

 /*************************************************************************************************
*
* File Name : UILabelEx.h
*
* Created : 2016/11/11
*
* Author : http://www.cnblogs.com/chechen/
*
* Model :
*
* Description : [[CN]] 扩展Label控件 [[CN]]
*
**************************************************************************************************/
#ifndef _UILABELEX_H_
#define _UILABELEX_H_ class CLabelExUI : public CLabelUI
{
public:
CLabelExUI(void);
~CLabelExUI(void); LPCTSTR GetClass() const;
LPVOID GetInterface(LPCTSTR pstrName); void PaintText(HDC hDC);
void SetPos(RECT rc); private:
Color _MakeRGB(int a, Color cl);
Color _MakeRGB(int r, int g, int b);
};
#endif//_UILABELEX_H_

.cpp

 #include "StdAfx.h"
#include "UILabelEx.h" CLabelExUI::CLabelExUI(void)
{
} CLabelExUI::~CLabelExUI(void)
{
} LPCTSTR CLabelExUI::GetClass() const
{
return TEXT("LabelExUI");
} LPVOID CLabelExUI::GetInterface(LPCTSTR pstrName)
{
if( _tcscmp(pstrName, TEXT("LabelEx")) == ) return static_cast<CLabelExUI*>(this);
return CLabelUI::GetInterface(pstrName);
} Color CLabelExUI::_MakeRGB(int a, Color cl)
{
return Color(a, cl.GetR(), cl.GetG(), cl.GetB());
} Color CLabelExUI::_MakeRGB(int r, int g, int b)
{
return Color(, r, g, b);
} void CLabelExUI::SetPos(RECT rc)
{
__super::SetPos(rc);
} void CLabelExUI::PaintText(HDC hDC)
{
if( m_dwTextColor == ) m_dwTextColor = m_pManager->GetDefaultFontColor();
if( m_dwDisabledTextColor == ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor(); RECT rc = m_rcItem;
rc.left += m_rcTextPadding.left;
rc.right -= m_rcTextPadding.right;
rc.top += m_rcTextPadding.top;
rc.bottom -= m_rcTextPadding.bottom; if(!GetEnabledEffect())
{
if( m_sText.IsEmpty() ) return;
int nLinks = ;
if( IsEnabled() ) {
if( m_bShowHtml )
CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, m_dwTextColor,
NULL, NULL, nLinks, DT_SINGLELINE | m_uTextStyle);
else
{
ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
::SetBkMode(hDC, TRANSPARENT);
::SetTextColor(hDC, RGB(GetBValue(m_dwTextColor), GetGValue(m_dwTextColor), GetRValue(m_dwTextColor)));
HFONT hOldFont = (HFONT)::SelectObject(hDC, m_pManager->GetFont(m_iFont)); SIZE size;
::GetTextExtentPoint32(hDC,m_sText.GetData(),m_sText.GetLength(),&size); UINT uStyle = ;
if(size.cx > rc.right - rc.left)
{
uStyle = DT_PATH_ELLIPSIS|DT_WORD_ELLIPSIS|DT_END_ELLIPSIS;
}
else
{
uStyle = DT_CENTER | DT_VCENTER | DT_SINGLELINE;
}
::DrawText(hDC, m_sText, -, &rc, uStyle | DT_NOPREFIX);
::SelectObject(hDC, hOldFont);
}
}
else {
if( m_bShowHtml )
CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, m_dwDisabledTextColor,
NULL, NULL, nLinks, DT_SINGLELINE | m_uTextStyle);
else {
ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
::SetBkMode(hDC, TRANSPARENT);
::SetTextColor(hDC, RGB(GetBValue(m_dwTextColor), GetGValue(m_dwTextColor), GetRValue(m_dwTextColor)));
HFONT hOldFont = (HFONT)::SelectObject(hDC, m_pManager->GetFont(m_iFont)); SIZE size;
::GetTextExtentPoint32(hDC,m_sText.GetData(),m_sText.GetLength(),&size); UINT uStyle = ;
if(size.cx > rc.right - rc.left)
{
uStyle = DT_PATH_ELLIPSIS|DT_WORD_ELLIPSIS|DT_END_ELLIPSIS;
}
else
{
uStyle = DT_CENTER | DT_VCENTER | DT_SINGLELINE;
}
::DrawText(hDC, m_sText, -, &rc, uStyle | DT_NOPREFIX);
::SelectObject(hDC, hOldFont); //CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, m_dwDisabledTextColor,
//m_iFont, /* DT_SINGLELINE | */m_uTextStyle); ///< @todo
} }
}
else
{
Font nFont(hDC,m_pManager->GetFont(GetFont())); Graphics nGraphics(hDC);
nGraphics.SetTextRenderingHint(m_TextRenderingHintAntiAlias); StringFormat format;
format.SetAlignment((StringAlignment)m_hAlign);
format.SetLineAlignment((StringAlignment)m_vAlign); RectF nRc((float)rc.left,(float)rc.top,(float)rc.right-rc.left,(float)rc.bottom-rc.top);
RectF nShadowRc = nRc;
nShadowRc.X += m_ShadowOffset.X;
nShadowRc.Y += m_ShadowOffset.Y; int nGradientLength = GetGradientLength(); if(nGradientLength == )
nGradientLength = (rc.bottom-rc.top); LinearGradientBrush nLineGrBrushA(Point(GetGradientAngle(), ),Point(,nGradientLength),_MakeRGB(GetTransShadow(),GetTextShadowColorA()),_MakeRGB(GetTransShadow1(),GetTextShadowColorB() == -?GetTextShadowColorA():GetTextShadowColorB()));
LinearGradientBrush nLineGrBrushB(Point(GetGradientAngle(), ),Point(,nGradientLength),_MakeRGB(GetTransText(),GetTextColor()),_MakeRGB(GetTransText1(),GetTextColor1() == -?GetTextColor():GetTextColor1())); if(GetEnabledStroke() && GetStrokeColor() > )
{
LinearGradientBrush nLineGrBrushStroke(Point(GetGradientAngle(),),Point(,rc.bottom-rc.top+),_MakeRGB(GetTransStroke(),GetStrokeColor()),_MakeRGB(GetTransStroke(),GetStrokeColor())); #ifdef _UNICODE
nRc.Offset(-,);
nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(,);
nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(-,-);
nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(,);
nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(,-);
#else
USES_CONVERSION;
wstring mTextValue = A2W(m_TextValue.GetData()); nRc.Offset(-,);
nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(,);
nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(-,-);
nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(,);
nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(,-);
#endif }
#ifdef _UNICODE
if(GetEnabledShadow() && (GetTextShadowColorA() > || GetTextShadowColorB() > ))
nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nShadowRc,&format,&nLineGrBrushA); nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushB);
#else
USES_CONVERSION;
wstring mTextValue = A2W(m_TextValue.GetData()); if(GetEnabledShadow() && (GetTextShadowColorA() > || GetTextShadowColorB() > ))
nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nShadowRc,&format,&nLineGrBrushA); nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushB);
#endif }
}

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

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

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

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

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

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

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

xml文件:

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

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

上一篇:安装VMware vSphere 的目的就是在一台物理服务器上安装很多很多的虚拟机


下一篇:Java基础类库