Ejemplo n.º 1
1
void CChildView::OnFileSave()
{
	CString strFilter;
	CSimpleArray<GUID> aguidFileTypes;
	CImage *pImage;
	CString fmt;
	HRESULT hResult;
	INT_PTR nResult;

	pImage = m_pSurface->GetImage();
	ASSERT(pImage != NULL);
	hResult = pImage->GetExporterFilterString( strFilter, aguidFileTypes );
	if(FAILED(hResult)) {
		fmt.Format(IDS_ERROR_GETEXPORTERFILTER, hResult, _com_error(hResult).ErrorMessage());
		::AfxMessageBox(fmt);
		return;
	}

	CFileDialog dlg(FALSE, NULL, NULL, 
		OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, strFilter);
	dlg.m_ofn.nFilterIndex = m_nFilterSave;

	nResult = dlg.DoModal();
	if( nResult != IDOK ) {
		return;
	}
	m_nFilterSave = dlg.m_ofn.nFilterIndex;
	GUID guid = m_nFilterSave > 0 ? aguidFileTypes[m_nFilterSave-1] : GUID(GUID_NULL);
	CString strFileName = dlg.GetPathName();
	if (dlg.GetFileExt().IsEmpty()) {
		if (strFileName[strFileName.GetLength()-1] == '.') {
			strFileName = strFileName.Left(strFileName.GetLength()-1);
		}
		CString strExt;
		if (m_nFilterSave == 0) {
			strExt = _T(".jpg"); // default to JPEG
		}
		else {
			// Look up the first extension in the filters
			int nCount = (m_nFilterSave*2)-1;
			int nLeft = 0;
			while (nCount) {
				if (strFilter[nLeft++] == '|') {
					nCount--;
				}
			}
			ASSERT(nLeft < strFilter.GetLength());
			strExt = strFilter.Tokenize(_T(";|"), nLeft).MakeLower();
			strExt = ::PathFindExtension(strExt);
		}
		strFileName += strExt;
	}

	hResult = pImage->Save(strFileName, guid);
	if(FAILED(hResult)) {
		fmt.Format(IDS_ERROR_SAVE, hResult, _com_error(hResult).ErrorMessage());
		::AfxMessageBox(fmt);
	}
}