头文件中
CBitmap m_bitmap1;
CRect rect;
ondraw中
//打开位图文件
if(m_bitmap1.m_hObject)
{
CDC dcImage;
if(!dcImage.CreateCompatibleDC(pDC))
MessageBox(NULL,"wrong",NULL);
BITMAP bm;
m_bitmap1.GetBitmap(&bm);
dcImage.SelectObject(&m_bitmap1);
pDC->BitBlt(0,50,bm.bmWidth,bm.bmHeight,&dcImage,0,0,SRCCOPY);
}
void CMyBView::OnFileOpen()
{
// TODO: Add your command handler code here
CFileDialog filedlg1(TRUE,"*.bmp","位图文件",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"(*.bmp)|*.bmp|(*.*)|*.*||",NULL);
if(filedlg1.DoModal()==IDOK)
{
CString filename;//filename!=NULL&&
filename= filedlg1.GetFileName();
if(filedlg1.GetFileExt( )=="bmp")
{
HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,_T(filename),IMAGE_BITMAP,
0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
if(m_bitmap1.m_hObject)m_bitmap1.DeleteObject();
m_bitmap1.Attach(hBitmap);
Invalidate(TRUE );
}
else MessageBox("Not BMP files!");
}
}
在文件中点击打开
弹出一个文件对话框 打开bmp位图文件并在视图中显示的程序段