duilib模仿实现list ctrl的icon

来源:互联网 发布:淘宝ps4游戏商店 编辑:程序博客网 时间:2024/05/01 21:11

// hDc HDC句柄
// CPaintManagerUI句柄
// rc 目标画布的大小
// rcPaint 绘制区域
// pStrImage 为源图像的名称,不需提供路径,函数内部会自动加上资源位置的路径,路径为CPaintManagerUI::SetResourcePath设置的路径。
// pStrModify 设置绘制属性,含义为:
// 2、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'
// source和dest表示从源图像的source区域贴到目标图像的dest区域,mask表示让某颜色为透明色,例如mask="#FF000000",设置黑色为透明色。
// xtiled,ytiled 设置为true表示横向和纵向的图像不拉伸显示而是平铺显示
// hole 为true表示不绘制中间部分,为某些场合提高性能
bool CRenderEngine::DrawImageString(HDC hDC, CPaintManagerUI* pManager, const RECT& rc, const RECT& rcPaint, LPCTSTR pStrImage, LPCTSTR pStrModify)
{
 if ((pManager == NULL) || (hDC == NULL)) return false;


    // 1、aaa.jpg
    // 2、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'


}
void CRenderEngine::DrawImage(HDC hDC, HBITMAP hBitmap, const RECT& rc, const RECT& rcPaint, const RECT& rcBmpPart, const RECT& rcCorners, bool alphaChannel, BYTE uFade, bool hole, bool xtiled, bool ytiled)

画:image(m_rcItem,m_rcPaint,rcItem,rcBmpPart,rcCorner)
1、CControlUI
m_rcItem:控件在窗口上的坐标(相对于窗口)
m_rcPaint:控件需要重绘区域的坐标(相对于窗口)
2、CRenderEngine
函数DrawImageData,DrawImageString处理完之后得到rcItem, rcBmpPart, rcCorner。
rcItem:图像绘制位置坐标,m_rcItem相对于dest(rcItem.left相对于m_rcItem.left移动,rcItem.right相对于m_rcItem.left移动,rcItem.top相对于m_rcItem.top移动,rcItem.bottom相对于m_rcItem.top移动)移动之后的坐标(相对于窗口)
rcBmpPart:绘制图像的那部分区域
rcCorner:图像距离rcItem四周的距离

函数DrawImage处理rcBmpPart

函数DrawImage绘制(根据rcItem,rcPaint,rcBmpPart,rcCorner,其他属性)


设置image
根据xml设置:
CControlUI* CDialogBuilder::Create调用TImageInfo* CPaintManagerUI::AddImage。
TImageInfo* CPaintManagerUI::AddImage调用TImageInfo* CRenderEngine::LoadImage,并保存到m_mImageHash。
程序中动态设置:
bool CControlUI::DrawImage调用bool CRenderEngine::DrawImageString。
bool CRenderEngine::DrawImageString调用bool DrawImage。
bool DrawImage调用TImageInfo* CPaintManagerUI::GetImageEx。
TImageInfo* CPaintManagerUI::GetImageEx调用TImageInfo* CPaintManagerUI::AddImage。
TImageInfo* CPaintManagerUI::AddImage调用ImageInfo* CRenderEngine::LoadImage,并保存到m_mImageHash。


修改duilib动态设置image数据

CRenderEngine:添加

static TImageInfo* LoadImage(PBYTE pImageData, DWORD dwImageSize, LPCTSTR type = NULL, DWORD mask = 0);
 static bool DrawImageData(HDC hDC, const TImageInfo *data, const RECT& rcItem, const RECT& rcPaint,
        LPCTSTR pStrModify = NULL);

