0

I import a picture(.bmp or .jpeg) as the background of a client view in MFC.

When I click Open, function CDrawToolView::OnFileOpen() open a window to choose a picture, then I use ShowBitmap(CDC* pDC,CString strPicPath) and ShowPic(CDC* pDC,CString strPicPath) to load the picture as backgroud and adjust the size of client view to fit the picture.

I want to set the picture translucent, so the background looks softer. Could some one help me or give some suggestion, thanks.

Here is my code:

void CDrawToolView::ShowBitmap(CDC* pDC,CString strPicPath)
{        
    HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,strPicPath,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
    m_bitmap.Detach();
    m_bitmap.Attach(hBitmap);

    CRect rect;
    GetClientRect(&rect);

    CDC dcImage;
    if (!dcImage.CreateCompatibleDC(pDC))
    {
        return;
    }

    BITMAP bm;
    m_bitmap.GetBitmap(&bm);
    dcImage.SelectObject(&m_bitmap);
    pDC->StretchBlt(0,0,rect.right,rect.bottom,&dcImage,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY); 
}


void CDrawToolView::ShowPic(CDC* pDC,CString strPicPath)
{        
    if(!m_MyImage.IsNull())
        m_MyImage.Destroy();
    HRESULT hResult=m_MyImage.Load(strPicPath);
    int iWidth=m_MyImage.GetWidth();
    int iHeight=m_MyImage.GetHeight();
    m_MyImage.Draw(pDC->m_hDC,0,0,iWidth,iHeight);
    CRect client(0, 0, iWidth, iHeight);
    client.bottom=client.bottom+::GetSystemMetrics(SM_CYMENU)+::GetSystemMetrics(SM_CYEDGE)*2;
    client.right=client.right+::GetSystemMetrics(SM_CXEDGE)*2;
    CFrameWnd* pFrame = GetParentFrame(); 
    pFrame->CalcWindowRect(&client);
    int width  = client.Width();
    int height = client.Height();
    int y  = (::GetSystemMetrics(SM_CYSCREEN) - height) / 2 + 100;
    int x  = (::GetSystemMetrics(SM_CXSCREEN) - width) / 2;
    pFrame->SetWindowPos( NULL, x, y, width, height, SWP_NOACTIVATE | SWP_NOZORDER );
}
Qingyao Li
  • 173
  • 2
  • 11
  • Maybe this http://stackoverflow.com/questions/13684437/cwnd-with-transparent-background or this http://stackoverflow.com/questions/5564016/how-to-make-a-cstatic-control-mfc-transparent can help you. – RedX Oct 09 '13 at 10:52

1 Answers1

3

Use AlphaBlend instead of StretchBlt

CDC::AlphaBlend

Set SourceConstantAlpha in your BLENDFUNCTION struct to something like 128 (halfway between transparent and opaque), then adjust until it looks good.

AlphaFormat should be zero unless your usa a 32-bit bitmap with an alpha channel.

Christopher Oicles
  • 3,017
  • 16
  • 11