为什么两个控件里的图片是一样?
void CDemoDlg::OnPaint()
{
CPaintDC dc(this);
DrawImage(60,130,&dc);
DrawImage(530,130,&dc);
}
//第一个按钮
void CDemoDlg::OnBnClickedButton1()
{
CString szFilter= _T("JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|GIF Files (*.gif)|*.gif|All Files (*.*)|*.*||");
CFileDialog dlg(TRUE,_T("jpg|gif"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if(dlg.DoModal() == IDOK)
{
CString strPathName1 = dlg.GetPathName();
LoadJPGFile((LPSTR)(LPCTSTR)strPathName1);
Invalidate();//显示图片
}
}
//第二个按钮
void CDemoDlg::OnBnClickedButton2()
{
CString szFilter= _T("JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|GIF Files (*.gif)|*.gif|All Files (*.*)|*.*||");
CFileDialog dlg(TRUE,_T("jpg|gif"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if(dlg.DoModal() == IDOK)
{
CString strPathName1 = dlg.GetPathName();
LoadJPGFile((LPSTR)(LPCTSTR)strPathName1);
Invalidate();//显示图片
}
}
//加载JPG文件到内存
BOOL CDemoDlg::LoadJPGFile(const char *pPathname)
{
CFile file;
if(!file.Open((LPCTSTR)pPathname, CFile::modeRead))
return FALSE;
m_nFileLen=file.GetLength();
HGLOBAL hMem = ::GlobalAlloc( GMEM_MOVEABLE, m_nFileLen );
LPVOID lpBuf = ::GlobalLock( hMem );
if(file.Read(lpBuf, m_nFileLen )!=m_nFileLen)
return FALSE;
file.Close();
m_pJPGBuffer = (char *)lpBuf;
::GlobalUnlock( hMem );
if ( CreateStreamOnHGlobal( hMem, TRUE, &pStream ) !=S_OK )
return FALSE;
if ( OleLoadPicture(pStream, m_nFileLen, TRUE, IID_IPicture, ( LPVOID * )&pPicture ) !=S_OK )
return FALSE;
return TRUE;
}
//在屏幕上显示JPG图像
void CDemoDlg::DrawImage(int x, int y, CDC *pDC)
{
if (pPicture != NULL)
{
long nWidth,nHeight;
pPicture->get_Width( &nWidth );
pPicture->get_Height( &nHeight );
//图片缩小3/2原大显示
CSize sz( nWidth, nHeight );
pDC->HIMETRICtoDP( &sz );
pPicture->Render(pDC->m_hDC,x,y,sz.cx/1.5,sz.cy/1.5,0,nHeight,nWidth,-nHeight,NULL);
}
}