HRGN CLaucherApp::BitmaoToRegion(HBITMAP _hBitMap)
{
BITMAP * bitMap; // BITMAP 对象,用于储存BITMAP信息
BYTE * BMP; // BMP数组,储存BITMAP的RGB信息数组
POINT * leftPoint; // 左 POINT数组,储存获得的点信息
POINT * rightPoint; // 右POINT数组,储存获得的点信息
POINT * point; // POINT 数组,储存获得的点信息
int index = 0; // POINT数组索引
int bmpIndex = 0; // BMP数组索引
COLORREF color; // 透明颜色值
bitMap = new BITMAP();
GetObject(_hBitMap, sizeof(BITMAP),bitMap);
int bytesPix = bitMap->bmBitsPixel/8;
int count = bitMap->bmHeight*bitMap->bmWidth*bytesPix;
BMP = new BYTE[count];
leftPoint = new POINT[bitMap->bmHeight];
rightPoint = new POINT[bitMap->bmHeight];
GetBitmapBits(_hBitMap,count, BMP);
// 设置左上角第一个像素颜色为默认透明色
color = RGB(BMP[2],BMP[1],BMP[0]);
// 左边轮廓扫描
for (int row=0; row<bitMap->bmHeight; row +=1)
{
for (int col=0; col<bitMap->bmWidth*bytesPix; col+=4)
{
bmpIndex = row*bitMap->bmWidth*bytesPix + col;
if(BMP[bmpIndex] != GetBValue(color) || BMP[bmpIndex+1] != GetGValue(color) || BMP[bmpIndex+2] != GetRValue(color))
{
leftPoint[index].x = (col/4)%bitMap->bmWidth;
leftPoint[index].y = row;
index++;
break;
}
}
}
// 右边轮廓扫描
index = 0;
for (int row=0; row<bitMap->bmHeight; row+=1)
{
for (int col=bitMap->bmWidth*bytesPix-1; col>=0; col-=4)
{
bmpIndex = row*bitMap->bmWidth*bytesPix + col;
if(BMP[bmpIndex-3] != GetBValue(color) || BMP[bmpIndex-2] != GetGValue(color) || BMP[bmpIndex-1] != GetRValue(color))
{
rightPoint[index].x = (col/4)%bitMap->bmWidth+1;
rightPoint[index].y = row;
index++;
break;
}
}
}
point = new POINT[index*2];
for(int i=0; i<index; i++)
{
point[i] = leftPoint[i];
point[i+index] = rightPoint[index-i-1];
}
HRGN hRgn = CreatePolygonRgn(point, index*2, ALTERNATE);
int start = 0;
int length = 0;
bool isStart = false;
for (int row=0; row<index; row +=1)
{
for (int col=leftPoint[row].x; col<rightPoint[row].x; col+=1)
{
bmpIndex = leftPoint[row].y*bitMap->bmWidth*bytesPix + col*bytesPix;
if(BMP[bmpIndex] == GetBValue(color) && BMP[bmpIndex+1] == GetGValue(color) && BMP[bmpIndex+2] == GetRValue(color))
{
if(!isStart)
{
isStart = true;
start = col;
}
length++;
}
else
{
if(isStart)
{
HRGN TempRgn = CreateRectRgn(
start%bitMap->bmWidth,
leftPoint[row].y,
(start+length)%bitMap->bmWidth,
leftPoint[row].y+1);
CombineRgn(hRgn, hRgn, TempRgn, RGN_XOR);
DeleteObject(TempRgn);
start = start+length;
length = 0;
isStart = false;
}
}
}
}
delete point;
delete leftPoint;
delete rightPoint;
delete BMP;
delete bitMap;
return hRgn;
}
根据bmp设置窗口轮廓
最新推荐文章于 2021-01-14 12:17:59 发布