//參數 圖片路徑,窗口句柄,圖片寬,高
HRESULT ShowPic(char *lpstrFile,HWND hWnd,int nScrWidth,int nScrHeight)
{
HDC hDC_Temp=GetDC(hWnd);
IPicture *pPic;
IStream *pStm;
BOOL bResult;
HANDLE hFile=NULL;
DWord dwFileSize,dwByteRead;
//打開硬盤中的圖形文件
hFile=CreateFile(lpstrFile,GENERIC_READ,
FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile!=INVALID_HANDLE_VALUE)
{
dwFileSize=GetFileSize(hFile,NULL);//獲取文件字節數
if (dwFileSize==0xFFFFFFFF)
return E_FAIL;
}
else
{
return E_FAIL;
}
//分配全局存儲空間
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData = NULL;
if (hGlobal == NULL)
return E_FAIL;
if ((pvData = GlobalLock(hGlobal)) == NULL)//鎖定分配內存塊
return E_FAIL;
ReadFile(hFile,pvData,dwFileSize,&dwByteRead,NULL);//把文件讀入內存緩沖區
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
//裝入圖形文件
bResult=OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID*)&pPic);
if(FAILED(bResult))
return E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth;//圖片的真實寬度
OLE_YSIZE_HIMETRIC hmHeight;//圖片的真實高度
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
//將圖形輸出到屏幕上(有點像BitBlt)
bResult=pPic->Render(hDC_Temp,0,0,nScrWidth,nScrHeight,
0,hmHeight,hmWidth,-hmHeight,NULL);
pPic->Release();
CloseHandle(hFile);//關閉打開的文件
if (SUCCEEDED(bResult))
{
return S_OK;
}
else
{
return E_FAIL;
}
}