如何在Aero玻璃窗上绘制不透明图像?

时间:2016-01-06 01:07:36

标签: c++ winapi mfc aero-glass

我使用DWM API通过调用DwmExtendFrameIntoClientArea来创建Aero玻璃窗口。

void CMainFrame::OnActivate(UINT nState,CWnd* pWndOther,BOOL bMinimized )
{
    CFrameWnd::OnActivate(nState,pWndOther,bMinimized);
    BOOL fDwmEnabled = FALSE;
    if (SUCCEEDED(DwmIsCompositionEnabled(&fDwmEnabled)))
    {
        if(nState == WA_ACTIVE )
        {
            MARGINS margins ={-1};
            HRESULT hr = DwmExtendFrameIntoClientArea(m_hWnd, &margins);
            if (!SUCCEEDED(hr))
               TRACE0("Failed to DwmExtendFrameIntoClientArea\n");
        }
    }
}

然后,我在窗口上绘制一个位图图像(我也尝试调用DrawThemeIcon和CImageList :: Draw来绘制图像)。

void CMainFrame::DisplayBitmap( CBitmap *p, CDC *pDC)
{
    CDC dcMemory;
    BITMAP bm;
    dcMemory.CreateCompatibleDC(pDC);
    dcMemory.SelectObject(p);
    p->GetBitmap(&bm);
    pDC->BitBlt(100,100,bm.bmWidth,bm.bmHeight,&dcMemory,0,0,SRCCOPY);
}

void CMainFrame::OnNcPaint(){
    CFrameWnd::OnNcPaint();
    CDC* dc = GetWindowDC();
    CRect rct;
    GetWindowRect(&rct);
    dc->FillSolidRect(0, 0, rct.right - rct.left, rct.bottom - rct.top, RGB(0, 0, 0));
    DisplayBitmap(&bmpBtn,dc);
    ReleaseDC(dc);
}

我发现图像丑陋且半透明。如何在Aero玻璃窗上绘制不透明图像? result

更新 我还需要一个能为我提供另一种解决方案而不使用GDI +库的人。谢谢!

1 个答案:

答案 0 :(得分:1)

解决方案1:

CMainFrame::CMainFrame()
{
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR           gdiplusToken;
    //Initialize GDI+.
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    m_pImage = m_pImage->FromFile (_T("lena.bmp"));
}

void CMainFrame::OnNcPaint(){
    CFrameWnd::OnNcPaint();
    CDC* dc = GetWindowDC();
    CRect rct;
    GetWindowRect(&rct);
    dc->FillSolidRect(0, 0, rct.right - rct.left, rct.bottom - rct.top, RGB(0, 0, 0));
    CPaintDC gdc(this);
    Graphics g(gdc);
    //I don't why the image will disappear sometimes when I move the window.
    g.DrawImage ( m_pImage, 0, 0 );
    ReleaseDC(dc);

}

结果: enter image description here