[英]How to make background in MFC translucent?
我將圖片(.bmp或.jpeg)作為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 );
}
使用AlphaBlend代替StretchBlt
將BLENDFUNCTION結構中的SourceConstantAlpha設置為128(透明和不透明之間的一半),然后進行調整,直到看起來不錯為止。
除非您使用帶Alpha通道的32位位圖,否則AlphaFormat應該為零。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.