Skip to content

Commit

Permalink
-ImageString支持size、padding和align属性,图片目标区域绘制更加方便
Browse files Browse the repository at this point in the history
  • Loading branch information
qdtroy committed Jul 27, 2020
1 parent 351345b commit 75113f7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 52 deletions.
5 changes: 4 additions & 1 deletion Demos/duidemo/MainWnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void CMainWnd::InitWindow()
CWebBrowserUI* pBrowser2 = static_cast<CWebBrowserUI*>(m_pm.FindControl(_T("oneclick_browser2")));
pBrowser2->SetWebBrowserEventHandler(this);
pBrowser1->NavigateUrl(_T("https://www.baidu.com"));
pBrowser2->NavigateUrl(_T("http://www.winradar.com"));
pBrowser2->NavigateUrl(_T("https://pbsz.ebank.cmbchina.com/CmbBank_GenShell/UI/GenShellPC/Login/Login.aspx"));

// 动态创建Combo
CComboUI* pFontSize = static_cast<CComboUI*>(m_pm.FindControl(_T("font_size")));
Expand Down Expand Up @@ -157,6 +157,9 @@ void CMainWnd::InitWindow()
pItem->SetText(0, _T("张三"));
pItem->SetText(1, _T("1000"));
pItem->SetText(2, _T("100"));
pItem->SetTextColor(0, 0xff0000ff);
pItem->SetTextColor(1, 0xffff0000);
pItem->SetTextColor(2, 0xffffff00);
}

CTreeViewUI* pTreeView = static_cast<CTreeViewUI*>(m_pm.FindControl(_T("treeview")));
Expand Down
23 changes: 16 additions & 7 deletions DuiLib/Core/UIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,19 @@ namespace DuiLib {
else if( sItem == _T("hsl") ) {
bHSL = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
}
else if( sItem == _T("iconsize") ) {
szIcon.cx = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
szIcon.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
else if( sItem == _T("size") ) {
szImage.cx = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
szImage.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
}
else if( sItem == _T("iconalign") ) {
sIconAlign = sValue;
else if( sItem == _T("align") ) {
sAlign = sValue;
}
else if( sItem == _T("padding") ) {
rcPadding.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
rcPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
rcPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
rcPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcPadding);
}
}
if( *pStrImage++ != _T(' ') ) break;
Expand Down Expand Up @@ -182,8 +189,10 @@ namespace DuiLib {
bTiledX = false;
bTiledY = false;
bHSL = false;
szIcon.cx = szIcon.cy = 0;
sIconAlign.Empty();

szImage.cx = szImage.cy = 0;
sAlign.Empty();
memset(&rcPadding, 0, sizeof(RECT));
}

/////////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 4 additions & 2 deletions DuiLib/Core/UIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ namespace DuiLib {
bool bTiledX;
bool bTiledY;
bool bHSL;
SIZE szIcon;
CDuiString sIconAlign;

CDuiSize szImage;
RECT rcPadding;
CDuiString sAlign;
} TDrawInfo;

typedef struct UILIB_API tagTPercentInfo
Expand Down
69 changes: 31 additions & 38 deletions DuiLib/Core/UIRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,55 +729,42 @@ namespace DuiLib {
}
}


bool CRenderEngine::DrawIconImageString(HDC hDC, CPaintManagerUI* pManager, const RECT& rc, const RECT& rcPaint, LPCTSTR pStrImage, LPCTSTR pStrModify)
{
if ((pManager == NULL) || (hDC == NULL))
return false;

RECT rcDest = rc;
const TDrawInfo* pDrawInfo = pManager->GetDrawInfo(pStrImage, pStrModify);
if(!pDrawInfo->sIconAlign.IsEmpty()) {
MakeFitIconDest(rc, pDrawInfo->szIcon, pDrawInfo->sIconAlign, rcDest);
}

bool bRet = DuiLib::DrawImage(hDC, pManager, rc, rcPaint, pDrawInfo->sImageName, pDrawInfo->sResType, rcDest, \
pDrawInfo->rcSource, pDrawInfo->rcCorner, pDrawInfo->dwMask, pDrawInfo->uFade, pDrawInfo->bHole, pDrawInfo->bTiledX, pDrawInfo->bTiledY);

return true;
}

