您的位置:首页 > 其它

VC窗体透明而控件不透明以及Static文本背景透明方法

2015-06-03 17:07 507 查看
优点:

1. Dialog 窗体完全透明。

2. 窗体上的控件不透明、
DC 绘制的图形不透明。

3. 拖动窗体上用 DC 绘制的图形可以移动窗体。

缺点:

1. 窗体设置透明使用是掩码颜色,所以在窗体上用
DC 绘图的过程当中如果采用了和透明掩码颜色相同的颜色将不会显示出来。

2. 如果将 Border 属性设置成了
NONE ,那么想通过响应 WM_NCHITEST 消息来实现窗体拖动就无法完成了。只能通过在窗体中增加
DC 绘图的方式来完成。

3. DC绘图时虽然支持半透明绘图,但是透明绘图区域不透明。

1、在OnInitDialog()或OnPaint()中加入:

vs2003以上

COLORREF maskColor = RGB(255,255,255);   //掩码颜色
SetWindowLong(this->GetSafeHwnd(),
             GWL_EXSTYLE, 
             GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)| 0x80000);  //设定窗体使用扩展模式 
SetLayeredWindowAttributes(maskColor,255,1);

vc6.0

SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(),GWL_EXSTYLE)|0x00080000);  
    HINSTANCE hInst = LoadLibrary(_T("User32.dll"));  
    if (hInst)  
    {  
        typedef BOOL (WINAPI *MyFun)(HWND,COLORREF,BYTE,DWORD);  
        MyFun myfun = NULL;  
        myfun = (MyFun)GetProcAddress(hInst, "SetLayeredWindowAttributes");  
        if (myfun) myfun(GetSafeHwnd(),RGB(255,255,255),255,1);  
	    FreeLibrary(hInst);  
   }

2、在重载的消息WM_CTLCOLOR中加入:(其中包括Static背景透明方法)

HBRUSH CMainDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if(nCtlColor == CTLCOLOR_DLG)   //此处设置为窗体透明,CTLCOLOR_DLG表示对话框
    	{
        CBrush *brush;
        brush = new CBrush(RGB(255,255,255));
        return (HBRUSH)(brush->m_hObject);
    	}
	if(nCtlColor == CTLCOLOR_DLG || nCtlColor == CTLCOLOR_BTN || nCtlColor ==  CTLCOLOR_STATIC)	//静态文本背景透明	 		 
	{   		 
		pDC->SetBkMode(TRANSPARENT); 
		pDC->SetTextColor(RGB(50,255,255)); 
		return (HBRUSH)GetStockObject(HOLLOW_BRUSH);    		 
	 }   
	// TODO: Return a different brush if the default is not desired
	return hbr;
}


原文链接http://my.oschina.net/ypimgt/blog/60951
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: