我的对话框用SetWindowRgn函数已经透明了,但是我继承的STATIC控件中使用此函数为什么不能透明??我的要求是对话框要透明,而且上面的控件也要透明.哪位高手赐教赐教??

解决方案 »

  1.   

    你需要知道透明的原理。免费的控件CButtonST可以帮你理解。实现透明的方法是很多的,主要的原理是颜色的运算,BitBlt函数的用法和Raster的使用方法,前景图和背景图用不同的运算可以实现不同效果,让需要显示的内容显示,不需要显示的内容不显示。
    给你一段代码。
    canvasFrame::canvasFrame()
    {
    Create(NULL,"绘图窗口");
    CClientDC dc(this);
    int width = dc.GetDeviceCaps(HORZRES);
    int height = dc.GetDeviceCaps(VERTRES);
    GetWindowRect( &rect );
    width = ( width - ( rect.right - rect.left ))/2 ;
    height = (height - (rect.bottom - rect.top ))/2 ;
    MoveWindow( width , height , (rect.right - rect.left ) , (rect.bottom - rect.top ) ,true);
    GetClientRect(&rect); 
    mdc = new CDC;        //建立一暂存DC mdc
    bitmap = new CBitmap; //建立源位图
    bgbmp = new CBitmap;  //建立背景图
    mdc->CreateCompatibleDC(&dc); //将mdc转换成与dc兼容的DC;非常重要。
    bgbmp->m_hObject = (HBITMAP)::LoadImage(NULL,"bground.bmp",IMAGE_BITMAP,rect.right,rect.bottom,LR_LOADFROMFILE); //加载背//景图案
    bitmap->m_hObject = (HBITMAP)::LoadImage(NULL,"man.bmp",IMAGE_BITMAP,208,154,LR_LOADFROMFILE); //加载源位图图案
    }另外,给你推荐一本参考书,visualc++ 动感设计,电子工业出版社。
      

  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里加上
    SetBackTransparent(this);
    如果要连非客户区都透明,用
    SetBackTransparent(this, FALSE);  
     
      

  3.   

    第一步 定义功能typedef BOOL (FAR PASCAL * FUNC1)(
      HWND hwnd,           // handle to the layered window
      COLORREF crKey,      // specifies the color key
      BYTE bAlpha,         // value for the blend function
      DWORD dwFlags        // action
    );
    第二步 实现代码在OnInitDialog中加入下列代码(如果在SDI里面,应该是在OnCreat里面添加).....
     HMODULE hModule = GetModuleHandle("user32.dll");
     FUNC1 SetLayeredWindowAttributes;
     SetLayeredWindowAttributes =  (FUNC1) GetProcAddress (hModule, _T( "SetLayeredWindowAttributes" ) ); // 设置分层扩展标记
     SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE) | 0x80000L);
     // 70% alpha
     SetLayeredWindowAttributes(GetSafeHwnd(), 0, (255 * 70) / 100, 0x2);
    工作完成,怎么样,现在你可以运行你的程序来查看效果,即使背景变化也能立刻反映到你的窗口当中,这一点比金山词霸的效果要好。 看到下面这段文章的人有福了,这是新加入的:第三步:如何除去透明选项? // 除去分层扩展标记
    SetWindowLong(GetSafeHwnd(),, GWL_EXSTYLE,
            GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE) & ~ 0x80000L);
     // 重画窗口
    RedrawWindow();
      

  4.   

    只有rosestrom(ztsoon)比较接近,其它的都不是很对啊..
    我不只只要窗口或对话框透明,我还要求它上面的控件也能透明啊..现在如果按上面的方法,对话框可以透明,但是控件透明不了啊...
      

  5.   

    typedef BOOL (FAR PASCAL * TFUNC)(
      HWND hwnd,           // 窗体句柄
      COLORREF crKey,      // 颜色值
      BYTE bAlpha,         // Alpha值0--255
      DWORD dwFlags        // action
    );
    .............
    HMODULE hModule = GetModuleHandle("user32.dll");
    TFUNC SetLayeredWindowAttributes;
    SetLayeredWindowAttributes =  (FUNC1) GetProcAddress (hModule, _T
                                           ( "SetLayeredWindowAttributes" ) );SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(GetSafeHwnd(), 
                                           GWL_EXSTYLE) | 0x80000L);
     // nAlpha设置透明值
    SetLayeredWindowAttributes(GetSafeHwnd(), 0, nAlpha, 0x2);