📄 facebrowse.cpp
字号:
str_time.Format("%d",m_keeptimenumber);
::WritePrivateProfileString("Face","m_timetype",str_time,files_path);
::ShowWindow(AfxGetMainWnd()->GetSafeHwnd(),SW_HIDE);
CBitmap bmp;
switch(m_keepfacenumber)
{
case 0:
bmp.LoadBitmap(IDB_BIT1);
m_nNumX=10;
m_nNumY=119;
break;
case 1:
bmp.LoadBitmap(IDB_BIT2);
m_nNumX=8;
m_nNumY=124;
break;
case 2:
bmp.LoadBitmap(IDB_BIT3);
m_nNumX=8;
m_nNumY=124;
break;
case 3:
bmp.LoadBitmap(IDB_BIT4);
m_nNumX=8;
m_nNumY=125;
break;
case 4:
bmp.LoadBitmap(IDB_BIT5);
m_nNumX=8;
m_nNumY=122;
break;
case 5:
bmp.LoadBitmap(IDB_BIT6);
m_nNumX=8;
m_nNumY=122;
break;
case 6:
bmp.LoadBitmap(IDB_BIT7);
m_nNumX=7;
m_nNumY=108;
break;
case 7:
bmp.LoadBitmap(IDB_BIT8);
m_nNumX=8;
m_nNumY=120;
break;
case 8:
bmp.LoadBitmap(IDB_BIT9);
m_nNumX=9;
m_nNumY=104;
break;
case 9:
bmp.LoadBitmap(IDB_BIT10);
m_nNumX=8;
m_nNumY=125;
break;
case 10:
bmp.LoadBitmap(IDB_BIT11);
m_nNumX=13;
m_nNumY=121;
break;
case 11:
bmp.LoadBitmap(IDB_BIT12);
m_nNumX=8;
m_nNumY=122;
break;
}
HRGN rgn;
rgn = BitmapToRegion((HBITMAP)bmp, RGB(255, 0, 255));
::SetWindowRgn(AfxGetMainWnd()->GetSafeHwnd(),rgn, TRUE);
bmp.DeleteObject();
::ShowWindow(AfxGetMainWnd()->GetSafeHwnd(),SW_SHOW);
}
CDialog::OnCancel();
}
HRGN CFaceBrowse::BitmapToRegion(HBITMAP hBmp, COLORREF cTransparentColor, COLORREF cTolerance)
{
HRGN hRgn = NULL;
if (hBmp)
{
HDC hMemDC = CreateCompatibleDC(NULL);//向系统申请内存设备环境句柄
if (hMemDC)
{
BITMAP bm;
GetObject(hBmp, sizeof(bm), &bm);//得到指定的图形对象的信息
//创建一个32位色的位图,并选进内存设备环境,
BITMAPINFOHEADER RGB32BITSBITMAPINFO = {sizeof(BITMAPINFOHEADER),bm.bmWidth,bm.bmHeight,1,32,BI_RGB,0,0,0,0,0};
VOID * pbits32;
//创建一个可以直接写的与设备无关点位图的应用
HBITMAP hbm32 = CreateDIBSection(hMemDC,(BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pbits32, NULL, 0);
if (hbm32)
{
HBITMAP holdBmp = (HBITMAP)SelectObject(hMemDC, hbm32);
// Create a DC just to copy the bitmap into the memory DC(device context )
HDC hDC = CreateCompatibleDC(hMemDC);
if (hDC)
{
// Get how many bytes per row we have for the bitmap bits (rounded up to 32 bits)
BITMAP bm32;
GetObject(hbm32, sizeof(bm32), &bm32);
while (bm32.bmWidthBytes % 4)
bm32.bmWidthBytes++;
// Copy the bitmap into the memory DC
HBITMAP holdBmp = (HBITMAP)SelectObject(hDC, hBmp);
BitBlt(hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY);
// For better performances(性能,执行), we will use the ExtCreateRegion() function to create the
// region. This function take a RGNDATA structure on entry(登入,进入). We will add rectangles(长方形)
// by amount of ALLOC_UNIT number in this structure.
#define ALLOC_UNIT 100
DWORD maxRects = ALLOC_UNIT;
//GlobalAlloc从指定的堆中分配指定的大小的字节
HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects));
//锁住全局内存对象,并返回一个指向内存对象块的第一个字节的指针
RGNDATA *pData = (RGNDATA *)GlobalLock(hData);
pData->rdh.dwSize = sizeof(RGNDATAHEADER);// RGNDATAHEADER rdh;
pData->rdh.iType = RDH_RECTANGLES;
pData->rdh.nCount = pData->rdh.nRgnSize = 0;
SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
// Keep on hand highest and lowest values for the "transparent" pixels
BYTE lr = GetRValue(cTransparentColor);
BYTE lg = GetGValue(cTransparentColor);
BYTE lb = GetBValue(cTransparentColor);
BYTE hr = min(0xff, lr + GetRValue(cTolerance));
BYTE hg = min(0xff, lg + GetGValue(cTolerance));
BYTE hb = min(0xff, lb + GetBValue(cTolerance));
// Scan each bitmap row from bottom to top (the bitmap is inverted vertically)
BYTE *p32 = (BYTE *)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;
for (int y = 0; y < bm.bmHeight; y++)
{
// Scan each bitmap pixel from left to right
for (int x = 0; x < bm.bmWidth; x++)
{
// Search for a continuous range of "non transparent pixels"
int x0 = x;
LONG *p = (LONG *)p32 + x;
while (x < bm.bmWidth)
{
BYTE b = GetRValue(*p);
if (b >= lr && b <= hr)
{
b = GetGValue(*p);
if (b >= lg && b <= hg)
{
b = GetBValue(*p);
if (b >= lb && b <= hb)
// This pixel is "transparent"
break;
}
}
p++;
x++;
}
if (x > x0)
{
// Add the pixels (x0, y) to (x, y+1) as a new rectangle in the region
if (pData->rdh.nCount >= maxRects)
{
GlobalUnlock(hData);
maxRects += ALLOC_UNIT;
hData = GlobalReAlloc(hData, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), GMEM_MOVEABLE);
pData = (RGNDATA *)GlobalLock(hData);
}
RECT *pr = (RECT *)&pData->Buffer;
SetRect(&pr[pData->rdh.nCount], x0, y, x, y+1);
if (x0 < pData->rdh.rcBound.left)
pData->rdh.rcBound.left = x0;
if (y < pData->rdh.rcBound.top)
pData->rdh.rcBound.top = y;
if (x > pData->rdh.rcBound.right)
pData->rdh.rcBound.right = x;
if (y+1 > pData->rdh.rcBound.bottom)
pData->rdh.rcBound.bottom = y+1;
pData->rdh.nCount++;
// On Windows98, ExtCreateRegion() may fail if the number of rectangles is too
// large (ie: > 4000). Therefore, we have to create the region by multiple steps.
if (pData->rdh.nCount == 2000)
{
HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
if (hRgn)
{
CombineRgn(hRgn, hRgn, h, RGN_OR);
DeleteObject(h);
}
else
hRgn = h;
pData->rdh.nCount = 0;
SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
}
}
}
// Go to next row (remember, the bitmap is inverted vertically)
p32 -= bm32.bmWidthBytes;
}
// Create or extend the region with the remaining rectangles
HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
if (hRgn)
{
CombineRgn(hRgn, hRgn, h, RGN_OR);
DeleteObject(h);
}
else
hRgn = h;
// Clean up
GlobalFree(hData);
SelectObject(hDC, holdBmp);
DeleteDC(hDC);
}
DeleteObject(SelectObject(hMemDC, holdBmp));
}
DeleteDC(hMemDC);
}
}
return hRgn;
}
void CFaceBrowse::OnPaint()
{
HBITMAP l_hbmpBitmap = m_ctrlbitmap.GetBitmap();
if( l_hbmpBitmap == NULL )
{
Default();
return;
}
CPaintDC l_PaintDC( this );
// Prepare everything for drawing
CRect l_rcClient;
GetClientRect(&l_rcClient);
CDC l_BufferDC;
l_BufferDC.CreateCompatibleDC(&l_PaintDC);//向系统申请内存设备描述表
CBitmap l_BufferBitmap ;
l_BufferBitmap.CreateCompatibleBitmap(&l_PaintDC,l_rcClient.Width(), l_rcClient.Height() ) ;
CBitmap* l_pOldBufferBitmap = l_BufferDC.SelectObject( &l_BufferBitmap ) ;
CDC l_MaskDC;
l_MaskDC.CreateCompatibleDC( &l_PaintDC );
CBitmap l_MaskBitmap;
l_MaskBitmap.CreateBitmap( l_rcClient.Width(), l_rcClient.Height(), 1, 1, NULL );//creates a monochrome单色 bitmap
CBitmap* l_pOldMaskBitmap = l_MaskDC.SelectObject( &l_MaskBitmap );
// #define SRCMASK 0x00220326
// Fill with transparent color
l_BufferDC.FillSolidRect( &l_rcClient, RGB( 255, 0, 255 ) );
// Blit the bitmap to the buffer
CDC l_MemoryDC ;
l_MemoryDC.CreateCompatibleDC( &l_PaintDC );//向系统申请内存设备描述表
CBitmap* l_pOldMemoryBitmap = l_MemoryDC.SelectObject( CBitmap::FromHandle( l_hbmpBitmap ) );
l_BufferDC.BitBlt( 340, 100, l_rcClient.Width(), l_rcClient.Height(), &l_MemoryDC,0, 0, SRCCOPY );
l_MemoryDC.SelectObject( l_pOldMemoryBitmap );
//BOOL BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop );
//函数的参数:int x表示贴到目的地的左上角X坐标;int y表示/贴到目的地的左上角Y坐标;
//int nWidth表示贴到目的地的区域宽度;int nHeight表示贴到目的地的区域高度;
//CDC* pSrcDC表示存储源位图的设备描述表;int xSrc表示源位图的左上角X坐标;int ySrc表示源位图的左上角Y坐标;
//DWORD dwRop为柵格运算标志,一般我们选择SRCCOPY,直接拷贝源位图到目标
// Create the mask.
//绘制"透明"位图的关键是创建一个"掩码"位图(mask bitmap),"掩码"位图是一个单色位图,它是位图中图像的一个单色剪影
//先创建一个单色的Bitmap,装入mask DC,然后,以"SRCCOPY"的方式将装有位图的位图设备描述表绘制(BitBlt)到mask DC上。
//这样,mask DC的显示平面中的位图即是"掩码"位图。
COLORREF l_crOldBack = l_BufferDC.SetBkColor( RGB( 255, 0, 255 ) );
l_MaskDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_BufferDC,0, 0, SRCCOPY );
l_BufferDC.SetBkColor( l_crOldBack );
// Draw the bitmap transparently now;
CDC l_CopyDC;
l_CopyDC.CreateCompatibleDC( &l_PaintDC );
CBitmap l_CopyBitmap;
l_CopyBitmap.CreateCompatibleBitmap( &l_PaintDC, l_rcClient.Width(),l_rcClient.Height() );
CBitmap* l_pOldCopyBitmap = l_CopyDC.SelectObject( &l_CopyBitmap ) ;
// 绘制"透明"位图的实际操作步骤
// 1位图设备描述表以"SRCINVERT"的方式绘制(BitBlt)到显示设备描述表上;
// 2"掩码"位图设备描述表以"SRCAND"的方式绘制(BitBlt)到显示设备描述表上;
// 3再将位图设备描述表以"SRCINVERT"的方式绘制(BitBlt)到显示设备描述表上。
// 这样除"透明色"外的其余位图部分(图像部分)就被绘制到窗口上了。
l_CopyDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_PaintDC,0, 0, SRCCOPY ) ;
l_CopyDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_MaskDC,0, 0, SRCAND ) ;
l_BufferDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_MaskDC,0, 0, SRCINVERT ) ;
l_CopyDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_BufferDC,0, 0, SRCPAINT ) ;
l_PaintDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_CopyDC,0, 0, SRCCOPY ) ;
l_CopyDC.SelectObject( l_pOldCopyBitmap ) ;
// Clean up.
l_MaskDC.SelectObject( l_pOldMaskBitmap ) ;
l_BufferDC.SelectObject( l_pOldBufferBitmap ) ;
}
void CFaceBrowse::OnDestroy()
{
CDialog::OnDestroy();
if(m_ctrlbitmap.GetBitmap()!=NULL)
DeleteObject(m_ctrlbitmap.GetBitmap());
if(m_timetypebitmap.GetBitmap()!=NULL)
DeleteObject(m_timetypebitmap.GetBitmap());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -