还在苦苦敲代码开发APP?你out啦! 试试积木搭建APP吧~

截图软件截图区域以外背景变灰的实现--AlphaBlend

来源:清泛原创     2016-06-07 13:55:52    人气:     我有话说( 0 人参与)

OnPaint()方法中: ------------画黑色背景---------------- COLORREF bgColor =...

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);
效果图:

AlphaBlend 透明背景

注:本文为本站或本站会员原创优质内容,版权属于原作者及清泛网所有,
欢迎转载,转载时须注明版权并添加来源链接,谢谢合作! (编辑:admin)
分享到: