void CTestDemoView::OnDraw(CDC* pDC)
{
CTestDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
m_sPath = _T("C://Angel Light.jpg");
IPicture* pPicture = NULL;
LPSTREAM pStream = NULL;
CFileStatus cFileStatus;
CFile cFile;
LONG nSize;
int nWidth = 0,nHeight = 0;
HGLOBAL hGlobal = NULL;
LPVOID lpData = NULL;
if ( cFile.Open(m_sPath,CFile::modeRead))
{
cFile.GetStatus(m_sPath,cFileStatus);
nSize = (long)cFileStatus.m_size;
hGlobal = ::GlobalAlloc(GMEM_MOVEABLE,nSize);//分配全局内存
if ((hGlobal != NULL) )
{
lpData = ::GlobalLock(hGlobal);
if ( lpData != NULL)
{
cFile.Read(lpData,nSize);
::GlobalUnlock(hGlobal);
::CreateStreamOnHGlobal(hGlobal,TRUE,&pStream);
//基于流创建文件
if( SUCCEEDED(::OleLoadPicture(pStream,nSize,TRUE,IID_IPicture,(LPVOID*)&pPicture)))
{
OLE_XSIZE_HIMETRIC hmWidth = 0;
OLE_YSIZE_HIMETRIC hmHeight = 0;
pPicture->get_Width(&hmWidth);
pPicture->get_Height(&hmHeight);
//转换宽度
nWidth = pDC->GetDeviceCaps(HORZRES);//物理显示宽度(以毫米为单位)
nHeight = pDC->GetDeviceCaps(VERTRES);//物理显示高度(以毫米为单位)
CRect rect;
this->GetClientRect(&rect);
nWidth = rect.Width();//图片显示的宽度
nHeight = rect.Height();//图片显示的高度
if ( FAILED(pPicture->Render(pDC->m_hDC,0,0,nWidth,nHeight,0,hmHeight,hmWidth,-hmHeight,NULL)))
{
AfxMessageBox(_T("Failed to render"),MB_OK);
}
pPicture->Release();
}
}
}
}
}