updatelayeredwindow无法正常工作

时间:2012-07-28 10:48:03

标签: c++ png gdi

我试图整天使用UpdateLayeredWindow,但它无法正常工作:(我把代码放在OnCreate中并加载一个png文件(由photoshop创建)和CImage。

int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    SetWindowLong(this->m_hWnd, GWL_STYLE, 0);
    SetWindowLong(this->m_hWnd, GWL_EXSTYLE, 0);
    SetWindowLong(this->m_hWnd, GWL_EXSTYLE, GetWindowLong(this->m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
    this->SetMenu(NULL);
    ::SetWindowPos(this->m_hWnd, HWND_TOP, 50, 50, 652, 492, SWP_SHOWWINDOW);

    CBitmap imageBitmap, *oldBitmap;
    CDC *dcWindow, dcMem;
    CRect wRect;
    CPoint wPos;
    CSize wSize;
    HBITMAP hbmp;
    BLENDFUNCTION bFunc;

    m_Image.Load(_T("Img/BG_Blue.png"));
    GetWindowRect(&wRect);
    dcWindow = GetWindowDC();
    imageBitmap.CreateCompatibleBitmap(dcWindow, wRect.Width(), wRect.Height());
    dcMem.CreateCompatibleDC(dcWindow);
    oldBitmap = dcMem.SelectObject(&imageBitmap);
    m_Image.Draw(dcMem.m_hDC, 0, 0, wRect.Width(), wRect.Height(), 0, 0, wRect.Width(), wRect.Height());
    wPos.x = 0; wPos.y = 0;
    wSize.cx = wRect.left; wSize.cy = wRect.bottom;
    bFunc.SourceConstantAlpha = 125;
    bFunc.BlendFlags = 0;
    bFunc.BlendOp = AC_SRC_OVER;
    bFunc.AlphaFormat = AC_SRC_ALPHA;
    UpdateLayeredWindow(dcWindow, &wPos, &wSize, &dcMem, &wPos, 0, &bFunc, ULW_ALPHA);
    //BitBlt(dcWindow->m_hDC, 0, 0, wRect.Width(), wRect.Height(), dcMem.m_hDC, 0, 0, SRCCOPY);
    DWORD error = GetLastError();
    dcMem.SelectObject(oldBitmap);
    return 0;
}

函数返回1,但屏幕上没有任何内容,只有starup菜单上的一个图标表示程序正在运行。我想知道png文件是否有问题...有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

dcWindow应该是屏幕的设备上下文而不是窗口。因此,请将其初始化为:dcWindow->Attach(::GetDC(NULL))而不是dcWindow = GetWindowDC();