VC实现对话框客户区透明!

来源:互联网 发布:爱淘宝流氓软件 编辑:程序博客网 时间:2024/05/17 07:55

这2个函数很是神奇,能让窗口客户区透明,真正的透明,像被美工刀抠掉一样,可以穿过窗口点击后面的图标或者按钮。


BOOL CALLBACK EnumChildFunc(HWND hwnd, LPARAM lParam)
{
        CRgn *pRgn = (CRgn*)lParam;
        CRect rcChild;
        ::GetWindowRect(hwnd, rcChild);
        CRgn rgnChild;
        CRgn rgnCopy;
        rgnCopy.CreateRectRgn(0, 0, 1, 1);
        rgnCopy.CopyRgn(pRgn);
        rgnChild.CreateRectRgn(rcChild.left,  rcChild.top,  
                rcChild.right, rcChild.bottom);
        pRgn->CombineRgn(&rgnCopy, &rgnChild, RGN_OR);
        return TRUE;
}


int SetBackTransparent(CWnd *pWnd, BOOL bClientOnly = TRUE)
{
        CRgn rgn;
        if(bClientOnly)
        {
                CRgn rgnWindow, rgnClient;  
                CRect rcWindow, rcClient, rcRgn;
                pWnd->GetWindowRect(rcWindow);  
                pWnd->GetClientRect(rcClient);  
                pWnd->ClientToScreen(rcClient);  
                rgnWindow.CreateRectRgn(rcWindow.left, rcWindow.top,    
                        rcWindow.right,  rcWindow.bottom);  
                rgnClient.CreateRectRgn(rcClient.left, rcClient.top,    
                        rcClient.right,  rcClient.bottom);  
                rgn.CreateRectRgn(0, 0, 1, 1);
                rgn.CombineRgn(&rgnWindow,  &rgnClient, RGN_DIFF);
        } else {
                rgn.CreateRectRgn(0, 0, 0, 0);
        }
        ::EnumChildWindows(pWnd->GetSafeHwnd(), (WNDENUMPROC)EnumChildFunc,(LPARAM)&rgn);  
        return pWnd->SetWindowRgn(rgn, TRUE);
}


调用办法:


  在CYourDialog::OnInitDialog 里加上    //也就是MFC的×××::OnInitDialog函数(×××代表具体的工程名)
  SetBackTransparent(this);
  如果要连非客户区都透明,用
  SetBackTransparent(this,  FALSE);

以上两个函数一定要写在OnInitDialog()之前,不然会报错;这两个函数在MFC的默认包含文件里已经声明过了,调用前无需再声明,此处应该只是重写了这两个函数。


示例代码:https://pan.baidu.com/s/1qYbP77A

原创粉丝点击