按钮剪裁函数//不明白什么情况下会出空间不足的情况if ( pRgnData->nCount >= cBlocks * MAXBUF ){

mirroatl196 2013-01-29 05:51:01


HRGN GdixConButton::CreateRgnFromBitmap(HBITMAP hBmp, COLORREF color)
{
if (!hBmp) return NULL;

BITMAP bm;
GetObject( hBmp, sizeof(BITMAP), &bm ); // get bitmap attributes

CDC dcBmp;
CDC *pDC = GetDC();
if (!pDC) return NULL;
dcBmp.CreateCompatibleDC(pDC); //Creates a memory device context for the bitmap
ReleaseDC(pDC);
HBITMAP hOldBitmap = (HBITMAP)dcBmp.SelectObject(hBmp); //selects the bitmap in the device context

const DWORD RDHDR = sizeof(RGNDATAHEADER);
const DWORD MAXBUF = 40; // size of one block in RECTs
// (i.e. MAXBUF*sizeof(RECT) in bytes)
LPRECT pRects;
DWORD cBlocks = 0; // number of allocated blocks

INT i, j; // current position in mask image
INT first = 0; // left position of current scan line
// where mask was found
bool wasfirst = false; // set when if mask was found in current scan line
bool ismask; // set when current color is mask color

// allocate memory for region data
RGNDATAHEADER* pRgnData = (RGNDATAHEADER*)new BYTE[ RDHDR + ++cBlocks * MAXBUF * sizeof(RECT) ];
memset( pRgnData, 0, RDHDR + cBlocks * MAXBUF * sizeof(RECT) );
// fill it by default
pRgnData->dwSize = RDHDR;
pRgnData->iType = RDH_RECTANGLES;
pRgnData->nCount = 0;
for ( i = 0; i < bm.bmHeight; i++ )
for ( j = 0; j < bm.bmWidth; j++ ){
// get color
ismask=(dcBmp.GetPixel(j,bm.bmHeight-i-1)!=color);
// place part of scan line as RECT region if transparent color found after mask color or
// mask color found at the end of mask image
if (wasfirst && ((ismask && (j==(bm.bmWidth-1)))||(ismask ^ (j<bm.bmWidth)))){
// get offset to RECT array if RGNDATA buffer
pRects = (LPRECT)((LPBYTE)pRgnData + RDHDR);
// save current RECT
pRects[ pRgnData->nCount++ ] = CRect( first, bm.bmHeight - i - 1, j+(j==(bm.bmWidth-1)), bm.bmHeight - i );
// if buffer full reallocate it
if ( pRgnData->nCount >= cBlocks * MAXBUF ){
LPBYTE pRgnDataNew = new BYTE[ RDHDR + ++cBlocks * MAXBUF * sizeof(RECT) ];
memcpy( pRgnDataNew, pRgnData, RDHDR + (cBlocks - 1) * MAXBUF * sizeof(RECT) );
delete pRgnData;
pRgnData = (RGNDATAHEADER*)pRgnDataNew;
}
wasfirst = false;
} else if ( !wasfirst && ismask ){ // set wasfirst when mask is found
first = j;
wasfirst = true;
}
}
dcBmp.SelectObject( hOldBitmap );
dcBmp.DeleteDC(); //release the bitmap

HRGN hRgn=CreateRectRgn(0, 0, 0, 0);
ASSERT( hRgn!=NULL );
pRects = (LPRECT)((LPBYTE)pRgnData + RDHDR);
for(i=0;i<(int)pRgnData->nCount;i++)
{
HRGN hr=CreateRectRgn(pRects[i].left, pRects[i].top, pRects[i].right, pRects[i].bottom);
VERIFY(CombineRgn(hRgn, hRgn, hr, RGN_OR)!=ERROR);
if (hr) DeleteObject(hr);
}
ASSERT( hRgn!=NULL );
delete pRgnData;
return hRgn;
}
...全文
57 3 点赞 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
mirro187_ 2013-01-31
cBlocks * MAXBUF是什么意思??? 大哥 别复制粘贴 网上的 我看过
引用 2 楼 sha_jinhao 的回复:
// if buffer full reallocate it if ( pRgnData->nCount >= cBlocks * MAXBUF ){ LPBYTE pRgnDataNew = new BYTE[ RDHDR + ++cBlocks * MAXBUF * sizeof(RECT) ]; ……
  • 打赏
  • 举报
回复
jimette 2013-01-29
// if buffer full reallocate it if ( pRgnData->nCount >= cBlocks * MAXBUF ){ LPBYTE pRgnDataNew = new BYTE[ RDHDR + ++cBlocks * MAXBUF * sizeof(RECT) ]; memcpy( pRgnDataNew, pRgnData, RDHDR + (cBlocks - 1) * MAXBUF * sizeof(RECT) ); delete pRgnData; pRgnData = (RGNDATAHEADER*)pRgnDataNew; } wasfirst = false; } else if ( !wasfirst && ismask ){ // set wasfirst when mask is found first = j; wasfirst = true; } 代码是你自己写的 具体做什么你应该最清楚 RGNDATAHEADER* pRgnData = (RGNDATAHEADER*)new BYTE[ RDHDR + ++cBlocks * MAXBUF * sizeof(RECT) ]; 这里是第一次申请空间 然后在循环里 pRects[ pRgnData->nCount++ ] = CRect( first, bm.bmHeight - i - 1, j+(j==(bm.bmWidth-1)), bm.bmHeight - i ); pRgnData->nCount一直在累加 所以要判断之前的空间是否够用 不够就重新分配 至于为什么这么做还是要问你自己啊 http://bbs.csdn.net/topics/390332312 提过了一次!
  • 打赏
  • 举报
回复
allenhiman 2013-01-29
代码好长 帮顶吧
  • 打赏
  • 举报
回复
相关推荐
发帖
界面
加入

1.5w+

社区成员

VC/MFC 界面
申请成为版主
帖子事件
创建了帖子
2013-01-29 05:51
社区公告
暂无公告