TImageInfo* CRenderEngine::LoadImage(PBYTE pData, DWORD dwSize, LPCTSTR type, DWORD mask){LPBYTE pImage = NULL;    int x,y,n;    pImage = stbi_load_from_memory(pData, dwSize, &x, &y, &n, 4);    //delete[] pData;if( !pImage ) {//::MessageBox(0, _T("解析图片失败"), _T("抓BUG"), MB_OK);return NULL;}    BITMAPINFO bmi;    ::ZeroMemory(&bmi, sizeof(BITMAPINFO));    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);    bmi.bmiHeader.biWidth = x;    bmi.bmiHeader.biHeight = -y;    bmi.bmiHeader.biPlanes = 1;    bmi.bmiHeader.biBitCount = 32;    bmi.bmiHeader.biCompression = BI_RGB;    bmi.bmiHeader.biSizeImage = x * y * 4;    bool bAlphaChannel = false;    LPBYTE pDest = NULL;    HBITMAP hBitmap = ::CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void**)&pDest, NULL, 0);if( !hBitmap ) {//::MessageBox(0, _T("CreateDIBSection失败"), _T("抓BUG"), MB_OK);return NULL;}    for( int i = 0; i < x * y; i++ )     {        pDest[i*4 + 3] = pImage[i*4 + 3];        if( pDest[i*4 + 3] < 255 )        {            pDest[i*4] = (BYTE)(DWORD(pImage[i*4 + 2])*pImage[i*4 + 3]/255);            pDest[i*4 + 1] = (BYTE)(DWORD(pImage[i*4 + 1])*pImage[i*4 + 3]/255);            pDest[i*4 + 2] = (BYTE)(DWORD(pImage[i*4])*pImage[i*4 + 3]/255);             bAlphaChannel = true;        }        else        {            pDest[i*4] = pImage[i*4 + 2];            pDest[i*4 + 1] = pImage[i*4 + 1];            pDest[i*4 + 2] = pImage[i*4];         }        if( *(DWORD*)(&pDest[i*4]) == mask ) {            pDest[i*4] = (BYTE)0;            pDest[i*4 + 1] = (BYTE)0;            pDest[i*4 + 2] = (BYTE)0;             pDest[i*4 + 3] = (BYTE)0;            bAlphaChannel = true;        }    }    stbi_image_free(pImage);    TImageInfo* data = new TImageInfo;    data->hBitmap = hBitmap;    data->nX = x;    data->nY = y;    data->alphaChannel = bAlphaChannel;    return data;}


bool CRenderEngine::DrawImageData(HDC hDC, const TImageInfo *data, const RECT& rc, const RECT& rcPaint, LPCTSTR pStrModify){if ((data == NULL) || (hDC == NULL)) return false;    // 1、aaa.jpg    // 2、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'char temp[256];sprintf_s(temp, sizeof(temp), "\nDrawImageData\nrc:%d %d %d %d, rcPaint:%d %d %d %d\n", rc.left, rc.top, rc.right, rc.bottom,  rcPaint.left, rcPaint.top, rcPaint.right, rcPaint.bottom);OutputDebugString(temp);OutputDebugString(pStrModify);OutputDebugString("\n");    //CDuiString sImageName = pStrImage;LPCTSTR pStrImage(NULL);    CDuiString sImageResType;    RECT rcItem = rc;    RECT rcBmpPart = {0};    RECT rcCorner = {0};    DWORD dwMask = 0;    BYTE bFade = 0xFF;    bool bHole = false;    bool bTiledX = false;    bool bTiledY = false;bool bInside = false;int image_count = 0;    CDuiString sItem;    CDuiString sValue;    LPTSTR pstr = NULL;    for( int i = 0; i < 2; ++i,image_count = 0 ) {        if( i == 1)            pStrImage = pStrModify;        if( !pStrImage ) continue;        while( *pStrImage != _T('\0') ) {            sItem.Empty();            sValue.Empty();            while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);            while( *pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' ') ) {                LPTSTR pstrTemp = ::CharNext(pStrImage);                while( pStrImage < pstrTemp) {                    sItem += *pStrImage++;                }            }            while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);            if( *pStrImage++ != _T('=') ) break;            while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);            if( *pStrImage++ != _T('\'') ) break;            while( *pStrImage != _T('\0') && *pStrImage != _T('\'') ) {                LPTSTR pstrTemp = ::CharNext(pStrImage);                while( pStrImage < pstrTemp) {                    sValue += *pStrImage++;                }            }            if( *pStrImage++ != _T('\'') ) break;            if( !sValue.IsEmpty() ) {                if( sItem == _T("file") || sItem == _T("res") ) {if( image_count > 0 )DuiLib::DrawImage(hDC, data, rc, rcPaint, sImageResType,rcItem, rcBmpPart, rcCorner, dwMask, bFade, bHole, bTiledX, bTiledY, bInside);                    //sImageName = sValue;if( sItem == _T("file") )++image_count;                }                else if( sItem == _T("restype") ) {if( image_count > 0 )DuiLib::DrawImage(hDC, data, rc, rcPaint, sImageResType,rcItem, rcBmpPart, rcCorner, dwMask, bFade, bHole, bTiledX, bTiledY, bInside);                    sImageResType = sValue;++image_count;                }                else if( sItem == _T("dest") ) {                    rcItem.left = rc.left + _tcstol(sValue.GetData(), &pstr, 10);  ASSERT(pstr);                        rcItem.top = rc.top + _tcstol(pstr + 1, &pstr, 10);    ASSERT(pstr);                    rcItem.right = rc.left + _tcstol(pstr + 1, &pstr, 10);  ASSERT(pstr);if (rcItem.right > rc.right) rcItem.right = rc.right;                    rcItem.bottom = rc.top + _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);if (rcItem.bottom > rc.bottom) rcItem.bottom = rc.bottom;                }                else if( sItem == _T("source") ) {                    rcBmpPart.left = _tcstol(sValue.GetData(), &pstr, 10);  ASSERT(pstr);                        rcBmpPart.top = _tcstol(pstr + 1, &pstr, 10);    ASSERT(pstr);                        rcBmpPart.right = _tcstol(pstr + 1, &pstr, 10);  ASSERT(pstr);                        rcBmpPart.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);                  }                else if( sItem == _T("corner") ) {                    rcCorner.left = _tcstol(sValue.GetData(), &pstr, 10);  ASSERT(pstr);                        rcCorner.top = _tcstol(pstr + 1, &pstr, 10);    ASSERT(pstr);                        rcCorner.right = _tcstol(pstr + 1, &pstr, 10);  ASSERT(pstr);                        rcCorner.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);                }                else if( sItem == _T("mask") ) {                    if( sValue[0] == _T('#')) dwMask = _tcstoul(sValue.GetData() + 1, &pstr, 16);                    else dwMask = _tcstoul(sValue.GetData(), &pstr, 16);                }                else if( sItem == _T("fade") ) {                    bFade = (BYTE)_tcstoul(sValue.GetData(), &pstr, 10);                }                else if( sItem == _T("hole") ) {                    bHole = (_tcscmp(sValue.GetData(), _T("true")) == 0);                }                else if( sItem == _T("xtiled") ) {bTiledX = (_tcscmp(sValue.GetData(), _T("true")) == 0);                }                else if( sItem == _T("ytiled") ) {                    bTiledY = (_tcscmp(sValue.GetData(), _T("true")) == 0);                }else if( sItem == _T("inside") ) {                    bInside = (_tcscmp(sValue.GetData(), _T("true")) == 0);                }            }            if( *pStrImage++ != _T(' ') ) break;        }    }sprintf_s(temp, sizeof(temp), "rcItem:%d %d %d %d, rcBmpPart:%d %d %d %d rcCorner:%d %d %d %d\n", rcItem.left, rcItem.top, rcItem.right, rcItem.bottom,  rcBmpPart.left, rcBmpPart.top, rcBmpPart.right, rcBmpPart.bottom,rcCorner.left, rcCorner.top, rcCorner.right, rcCorner.bottom);OutputDebugString(temp);OutputDebugString("*******************************\n");DuiLib::DrawImage(hDC, data, rc, rcPaint, sImageResType,rcItem, rcBmpPart, rcCorner, dwMask, bFade, bHole, bTiledX, bTiledY, bInside);    return true;}

bool DrawImage(HDC hDC, const TImageInfo *data, const RECT& rc, const RECT& rcPaint, \const CDuiString& sImageResType, RECT rcItem, RECT rcBmpPart, RECT rcCorner, DWORD dwMask, BYTE bFade, \bool bHole, bool bTiledX, bool bTiledY, bool bInside = false){if( !data ) return false;    if( rcBmpPart.left == 0 && rcBmpPart.right == 0 && rcBmpPart.top == 0 && rcBmpPart.bottom == 0 ) {rcBmpPart.right = data->nX;rcBmpPart.bottom = data->nY;}if (rcBmpPart.right > data->nX) rcBmpPart.right = data->nX;if (rcBmpPart.bottom > data->nY) rcBmpPart.bottom = data->nY;RECT rcTemp;if( !::IntersectRect(&rcTemp, &rcItem, &rc) ) return true;if( !::IntersectRect(&rcTemp, &rcItem, &rcPaint) ) return true;CRenderEngine::DrawImage(hDC, data->hBitmap, rcItem, rcPaint, rcBmpPart, rcCorner, data->alphaChannel, bFade, bHole, bTiledX, bTiledY, bInside);return true;}


。duilib中的函数

void CRenderEngine::DrawImage(HDC hDC, HBITMAP hBitmap, const RECT& rc, const RECT& rcPaint,                                    const RECT& rcBmpPart, const RECT& rcCorners, bool alphaChannel,                                     BYTE uFade, bool hole, bool xtiled, bool ytiled, bool inside){    ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);    typedef BOOL (WINAPI *LPALPHABLEND)(HDC, int, int, int, int,HDC, int, int, int, int, BLENDFUNCTION);    static LPALPHABLEND lpAlphaBlend = (LPALPHABLEND) ::GetProcAddress(::GetModuleHandle(_T("msimg32.dll")), "AlphaBlend");    if( lpAlphaBlend == NULL ) lpAlphaBlend = AlphaBitBlt;    if( hBitmap == NULL ) return;    HDC hCloneDC = ::CreateCompatibleDC(hDC);    HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(hCloneDC, hBitmap);    ::SetStretchBltMode(hDC, HALFTONE);    RECT rcTemp = {0};    RECT rcDest = {0};    if( lpAlphaBlend && (alphaChannel || uFade < 255) ) {        BLENDFUNCTION bf = { AC_SRC_OVER, 0, uFade, AC_SRC_ALPHA };        // middle        if( !hole ) {            rcDest.left = rc.left + rcCorners.left;            rcDest.top = rc.top + rcCorners.top;            rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;            rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;            rcDest.right += rcDest.left;            rcDest.bottom += rcDest.top;            if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                if(inside){LONG lBmpWidth = rcBmpPart.right - rcBmpPart.left;LONG lBmpHeight = rcBmpPart.bottom - rcBmpPart.top;LONG lWidth = rcDest.right - rcDest.left;LONG lHeight = rcDest.bottom - rcDest.top;double iTimesX = lWidth * 1.0 / lBmpWidth;double iTimesY = lHeight * 1.0  / lBmpHeight;double iTimes(min(iTimesX, iTimesY));rcDest.left += (LONG)((lWidth - lBmpWidth * iTimes) / 2);rcDest.right -= (LONG)((lWidth - lBmpWidth * iTimes) / 2);rcDest.top += (LONG)((lHeight - lBmpHeight * iTimes) / 2);rcDest.bottom -= (LONG)((lHeight - lBmpHeight * iTimes) / 2);rcDest.right -= rcDest.left;rcDest.bottom -= rcDest.top;lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \rcBmpPart.left, rcBmpPart.top, \rcBmpPart.right - rcBmpPart.left, \rcBmpPart.bottom - rcBmpPart.top, bf);}else if( !xtiled && !ytiled ) {                    rcDest.right -= rcDest.left;                    rcDest.bottom -= rcDest.top;                    lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                        rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \                        rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, \                        rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);                }                else if( xtiled && ytiled ) {                    LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;                    LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;                    int iTimesX = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;                    int iTimesY = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;                    for( int j = 0; j < iTimesY; ++j ) {                        LONG lDestTop = rcDest.top + lHeight * j;                        LONG lDestBottom = rcDest.top + lHeight * (j + 1);                        LONG lDrawHeight = lHeight;                        if( lDestBottom > rcDest.bottom ) {                            lDrawHeight -= lDestBottom - rcDest.bottom;                            lDestBottom = rcDest.bottom;                        }                        for( int i = 0; i < iTimesX; ++i ) {                            LONG lDestLeft = rcDest.left + lWidth * i;                            LONG lDestRight = rcDest.left + lWidth * (i + 1);                            LONG lDrawWidth = lWidth;                            if( lDestRight > rcDest.right ) {                                lDrawWidth -= lDestRight - rcDest.right;                                lDestRight = rcDest.right;                            }                            lpAlphaBlend(hDC, rcDest.left + lWidth * i, rcDest.top + lHeight * j,                                 lDestRight - lDestLeft, lDestBottom - lDestTop, hCloneDC,                                 rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, lDrawWidth, lDrawHeight, bf);                        }                    }                }                else if( xtiled ) {                    LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;                    int iTimes = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;                    for( int i = 0; i < iTimes; ++i ) {                        LONG lDestLeft = rcDest.left + lWidth * i;                        LONG lDestRight = rcDest.left + lWidth * (i + 1);                        LONG lDrawWidth = lWidth;                        if( lDestRight > rcDest.right ) {                            lDrawWidth -= lDestRight - rcDest.right;                            lDestRight = rcDest.right;                        }                        lpAlphaBlend(hDC, lDestLeft, rcDest.top, lDestRight - lDestLeft, rcDest.bottom,                             hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \                            lDrawWidth, rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);                    }                }                else { // ytiled                    LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;                    int iTimes = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;                    for( int i = 0; i < iTimes; ++i ) {                        LONG lDestTop = rcDest.top + lHeight * i;                        LONG lDestBottom = rcDest.top + lHeight * (i + 1);                        LONG lDrawHeight = lHeight;                        if( lDestBottom > rcDest.bottom ) {                            lDrawHeight -= lDestBottom - rcDest.bottom;                            lDestBottom = rcDest.bottom;                        }                        lpAlphaBlend(hDC, rcDest.left, rcDest.top + lHeight * i, rcDest.right, lDestBottom - lDestTop,                             hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \                            rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, lDrawHeight, bf);                                        }                }            }        }        // left-top        if( rcCorners.left > 0 && rcCorners.top > 0 ) {            rcDest.left = rc.left;            rcDest.top = rc.top;            rcDest.right = rcCorners.left;            rcDest.bottom = rcCorners.top;            rcDest.right += rcDest.left;            rcDest.bottom += rcDest.top;            if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                rcDest.right -= rcDest.left;                rcDest.bottom -= rcDest.top;                lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                    rcBmpPart.left, rcBmpPart.top, rcCorners.left, rcCorners.top, bf);            }        }        // top        if( rcCorners.top > 0 ) {            rcDest.left = rc.left + rcCorners.left;            rcDest.top = rc.top;            rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;            rcDest.bottom = rcCorners.top;            rcDest.right += rcDest.left;            rcDest.bottom += rcDest.top;            if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                rcDest.right -= rcDest.left;                rcDest.bottom -= rcDest.top;                lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                    rcBmpPart.left + rcCorners.left, rcBmpPart.top, rcBmpPart.right - rcBmpPart.left - \                    rcCorners.left - rcCorners.right, rcCorners.top, bf);            }        }        // right-top        if( rcCorners.right > 0 && rcCorners.top > 0 ) {            rcDest.left = rc.right - rcCorners.right;            rcDest.top = rc.top;            rcDest.right = rcCorners.right;            rcDest.bottom = rcCorners.top;            rcDest.right += rcDest.left;            rcDest.bottom += rcDest.top;            if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                rcDest.right -= rcDest.left;                rcDest.bottom -= rcDest.top;                lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                    rcBmpPart.right - rcCorners.right, rcBmpPart.top, rcCorners.right, rcCorners.top, bf);            }        }        // left        if( rcCorners.left > 0 ) {            rcDest.left = rc.left;            rcDest.top = rc.top + rcCorners.top;            rcDest.right = rcCorners.left;            rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;            rcDest.right += rcDest.left;            rcDest.bottom += rcDest.top;            if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                rcDest.right -= rcDest.left;                rcDest.bottom -= rcDest.top;                lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                    rcBmpPart.left, rcBmpPart.top + rcCorners.top, rcCorners.left, rcBmpPart.bottom - \                    rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);            }        }        // right        if( rcCorners.right > 0 ) {            rcDest.left = rc.right - rcCorners.right;            rcDest.top = rc.top + rcCorners.top;            rcDest.right = rcCorners.right;            rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;            rcDest.right += rcDest.left;            rcDest.bottom += rcDest.top;            if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                rcDest.right -= rcDest.left;                rcDest.bottom -= rcDest.top;                lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                    rcBmpPart.right - rcCorners.right, rcBmpPart.top + rcCorners.top, rcCorners.right, \                    rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);            }        }        // left-bottom        if( rcCorners.left > 0 && rcCorners.bottom > 0 ) {            rcDest.left = rc.left;            rcDest.top = rc.bottom - rcCorners.bottom;            rcDest.right = rcCorners.left;            rcDest.bottom = rcCorners.bottom;            rcDest.right += rcDest.left;            rcDest.bottom += rcDest.top;            if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                rcDest.right -= rcDest.left;                rcDest.bottom -= rcDest.top;                lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                    rcBmpPart.left, rcBmpPart.bottom - rcCorners.bottom, rcCorners.left, rcCorners.bottom, bf);            }        }        // bottom        if( rcCorners.bottom > 0 ) {            rcDest.left = rc.left + rcCorners.left;            rcDest.top = rc.bottom - rcCorners.bottom;            rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;            rcDest.bottom = rcCorners.bottom;            rcDest.right += rcDest.left;            rcDest.bottom += rcDest.top;            if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                rcDest.right -= rcDest.left;                rcDest.bottom -= rcDest.top;                lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                    rcBmpPart.left + rcCorners.left, rcBmpPart.bottom - rcCorners.bottom, \                    rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, rcCorners.bottom, bf);            }        }        // right-bottom        if( rcCorners.right > 0 && rcCorners.bottom > 0 ) {            rcDest.left = rc.right - rcCorners.right;            rcDest.top = rc.bottom - rcCorners.bottom;            rcDest.right = rcCorners.right;            rcDest.bottom = rcCorners.bottom;            rcDest.right += rcDest.left;            rcDest.bottom += rcDest.top;            if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                rcDest.right -= rcDest.left;                rcDest.bottom -= rcDest.top;                lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                    rcBmpPart.right - rcCorners.right, rcBmpPart.bottom - rcCorners.bottom, rcCorners.right, \                    rcCorners.bottom, bf);            }        }    }    else     {        if (rc.right - rc.left == rcBmpPart.right - rcBmpPart.left \            && rc.bottom - rc.top == rcBmpPart.bottom - rcBmpPart.top \            && rcCorners.left == 0 && rcCorners.right == 0 && rcCorners.top == 0 && rcCorners.bottom == 0)        {            if( ::IntersectRect(&rcTemp, &rcPaint, &rc) ) {                ::BitBlt(hDC, rcTemp.left, rcTemp.top, rcTemp.right - rcTemp.left, rcTemp.bottom - rcTemp.top, \                    hCloneDC, rcBmpPart.left + rcTemp.left - rc.left, rcBmpPart.top + rcTemp.top - rc.top, SRCCOPY);            }        }        else        {            // middle            if( !hole ) {                rcDest.left = rc.left + rcCorners.left;                rcDest.top = rc.top + rcCorners.top;                rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;                rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;                rcDest.right += rcDest.left;                rcDest.bottom += rcDest.top;                if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                    if(inside){LONG lBmpWidth = rcBmpPart.right - rcBmpPart.left;                        LONG lBmpHeight = rcBmpPart.bottom - rcBmpPart.top;LONG lWidth = rcDest.right - rcDest.left;                        LONG lHeight = rcDest.bottom - rcDest.top;double iTimesX = lWidth * 1.0 / lBmpWidth;                        double iTimesY = lHeight * 1.0  / lBmpHeight;double iTimes(min(iTimesX, iTimesY));rcDest.left += (LONG)((lWidth - lBmpWidth * iTimes) / 2);rcDest.right -= (LONG)((lWidth - lBmpWidth * iTimes) / 2);rcDest.top += (LONG)((lHeight - lBmpHeight * iTimes) / 2);rcDest.bottom -= (LONG)((lHeight - lBmpHeight * iTimes) / 2);rcDest.right -= rcDest.left;                        rcDest.bottom -= rcDest.top;::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                            rcBmpPart.left, rcBmpPart.top, \                            rcBmpPart.right - rcBmpPart.left, \                            rcBmpPart.bottom - rcBmpPart.top, SRCCOPY);}else if( !xtiled && !ytiled ) {                        rcDest.right -= rcDest.left;                        rcDest.bottom -= rcDest.top;                        ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                            rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \                            rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, \                            rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);                    }                    else if( xtiled && ytiled ) {                        LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;                        LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;                        int iTimesX = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;                        int iTimesY = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;                        for( int j = 0; j < iTimesY; ++j ) {                            LONG lDestTop = rcDest.top + lHeight * j;                            LONG lDestBottom = rcDest.top + lHeight * (j + 1);                            LONG lDrawHeight = lHeight;                            if( lDestBottom > rcDest.bottom ) {                                lDrawHeight -= lDestBottom - rcDest.bottom;                                lDestBottom = rcDest.bottom;                            }                            for( int i = 0; i < iTimesX; ++i ) {                                LONG lDestLeft = rcDest.left + lWidth * i;                                LONG lDestRight = rcDest.left + lWidth * (i + 1);                                LONG lDrawWidth = lWidth;                                if( lDestRight > rcDest.right ) {                                    lDrawWidth -= lDestRight - rcDest.right;                                    lDestRight = rcDest.right;                                }                                ::BitBlt(hDC, rcDest.left + lWidth * i, rcDest.top + lHeight * j, \                                    lDestRight - lDestLeft, lDestBottom - lDestTop, hCloneDC, \                                    rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, SRCCOPY);                            }                        }                    }                    else if( xtiled ) {                        LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;                        int iTimes = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;                        for( int i = 0; i < iTimes; ++i ) {                            LONG lDestLeft = rcDest.left + lWidth * i;                            LONG lDestRight = rcDest.left + lWidth * (i + 1);                            LONG lDrawWidth = lWidth;                            if( lDestRight > rcDest.right ) {                                lDrawWidth -= lDestRight - rcDest.right;                                lDestRight = rcDest.right;                            }                            ::StretchBlt(hDC, lDestLeft, rcDest.top, lDestRight - lDestLeft, rcDest.bottom,                                 hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \                                lDrawWidth, rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);                        }                    }                    else { // ytiled                        LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;                        int iTimes = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;                        for( int i = 0; i < iTimes; ++i ) {                            LONG lDestTop = rcDest.top + lHeight * i;                            LONG lDestBottom = rcDest.top + lHeight * (i + 1);                            LONG lDrawHeight = lHeight;                            if( lDestBottom > rcDest.bottom ) {                                lDrawHeight -= lDestBottom - rcDest.bottom;                                lDestBottom = rcDest.bottom;                            }                            ::StretchBlt(hDC, rcDest.left, rcDest.top + lHeight * i, rcDest.right, lDestBottom - lDestTop,                                 hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \                                rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, lDrawHeight, SRCCOPY);                                            }                    }                }            }                        // left-top            if( rcCorners.left > 0 && rcCorners.top > 0 ) {                rcDest.left = rc.left;                rcDest.top = rc.top;                rcDest.right = rcCorners.left;                rcDest.bottom = rcCorners.top;                rcDest.right += rcDest.left;                rcDest.bottom += rcDest.top;                if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                    rcDest.right -= rcDest.left;                    rcDest.bottom -= rcDest.top;                    ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                        rcBmpPart.left, rcBmpPart.top, rcCorners.left, rcCorners.top, SRCCOPY);                }            }            // top            if( rcCorners.top > 0 ) {                rcDest.left = rc.left + rcCorners.left;                rcDest.top = rc.top;                rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;                rcDest.bottom = rcCorners.top;                rcDest.right += rcDest.left;                rcDest.bottom += rcDest.top;                if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                    rcDest.right -= rcDest.left;                    rcDest.bottom -= rcDest.top;                    ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                        rcBmpPart.left + rcCorners.left, rcBmpPart.top, rcBmpPart.right - rcBmpPart.left - \                        rcCorners.left - rcCorners.right, rcCorners.top, SRCCOPY);                }            }            // right-top            if( rcCorners.right > 0 && rcCorners.top > 0 ) {                rcDest.left = rc.right - rcCorners.right;                rcDest.top = rc.top;                rcDest.right = rcCorners.right;                rcDest.bottom = rcCorners.top;                rcDest.right += rcDest.left;                rcDest.bottom += rcDest.top;                if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                    rcDest.right -= rcDest.left;                    rcDest.bottom -= rcDest.top;                    ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                        rcBmpPart.right - rcCorners.right, rcBmpPart.top, rcCorners.right, rcCorners.top, SRCCOPY);                }            }            // left            if( rcCorners.left > 0 ) {                rcDest.left = rc.left;                rcDest.top = rc.top + rcCorners.top;                rcDest.right = rcCorners.left;                rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;                rcDest.right += rcDest.left;                rcDest.bottom += rcDest.top;                if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                    rcDest.right -= rcDest.left;                    rcDest.bottom -= rcDest.top;                    ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                        rcBmpPart.left, rcBmpPart.top + rcCorners.top, rcCorners.left, rcBmpPart.bottom - \                        rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);                }            }            // right            if( rcCorners.right > 0 ) {                rcDest.left = rc.right - rcCorners.right;                rcDest.top = rc.top + rcCorners.top;                rcDest.right = rcCorners.right;                rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;                rcDest.right += rcDest.left;                rcDest.bottom += rcDest.top;                if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                    rcDest.right -= rcDest.left;                    rcDest.bottom -= rcDest.top;                    ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                        rcBmpPart.right - rcCorners.right, rcBmpPart.top + rcCorners.top, rcCorners.right, \                        rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);                }            }            // left-bottom            if( rcCorners.left > 0 && rcCorners.bottom > 0 ) {                rcDest.left = rc.left;                rcDest.top = rc.bottom - rcCorners.bottom;                rcDest.right = rcCorners.left;                rcDest.bottom = rcCorners.bottom;                rcDest.right += rcDest.left;                rcDest.bottom += rcDest.top;                if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                    rcDest.right -= rcDest.left;                    rcDest.bottom -= rcDest.top;                    ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                        rcBmpPart.left, rcBmpPart.bottom - rcCorners.bottom, rcCorners.left, rcCorners.bottom, SRCCOPY);                }            }            // bottom            if( rcCorners.bottom > 0 ) {                rcDest.left = rc.left + rcCorners.left;                rcDest.top = rc.bottom - rcCorners.bottom;                rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;                rcDest.bottom = rcCorners.bottom;                rcDest.right += rcDest.left;                rcDest.bottom += rcDest.top;                if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                    rcDest.right -= rcDest.left;                    rcDest.bottom -= rcDest.top;                    ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                        rcBmpPart.left + rcCorners.left, rcBmpPart.bottom - rcCorners.bottom, \                        rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, rcCorners.bottom, SRCCOPY);                }            }            // right-bottom            if( rcCorners.right > 0 && rcCorners.bottom > 0 ) {                rcDest.left = rc.right - rcCorners.right;                rcDest.top = rc.bottom - rcCorners.bottom;                rcDest.right = rcCorners.right;                rcDest.bottom = rcCorners.bottom;                rcDest.right += rcDest.left;                rcDest.bottom += rcDest.top;                if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {                    rcDest.right -= rcDest.left;                    rcDest.bottom -= rcDest.top;                    ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \                        rcBmpPart.right - rcCorners.right, rcBmpPart.bottom - rcCorners.bottom, rcCorners.right, \                        rcCorners.bottom, SRCCOPY);                }            }        }    }    ::SelectObject(hCloneDC, hOldBitmap);    ::DeleteDC(hCloneDC);}



使用duilib CTileLayoutUI 控件

<?xml version="1.0" encoding="utf-8"?><Window caption="0,0,0,30" roundcorner="5,5,5,5" sizebox="4,4,4,4" mininfo="600,320"> <Font name="宋体" size="13" bold="false"/> <Font name="宋体" size="12"/><Default name="VScrollBar" value="button1normalimage="file='scroll.png' source='0,0,16,16'" button1hotimage="file='scroll.png' source='0,0,16,16' mask='#FFFF00FF'" button1pushedimage="file='scroll.png' source='0,16,16,32' mask='#FFFF00FF'" button1disabledimage="file='scroll.png' source='0,0,16,16' mask='#FFFF00FF'" button2normalimage="file='scroll.png' source='0,32,16,48' mask='#FFFF00FF'" button2hotimage="file='scroll.png' source='0,32,16,48' mask='#FFFF00FF'" button2pushedimage="file='scroll.png' source='0,48,16,64' mask='#FFFF00FF'" button2disabledimage="file='scroll.png' source='0,32,16,48' mask='#FFFF00FF'" thumbnormalimage="file='scroll.png' source='0,64,16,80' corner='2,2,2,2' mask='#FFFF00FF'" thumbhotimage="file='scroll.png' source='0,64,16,80' corner='2,2,2,2' mask='#FFFF00FF'" thumbpushedimage="ffile='scroll.png' source='0,64,16,80' corner='2,2,2,2' mask='#FFFF00FF'" thumbdisabledimage="file='scroll.png' source='0,64,16,80' corner='2,2,2,2' mask='#FFFF00FF'" railnormalimage="file='scroll.png' source='0,80,16,96' corner='2,2,2,2' mask='#FFFF00FF'" railhotimage="file='scroll.png' source='0,80,16,96' corner='2,2,2,2' mask='#FFFF00FF'" railpushedimage="file='scroll.png' source='0,96,16,112' corner='2,2,2,2' mask='#FFFF00FF'" raildisabledimage="file='scroll.png' source='0,80,16,96' corner='2,2,2,2' mask='#FFFF00FF'" bknormalimage="file='scroll.png' source='0,128,16,146' corner='2,2,2,2' mask='#FFFF00FF'" bkhotimage="file='scroll.png' source='0,128,16,146' corner='2,2,2,2' mask='#FFFF00FF'" bkpushedimage="file='scroll.png' source='0,128,16,146' corner='2,2,2,2' mask='#FFFF00FF'" bkdisabledimage="file='scroll.png' source='0,128,16,146' corner='2,2,2,2' mask='#FFFF00FF'" " /><VerticalLayout bkcolor="#FF313C00"><HorizontalLayout height="37" inset="0,2,0,0" bkcolor="#FF2F006F"><VerticalLayout><Text text="ListDemo" textcolor="#FFFFFFFF" font="1"/></VerticalLayout><VerticalLayout width="70"><Button float="true" pos="0,0,28,17" name="minbtn" maxwidth="28" maxheight="17" normalimage="file='max_min.png' source='0,0,28,17'" hotimage="file='max_min_h.png' source='0,0,28,17'" pushedimage="file='max_min_h.png' source='0,0,28,17'"/><Button float="true" pos="28,0,56,17" name="closebtn" maxwidth="28" maxheight="17" normalimage="file='max_min.png' source='28,0,56,17'" hotimage="file='max_min_h.png' source='28,0,56,17'" pushedimage="file='max_min_h.png' source='28,0,56,17'"/></VerticalLayout></HorizontalLayout><HorizontalLayout height="60" inset="100,10,0,0" bkcolor="#FF00FFFF"><Button name="btn" text="Search" font="0" maxwidth="63" maxheight="23" normalimage="file='button.png' source='0,0,63,23'" hotimage="file='button.png' source='0,23,63,46'" pushedimage="file='button.png' source='0,23,63,46'"/></HorizontalLayout><TileLayout inset="10,1,10,10" name="title" childpadding="10" itemsize="200,0" vscrollbar="true" hscrollbar="false" />  </VerticalLayout></Window>

<?xml version="1.0" encoding="UTF-8"?><Window><Container width="200" height="200"><VerticalLayout><Button name="wstitle" height="100"/><Label height="100" textcolor="#FFDDDDDD" align="center" text="int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)" mouse="false"/></VerticalLayout></Container></Window>


使用

#include <fstream>#include <set>#include "..\DuiLib\UIlib.h"using namespace DuiLib;#ifdef _DEBUG#   ifdef _UNICODE#       pragma comment(lib, "..\\Lib\\DuiLib_ud.lib")#   else#       pragma comment(lib, "..\\Lib\\DuiLib_d.lib")#   endif#else#   ifdef _UNICODE#       pragma comment(lib, "..\\Lib\\DuiLib_u.lib")#   else#       pragma comment(lib, "..\\Lib\\DuiLib.lib")#   endif#endifclass ListMainForm : public WindowImplBase{public:    ListMainForm() {};~ListMainForm() {};virtual LPCTSTR    GetWindowClassName() const   {   return _T("DUIMainFrame");  }    virtual CDuiString GetSkinFile()                {   return _T("skin.xml");  }    virtual CDuiString GetSkinFolder()              {   return _T("");  }virtual void InitWindow()    {        m_pSearch = static_cast<CButtonUI*>(m_PaintManager.FindControl(_T("btn")));    }    void OnSearch()    {TCHAR szfile[1024];static int file_index = 1;if(file_index > 24){return;}sprintf_s(szfile, _countof(szfile), _T("D:\\123456\\%d.jpg"), file_index++);CDialogBuilder cbuilfer;CVerticalLayoutUI *pcontain = (CVerticalLayoutUI *)cbuilfer.Create(_T("listmen.xml"), 0, NULL, &m_PaintManager, NULL);if(!pcontain){return;}CButtonUI *pver = static_cast<CButtonUI *>(pcontain->FindSubControl(_T("wstitle")));if(!pver){return;}//pver->SetNormalImage(szfile);//pver->SetBkImage(szfile);static int nLenInfo(1024*1024);char *pInfo = new char[nLenInfo];int nLen(0);memset(pInfo, 0, nLenInfo);std::ifstream  readFile;readFile.open(szfile, std::ios::binary);char *pTemp = pInfo;unsigned int nPerLen(1024);char szTemp[1024];while(readFile && !readFile.eof()){memset(szTemp, 0, sizeof(szTemp));readFile.read(szTemp, nPerLen); nPerLen = readFile.gcount();memcpy(pTemp, szTemp, nPerLen);pTemp += nPerLen;nLen += nPerLen;}if(readFile) readFile.close();pver->SetBkImage(m_ImageList.Set(file_index, (PBYTE)pInfo, nLen));if(pInfo) delete []pInfo;CTileLayoutUI *ptitle = static_cast<CTileLayoutUI *>(m_PaintManager.FindControl(_T("title")));if(!ptitle){return;}ptitle->Add(pcontain);//m_setLayoutUI.insert(pcontain);}    void Notify(TNotifyUI& msg)    {        if( msg.sType == _T("click") )         {            if(msg.pSender == m_pSearch)            {                OnSearch();            }        }__super::Notify(msg);    }private:    CButtonUI* m_pSearch;CDuilibImageList m_ImageList;std::set<CHorizontalLayoutUI *> m_setLayoutUI;};int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow){     ::CoInitialize(NULL);    CPaintManagerUI::SetInstance(hInstance);    CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + _T("skin\\List"));    ListMainForm *pFrame = new ListMainForm();    pFrame->Create(NULL, _T("list"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE | WS_EX_ACCEPTFILES);    pFrame->CenterWindow();    pFrame->ShowModal();    delete pFrame;    ::CoUninitialize();    return 0;}



0 0
原创粉丝点击