现在有个MFC对话框工程,主窗口背景是GDI+画上去的一个图片;它有个子窗口附在上面,现在想把子窗口的背景和父窗口背景统一。
在子对话框的paint里这样处理:
===========================================================================================
CWnd *pParent = GetParent();
ASSERT_VALID(pParent);
CRect rect;
GetWindowRect(rect);
pParent->ScreenToClient(rect);
CDC *dcParent = pParent->GetDC();
ASSERT_VALID(dcParent); Bitmap cacheImage(rect.Width(), rect.Height()); // 缓冲画布
Graphics cacheGraphics(&cacheImage);
// 取父窗口背景
cacheGraphics.FromHDC(dcParent->m_hDC);
pParent->ReleaseDC(dcParent);
===========================================================================================
这样一来,子对话框的背景就是父窗口对应区域的图片了。但是太一致效果很难看。我想把子对话框的背景处理一下再画上去,比如用一个半透明的画刷和它叠加:
SolidBrush semiTransBrush(Color(128, 220, 236, 255));
cacheGraphics.SetCompositingQuality(CompositingQualityGammaCorrected);
cacheGraphics.FillRectangle(&semiTransBrush, 0, 0, rect.Width(), rect.Height());
然而绘图效率十分差,像拉幕一样(貌似用alpha的刷子都特别慢?)……
纠结了半天,发帖求一个好一点的方法。
谢谢!

解决方案 »

  1.   

    GDI和GDI+的效率相差近一个数量级……不过GDI处理半透明很费劲。
      

  2.   

    设置透明有个API函数,SetLayeredWindowAttributes
    在User32.DLL里面。
    //要使窗体透明必须修改窗体属性
    SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
    m_hInstance = LoadLibrary("User32.DLL"); 
    if(m_hInstance) 

    //取得SetLayeredWindowAttributes函数指针 
    m_pFunc=(TRANFUNC)GetProcAddress(m_hInstance, "SetLayeredWindowAttributes");
    m_pFunc(GetSafeHwnd(),0,128,2); 

    }
      

  3.   

    位图,还可以用AlphaBlend
    CBitmap bmpScr;
    bmpScr.LoadBitmap(IDB_BITMAP1);

    CRect rcClient;
    GetClientRect(&rcClient);
    CPaintDC dc(this);
    CDC dcScr;
    dcScr.CreateCompatibleDC(&dc);
    dcScr.SelectObject(&bmpScr);

    BLENDFUNCTION bm;
    bm.BlendOp=AC_SRC_OVER;
    bm.BlendFlags=0;
    bm.SourceConstantAlpha=100;
    bm.AlphaFormat=0;  
    AlphaBlend(dc.m_hDC,0,0,rcClient.Width(),rcClient.Height(),dcScr.m_hDC,0,0,rcClient.Width(),rcClient.Height(),bm);