0
我導入一張圖片(.bmp或.jpeg)作爲MFC中客戶端視圖的背景。如何使MFC中的背景半透明?
當我點擊Open
,功能CDrawToolView::OnFileOpen()
打開一個窗口,選擇照片,然後我用ShowBitmap(CDC* pDC,CString strPicPath)
和ShowPic(CDC* pDC,CString strPicPath)
加載圖片作爲背景與調整客戶視圖的大小以適應圖片。
我想設置圖片半透明,所以背景看起來更柔和。有人可以幫我或提出一些建議,謝謝。
這裏是我的代碼:
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);
}
也許這個http://stackoverflow.com/questions/13684437/cwnd-with-transparent-background或這個http://stackoverflow.com/questions/5564016/how-to-make-a-cstatic-control- mfc-transparent可以幫助你。 – RedX