void CBmpViewDlg::OnOpen() //打开按钮
{
// TODO: Add your control notification handler code here
CFileDialog pFiledlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "位图文件(*.bmp)|*.bmp|",NULL);
if(pFiledlg.DoModal())
{
m_strFile = pFiledlg.GetPathName();
}
if(m_strFile == "")
{
//没选择文件
return;
}
hbitmap = (HBITMAP)::LoadImage (::AfxGetInstanceHandle(), m_strFile,
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
DrawBmp();//画出来
}
void CBmpViewDlg::DrawBmp()
{
CRect rect;
CStatic* p_bitmap = (CStatic*)GetDlgItem(IDC_IMAGE);
p_bitmap->GetClientRect(&rect);
CWnd *pWnd = GetDlgItem(IDC_IMAGE);
CDC *pDC = pWnd->GetDC();
BITMAP m_Bm;
HBITMAP hOldBmp;
::GetObject(hbitmap, sizeof(m_Bm), &m_Bm);
CDC dcImage;
dcImage.CreateCompatibleDC(pDC);
hOldBmp = (HBITMAP)dcImage.SelectObject(hbitmap);
int x, y;
x = (rect.right - m_Bm.bmWidth) / 2;
y = (rect.bottom - m_Bm.bmHeight) / 2;
pDC->BitBlt(x, y, x + m_Bm.bmWidth, y + m_Bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
dcImage.SelectObject(hOldBmp);
}
hbitmap声明为类成员变量:
HBITMAP hbitmap;
这样能显示出来位图,但是显示出来的位图在窗口最小化后就没有了,并且在打开第二个位图后显示还有重叠,不知道问题出在了哪里。