HRESULT _AfxCreateStreamOnFile(LPCTSTR pszPath, LPSTREAM* ppstm,LONG* plSize)
{
ASSERT(pszPath != NULL);
ASSERT(ppstm != NULL);
*ppstm = NULL;
if (plSize != NULL)
*plSize = 0;
CFileStatus fstatus;
CFile file;
HRESULT hr = E_FAIL;
LONG cb;
if (file.Open(pszPath, CFile::modeRead) &&
file.GetStatus(pszPath, fstatus) &&
((cb = fstatus.m_size) != -1)) {
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData = NULL;
if (hGlobal != NULL) {
if ((pvData = GlobalLock(hGlobal)) != NULL) {
BOOL bRead = (file.ReadHuge(pvData, cb) == (ULONG)cb);
GlobalUnlock(hGlobal);
if (bRead && SUCCEEDED(
hr = CreateStreamOnHGlobal(hGlobal,
TRUE, ppstm))) {
ASSERT(*ppstm != NULL);
if (plSize != NULL)
*plSize = fstatus.m_size;
hr = S_OK;
}
}
if (FAILED(hr))
GlobalFree(hGlobal);
}
else
hr = E_OUTOFMEMORY;
}
else
hr = E_ACCESSDENIED;
return hr;
}
void CVCDemoDlg::OnBitmapBUTTON()
{
CString strFilter, strTitle;
strFilter.LoadString(AFX_IDS_PICTUREFILTER);
strTitle.LoadString(AFX_IDS_PICTUREBROWSETITLE);
CFileDialog fdlg(TRUE, NULL, NULL,
OFN_FILEMUSTEXIST |
OFN_HIDEREADONLY |
OFN_PATHMUSTEXIST,
strFilter);
fdlg.m_ofn.lpstrTitle = strTitle;
int nResult = fdlg.DoModal();
SetFocus();
if (nResult != IDOK)
return;
CString strPath = fdlg.GetPathName();
LPSTREAM pstm = NULL;
LONG lSize;
HRESULT hr;
if (FAILED(hr = _AfxCreateStreamOnFile(strPath, &pstm, &lSize))) {
UINT idsText;
CString strText;
CString strCaption;
switch (GetScode(hr)) {
case E_OUTOFMEMORY:
idsText = AFX_IDP_PICTURETOOLARGE;
break;
case E_ACCESSDENIED:
idsText = AFX_IDP_PICTURECANTOPEN;
break;
default:
idsText = AFX_IDP_PICTUREREADFAILED;
break;
}
AfxFormatString1(strText, idsText, strPath);
strCaption.LoadString(AFX_IDS_PICTURE_PPG_CAPTION);
MessageBox(strText, strCaption, MB_OK | MB_ICONEXCLAMATION);
SetFocus();
return;
}
ASSERT(pstm != NULL);
LPPICTURE pPict;
if (SUCCEEDED(OleLoadPicture(pstm, lSize, FALSE, IID_IPicture,
(LPVOID *)&pPict))) {
ASSERT(pPict != NULL);
LPPICTUREDISP pPictDisp = NULL;
if(SUCCEEDED(pPict->QueryInterface(IID_IPictureDisp,
(LPVOID*)&pPictDisp))) {
ASSERT(pPictDisp != NULL);
MovingMap.SetMapPicture(pPictDisp);
pPictDisp->Release();
}
pPict->Release();
}
else {
CString strText;
CString strCaption;
AfxFormatString1(strText, AFX_IDP_PICTURECANTLOAD, strPath);
strCaption.LoadString(AFX_IDS_PICTURE_PPG_CAPTION);
MessageBox(strText, strCaption, MB_OK | MB_ICONEXCLAMATION);
SetFocus();
}
pstm->Release();
}