bool CRenderEngine::MakeFitIconDest(const RECT& rcControl,const CDuiSize& szIcon, const CDuiString& sAlign, RECT& rcDest)
bool CRenderEngine::MakeImageDest(const RECT& rcControl, const CDuiSize& szImage, const CDuiString& sAlign, const RECT& rcPadding, RECT& rcDest)
{
ASSERT(!sAlign.IsEmpty());
if(sAlign == _T("left"))
if(sAlign.Find(_T("left")) != -1)
{
rcDest.left = rcControl.left;
rcDest.top = rcControl.top;
rcDest.right = rcDest.left + szIcon.cx;
rcDest.bottom = rcDest.top + szIcon.cy;
rcDest.right = rcDest.left + szImage.cx;
}
else if( sAlign == _T("center") )
else if(sAlign.Find(_T("center")) != -1)
{
rcDest.left = rcControl.left + ((rcControl.right - rcControl.left) - szIcon.cx)/2;
rcDest.top = rcControl.top + ((rcControl.bottom - rcControl.top) - szIcon.cy)/2;
rcDest.right = rcDest.left + szIcon.cx;
rcDest.bottom = rcDest.top + szIcon.cy;
rcDest.left = rcControl.left + ((rcControl.right - rcControl.left) - szImage.cx)/2;
rcDest.right = rcDest.left + szImage.cx;
}
else if( sAlign == _T("vcenter") )
else if(sAlign.Find(_T("right")) != -1)
{
rcDest.left = rcControl.left;
rcDest.top = rcControl.top + ((rcControl.bottom - rcControl.top) - szIcon.cy)/2;
rcDest.right = rcDest.left + szIcon.cx;
rcDest.bottom = rcDest.top + szIcon.cy;
rcDest.left = rcControl.right - szImage.cx;
rcDest.right = rcDest.left + szImage.cx;
}
else if( sAlign == _T("hcenter") )

if(sAlign.Find(_T("top")) != -1)
{
rcDest.left = rcControl.left + ((rcControl.right - rcControl.left) - szIcon.cx)/2;
rcDest.top = rcControl.top;
rcDest.right = rcDest.left + szIcon.cx;
rcDest.bottom = rcDest.top + szIcon.cy;
rcDest.bottom = rcDest.top + szImage.cy;
}
else if(sAlign.Find(_T("vcenter")) != -1)
{
rcDest.top = rcControl.top + ((rcControl.bottom - rcControl.top) - szImage.cy)/2;
rcDest.bottom = rcDest.top + szImage.cy;
}
else if(sAlign.Find(_T("bottom")) != -1)
{
rcDest.top = rcControl.bottom - szImage.cy;
rcDest.bottom = rcDest.top + rcDest.top;
}

::OffsetRect(&rcDest, rcPadding.left, rcPadding.top);
::OffsetRect(&rcDest, -rcPadding.right, -rcPadding.bottom);

if (rcDest.right > rcControl.right)
rcDest.right = rcControl.right;
Expand Down Expand Up @@ -1274,6 +1261,7 @@ namespace DuiLib {
{
if( pManager == NULL || hDC == NULL || pDrawInfo == NULL ) return false;
RECT rcDest = rcItem;
// 计算绘制目标区域
if( pDrawInfo->rcDest.left != 0 || pDrawInfo->rcDest.top != 0 ||
pDrawInfo->rcDest.right != 0 || pDrawInfo->rcDest.bottom != 0 ) {
rcDest.left = rcItem.left + pDrawInfo->rcDest.left;
Expand All @@ -1283,6 +1271,11 @@ namespace DuiLib {
rcDest.bottom = rcItem.top + pDrawInfo->rcDest.bottom;
if( rcDest.bottom > rcItem.bottom ) rcDest.bottom = rcItem.bottom;
}
// 根据对齐方式计算目标区域
if(pDrawInfo->szImage.cx > 0 && pDrawInfo->szImage.cy > 0) {
MakeImageDest(rcItem, pDrawInfo->szImage, pDrawInfo->sAlign, pDrawInfo->rcPadding, rcDest);
}

bool bRet = DuiLib::DrawImage(hDC, pManager, rcItem, rcPaint, pDrawInfo->sImageName, pDrawInfo->sResType, rcDest, \
pDrawInfo->rcSource, pDrawInfo->rcCorner, pDrawInfo->dwMask, pDrawInfo->uFade, pDrawInfo->bHole, pDrawInfo->bTiledX, pDrawInfo->bTiledY, instance);

Expand Down
4 changes: 1 addition & 3 deletions DuiLib/Core/UIRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ namespace DuiLib {
static Gdiplus::Image* GdiplusLoadImage(LPCTSTR pstrPath);
static Gdiplus::Image* GdiplusLoadImage(LPVOID pBuf, size_t dwSize);

static bool DrawIconImageString(HDC hDC, CPaintManagerUI* pManager, const RECT& rcItem, const RECT& rcPaint, \
LPCTSTR pStrImage, LPCTSTR pStrModify = NULL);
static bool MakeFitIconDest(const RECT& rcControl,const CDuiSize& szIcon, const CDuiString& sAlign, RECT& rcDest);
static bool MakeImageDest(const RECT& rcControl, const CDuiSize& szImage, const CDuiString& sAlign, const RECT& rcPadding, RECT& rcDest);

static void DrawText(HDC hDC, CPaintManagerUI* pManager, RECT& rc, LPCTSTR pstrText,DWORD dwTextColor, \
int iFont, UINT uStyle, DWORD dwTextBKColor);
Expand Down
2 changes: 1 addition & 1 deletion bin/skin/duidemo/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<Label text="按钮控件" textcolor="#FF000000" font="font13" width="80"/>
<HorizontalLayout childvalign="vcenter" childpadding="12" bordersize="1,1,1,1" bordercolor="#0">
<Button name="button1" text="普通按钮" textcolor="#FF000000" textpadding="6,0,6,0" style="btn_style" width="0" autocalcwidth="true" />
<Button name="button2" text="图标按钮" textcolor="#FF000000" style="btn_style" height="40" width="90" textpadding="25,0,0,0" align="center" valign="left" foreimage="dest='6,12,22,28' res='other/icon.png'" />
<Button name="button2" text="图标按钮" textcolor="#FF000000" style="btn_style" height="40" width="90" textpadding="25,0,0,0" align="center" valign="left" foreimage="size='16,16' padding='6,12' align='left,top' res='other/icon.png'" />
<Button name="cmdbtn" text="命令按钮(&S)" textcolor="#FF000000" style="btn_style" shortcut="S" width="120"/>
<Button text="Button按钮{\n}控件自动换行测试" width="60" height="50" align="left" wordbreak="true" endellipsis="true" textcolor="#FF000000" textpadding="6,0,6,0" style="colorbtn_style"/>
<Button name="autobtn" text="自适应比例按钮" minw1idth="120" colorhsl="true" float="0.7,0.2,0.9,0.8" textcolor="#FF000000" bkcolor="#FFFF5555" hotbkcolor="#AAFFFF00" pushedbkcolor="#FFFF00FF" borderround="4,4"/>
Expand Down

0 comments on commit 75113f7

Please sign in to comment.