MFC设置透明对话框
主要利用下面的函数
BOOL SetLayeredWindowAttributes(
HWND hwnd, // handle to the layered window 透明窗体的句柄
COLORREF crKey, // specifies the color key 颜色值
BYTE bAlpha, // value for the blend function 透明度,取值范围是[0,255]
DWORD dwFlags // action 透明方式,可以取两个值:
当取值为LWA_ALPHA时,crKey参数无效,bAlpha参数有效;
当取值为LWA_COLORKEY时,bAlpha参数有效,而窗体中的所有颜色为crKey的地方将变为透明。
LWA_ALPHA = 0x2
LWA_COLORKEY=0x1
);
这里我们将dwFlags 设置为LWA_COLORKEY,因为要实现全透明,而且要在窗口上绘图。
步骤:
1、 定义变量
CDC p_DC;
CBitmap* p_oldBitmap;
2、 在该对话框的OnInitDialog()中添加函数
CBitmap bitmap;
BITMAP bitInfo;
bitmap.LoadBitmap(IDB_BITMAP3);
//得到图片大小并调整窗口大小适应图片
bitmap.GetBitmap(&bitInfo);
CRect rect;
GetWindowRect(&rect);
rect.right = rect.left + bitInfo.bmWidth;//
rect.bottom = rect.top + bitInfo.bmHeight;
MoveWindow(rect);
//创建并保存DC
p_DC.CreateCompatibleDC(GetDC());
p_oldBitmap = p_DC.SelectObject(&bitmap);
//设置窗口掩码颜色和模式
//首先获得掩码颜色
COLORREF maskColor = p_DC.GetPixel(50,50);
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA 0x00000002
#define WS_EX_LAYERED 0x00080000
typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd,
COLORREF crKey, BYTE bAlpha,DWORD dwFlags);
lpfnSetLayeredWindowAttributes SetLayeredWindowAttributes;
HMODULE hUser32 = GetModuleHandle("user32.dll");
if (NULL!=hUser32)
{
SetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32,
"SetLayeredWindowAttributes");
SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(GetSafeHwnd(),maskColor, 255, LWA_COLORKEY);
FreeLibrary(hUser32);
}
3、 在该对话框的OnPaint()中添加函数
//////////////////////////////////////////////////////////////////////////
CDC * pDC = this->GetDC();
CRect rect1;
GetWindowRect(&rect1);
pDC->BitBlt(0,0,rect1.Width(),rect1.Height(),&p_DC,0,0,SRCCOPY);
参考了网上的一段代码,忘了是哪位写的,在此谢过!
https://m.sciencenet.cn/blog-379528-303208.html
上一篇:
世界顶级20家俱乐部年收入排行下一篇:
VC定时的两种方法