截图软件截图区域以外背景变灰的实现--AlphaBlend
清泛原创
OnPaint()方法中:
// ------------画黑色背景----------------
COLORREF bgColor = RGB(0, 0, 0);
char alpha = (255 * 30) / 100;
int screenX = GetSystemMetrics(SM_CXSCREEN);
int screenY = GetSystemMetrics(SM_CYSCREEN);
CDC memdc;
memdc.CreateCompatibleDC(&dc);
CBitmap bmp, *pOldBitmap;
bmp.CreateCompatibleBitmap(&dc, screenX, screenY);
pOldBitmap = memdc.SelectObject(&bmp);
// 拷贝背景位图到内存DC
memdc.BitBlt(0, 0, screenX, screenY, &dc, 0, 0, SRCCOPY);
CRgn rgnScreen, rgnTracker;
rgnScreen.CreateRectRgn(0, 0, screenX, screenY);
CRect rect = m_rectTracker.m_rect;
rect.DeflateRect(-1, -1, -1, -1);
rgnTracker.CreateRectRgn(rect.left, rect.top, rect.right, rect.bottom);
// 屏幕区域减去截图区域
rgnScreen.CombineRgn(&rgnScreen, &rgnTracker, RGN_DIFF);
CBrush brush;
brush.CreateSolidBrush(bgColor);
// 指定区域填充背景色
memdc.FillRgn(&rgnScreen, &brush);
brush.DeleteObject();
BLENDFUNCTION bf;
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = alpha;
bf.AlphaFormat = 0;
// 内存DC透明映射到绘图DC
dc.AlphaBlend(0,0,screenX,screenY,&memdc,0,0,screenX,screenY,bf);
memdc.SelectObject(pOldBitmap);
效果图:
上一篇:mfc对话框字体不正常?
下一篇:MFC OnKeyDown没反应,不响应键盘操作