VC窗口透明而窗口上的文字不透明:
1、第一步实现窗口透明而控件不透明:
运用这里提供的方法,可以实现背景颜色的透明。
其原理是使用SetLayeredWindowAttributes对对话框进行的Alpha的操作。
以MFC的对话框为例讲下具体方法:
(1)OnInitDialog()中
// TODO: 在此添加额外的初始化代码
COLORREF maskColor = RGB(255,0,0);//红色
SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
HINSTANCE hInst=LoadLibrary(_T("User32.DLL"));
if(hInst)
{
typedef BOOL (WINAPI * MYFUNC)(HWND , COLORREF,BYTE,DWORD);
MYFUNC fun=NULL;
fun=(MYFUNC)GetProcAddress(hInst,"SetLayeredWindowAttributes");
if(fun)fun(this->GetSafeHwnd(),maskColor,255,1);
FreeLibrary(hInst);
}
(2)在OnPaint中重绘,修改对话框背景色为红色
else
{
CRect rect;
CPaintDC dc(this);
GetClientRect(rect);
dc.FillSolidRect(rect,RGB(255,0,0));
CDialogEx::OnPaint();
}
2、实现静态文字框的背景透明,文字不透明:
VC中设置STATIC控件背景,比如透明
假设static控件ID为IDC_STATIC1:
然后重载对话框的WM_CTLCOLOR消息响应函数:
在OnCtlColor中添加如下代码:
HBRUSH CYourDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
switch(pWnd->GetDlgCtrlID())
{
case IDC_STATIC1:
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(0,0,0));
return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
default: break;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
这里主要是因为使用白色的话,会把菜单栏都透明了,无法响应鼠标拖拽对话框及关闭等。
欢迎光临 分享学习 (http://1314xuexi.com/) | Powered by Discuz! X3.3 |