画自定义尺寸的圆角背景
2012-01-29 22:09 Clingingboy 阅读(729) 评论(0) 编辑 收藏 举报
首先准备一个png的圆角背景图
1.分三部分画,可以是”上,中,下”,也可以是“左,中,右”
注意的是圆角的宽度和高度
2.绘制的时候不要忘记设置ImageAttributes的绘制模式,这样就可以画无限的高度和宽度
ImageAttributes imgAtt;
imgAtt.SetWrapMode(WrapModeTileFlipY);
绘制效果
源码Demo
void DrawMargin(Graphics& g, Image* pImage, Rect& rcDst, Rect& rcSrc, int nMarginLeft, int nMarginRight,
int nMarginTop, int nMarginBottom, BOOL bDrawCenter/* = TRUE*/)
{
//ASSERT(pImage);
int nImgWidth = pImage->GetWidth();
int nImgHeight = pImage->GetHeight();
Rect rcImage(0, 0, nImgWidth, nImgHeight);
//ASSERT(rcImage.Contains(rcSrc));
//ASSERT((rcSrc.Width > (nMarginLeft + nMarginRight)) && (rcSrc.Height > (nMarginTop + nMarginBottom)));
if((rcDst.Width <= (nMarginLeft + nMarginRight)) || (rcDst.Height <= (nMarginTop + nMarginBottom)))
g.DrawImage(pImage, rcDst, rcSrc.X, rcSrc.Y, rcSrc.Width, rcSrc.Height, UnitPixel);
ImageAttributes imgAtt;
//上
if(nMarginTop)
{
if(nMarginLeft)
{
Rect rcTopLeft(rcDst.X, rcDst.Y, nMarginLeft, nMarginTop);
g.DrawImage(pImage, rcTopLeft, rcSrc.X, rcSrc.Y, nMarginLeft, nMarginTop, UnitPixel);
}
Rect rcTop(rcDst.X + nMarginLeft, rcDst.Y, rcDst.Width - nMarginLeft - nMarginRight, nMarginTop);
imgAtt.SetWrapMode(WrapModeTileFlipX);
g.DrawImage(pImage, rcTop, rcSrc.X + nMarginLeft, rcSrc.Y, rcSrc.Width - nMarginLeft - nMarginRight, nMarginTop, UnitPixel, &imgAtt);
if(nMarginRight)
{
Rect rcTopRight(rcDst.GetRight() - nMarginRight, rcDst.Y, nMarginRight, nMarginTop);
g.DrawImage(pImage, rcTopRight, rcSrc.GetRight() - nMarginRight, rcSrc.Y, nMarginRight, nMarginTop, UnitPixel);
}
}
//中
if(nMarginLeft)
{
Rect rcLeft(rcDst.X, rcDst.Y + nMarginTop, nMarginLeft, rcDst.Height - nMarginTop - nMarginBottom);
imgAtt.SetWrapMode(WrapModeTileFlipY);
g.DrawImage(pImage, rcLeft, rcSrc.X, rcSrc.Y + nMarginTop, nMarginLeft, rcSrc.Height - nMarginTop - nMarginBottom, UnitPixel, &imgAtt);
}
if(bDrawCenter)
{
Rect rcCenter(rcDst.X + nMarginLeft, rcDst.Y + nMarginTop, rcDst.Width - nMarginLeft - nMarginRight, rcDst.Height - nMarginTop - nMarginBottom);
imgAtt.SetWrapMode(WrapModeTileFlipXY);
g.DrawImage(pImage, rcCenter, rcSrc.X + nMarginLeft, rcSrc.Y + nMarginTop, rcSrc.Width - nMarginLeft - nMarginRight, rcSrc.Height - nMarginTop - nMarginBottom, UnitPixel, &imgAtt);
}
if(nMarginRight)
{
Rect rcRight(rcDst.GetRight() - nMarginRight, rcDst.Y + nMarginTop, nMarginRight, rcDst.Height - nMarginTop - nMarginBottom);
imgAtt.SetWrapMode(WrapModeTileFlipY);
g.DrawImage(pImage, rcRight, rcSrc.GetRight() - nMarginRight, rcSrc.Y + nMarginTop, nMarginRight, rcSrc.Height - nMarginTop - nMarginBottom, UnitPixel, &imgAtt);
}
//下
if(nMarginTop)
{
if(nMarginLeft)
{
Rect rcBottomLeft(rcDst.X, rcDst.GetBottom() - nMarginBottom, nMarginLeft, nMarginBottom);
g.DrawImage(pImage, rcBottomLeft, rcSrc.X, rcSrc.GetBottom() - nMarginBottom, nMarginLeft, nMarginBottom, UnitPixel);
}
Rect rcBottom(rcDst.X + nMarginLeft, rcDst.GetBottom() - nMarginBottom, rcDst.Width - nMarginLeft - nMarginRight, nMarginBottom);
imgAtt.SetWrapMode(WrapModeTileFlipX);
g.DrawImage(pImage, rcBottom, rcSrc.X + nMarginLeft, rcSrc.GetBottom() - nMarginBottom, rcSrc.Width - nMarginLeft - nMarginRight, nMarginBottom, UnitPixel, &imgAtt);
if(nMarginRight)
{
Rect rcBottomRight(rcDst.GetRight() - nMarginRight, rcDst.GetBottom() - nMarginBottom, nMarginRight, nMarginBottom);
g.DrawImage(pImage, rcBottomRight, rcSrc.GetRight() - nMarginRight, rcSrc.GetBottom() - nMarginBottom, nMarginRight, nMarginBottom, UnitPixel);
}
}
}
void DrawMargin1(Graphics& g, Image* pImage, Rect& rcDst, int nMarginLeft, int nMarginRight, int nMarginTop, int nMarginBottom)
{
int nImgWidth = pImage->GetWidth();
int nImgHeight = pImage->GetHeight();
Rect rcSrc(0, 0, nImgWidth, nImgHeight);
DrawMargin(g, pImage, rcDst, rcSrc, nMarginLeft, nMarginRight, nMarginTop, nMarginBottom, TRUE);
}