void CMainWindow::OnBtn_Image(TNotifyUI& msg) { if (NULL == m_pSendEdit) return; LPCTSTR lpszFilter = _T("图像文件(*.bmp;*.jpg;*.jpeg;*.gif;*.png)\0*.bmp;*.jpg;*.jpeg;*.gif;*.png\0\0");; CFileDialogEx fileDlg; fileDlg.SetFilter(lpszFilter); fileDlg.SetParentWnd(m_hWnd); if (fileDlg.ShowOpenFileDlg()) { _RichEdit_InsertFace(m_pSendEdit, fileDlg.GetPathName().c_str(), -1, -1); m_pSendEdit->SetFocus(); } }
int S3FExportController::DoExport(void) { //этот класс необходим чтобы изменять дефаултное расширение в зависимости от выбранного фильтра class CFileDialogEx : public CFileDialog { public: CFileDialogEx() : CFileDialog(FALSE,_T("s3f"), NULL, NULL, _T("SECU-3 Format Files (*.s3f)|*.s3f|All Files (*.*)|*.*||"), NULL) {}; virtual ~CFileDialogEx() {}; protected: virtual void OnTypeChange( ) { if (m_ofn.nFilterIndex==1) m_ofn.lpstrDefExt = _T("s3f"); } }; CFileDialogEx save; if (save.DoModal()==IDOK) { //сохраняет имя файла выбранного пользователем для сохранения m_s3f_file_name = _TSTRING(save.GetFileName()); int id = mp_view->DoModal(); if (id == IDOK) { bool result = mp_s3f_io->Save(_TSTRING(save.GetPathName())); if (!result) { AfxMessageBox(MLL::LoadString(IDS_CANT_SAVE_THIS_FILE),MB_OK|MB_ICONWARNING); return IDCANCEL; } } return id; } else return IDCANCEL; //canceled by user }
// “另存为”菜单 void CMainWindow::OnMenu_SaveAs(TNotifyUI& msg) { if (msg.pSender != m_pSendEdit && msg.pSender != m_pRecvEdit) return; ITextServices * pTextServices = ((CRichEditUI *)msg.pSender)->GetTextServices(); IImageOle * pImageOle = NULL; BOOL bRet = RichEdit_GetImageOle(pTextServices, m_ptRBtnDown, &pImageOle); pTextServices->Release(); if (!bRet || NULL == pImageOle) return; tstring strFileName; BSTR bstrFileName = NULL; HRESULT hr = pImageOle->GetFileName(&bstrFileName); if (SUCCEEDED(hr)) strFileName = bstrFileName; if (bstrFileName != NULL) ::SysFreeString(bstrFileName); LPCTSTR lpszDefExt; LPCTSTR lpszFileName = _T("未命名"); DWORD dwFlags = OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR|OFN_EXTENSIONDIFFERENT; LPCTSTR lpszFilter; HWND hWndParent = m_hWnd; GUID guid = {0}; hr = pImageOle->GetRawFormat(&guid); if (InlineIsEqualGUID(guid, Gdiplus::ImageFormatJPEG)) { lpszDefExt = _T(".jpg"); lpszFilter = _T("图像文件(*.jpg)\0*.jpg\0图像文件(*.bmp)\0*.bmp\0\0"); } else if (InlineIsEqualGUID(guid, Gdiplus::ImageFormatPNG)) { lpszDefExt = _T(".png"); lpszFilter = _T("图像文件(*.png)\0*.png\0\0"); } else if (InlineIsEqualGUID(guid, Gdiplus::ImageFormatGIF)) { lpszDefExt = _T(".gif"); lpszFilter = _T("图像文件(*.gif)\0*.gif\0图像文件(*.jpg)\0*.jpg\0图像文件(*.bmp)\0*.bmp\0\0"); } else { lpszDefExt = _T(".jpg"); lpszFilter = _T("图像文件(*.jpg)\0*.jpg\0图像文件(*.bmp)\0*.bmp\0\0"); } CFileDialogEx fileDlg; fileDlg.SetDefExt(lpszDefExt); fileDlg.SetFileName(lpszFileName); fileDlg.SetFlags(dwFlags); fileDlg.SetFilter(lpszFilter); fileDlg.SetParentWnd(m_hWnd); fileDlg.SetTitle(_T("保存图片")); if (fileDlg.ShowSaveFileDlg()) { tstring strSavePath = fileDlg.GetPathName(); tstring strExtName = _T(".") + ZYM::CPath::GetExtension(strSavePath.c_str()); GUID guid2 = GetFileTypeGuidByExtension(strExtName.c_str()); if (InlineIsEqualGUID(guid, guid2)) { CopyFile(strFileName.c_str(), strSavePath.c_str(), FALSE); } else { BSTR bstrSavePath = ::SysAllocString(strSavePath.c_str()); if (bstrSavePath != NULL) { pImageOle->SaveAsFile(bstrSavePath); ::SysFreeString(bstrSavePath); } } } if (pImageOle != NULL) pImageOle->Release(); }
bool ImageViewForm::Notify(ui::EventArgs* msg) { if (msg->Type == kEventClick) { std::wstring control_name = msg->pSender->GetName(); if (control_name == L"close_btn") { this->Close(); } else if (control_name == L"close_btn_max") { this->Close(); } else if (msg->pSender->GetName() == L"preimage_btn") { ImageViewManager::GetInstance()->ShowNextImageView(false); } else if (msg->pSender->GetName() == L"nextimage_btn") { ImageViewManager::GetInstance()->ShowNextImageView(true); } else if (control_name == L"normal_btn") { ui_image_view_->SetOriginalSize(); ShowImageScale(true); } else if (control_name == L"max_btn") { ::ShowWindow(m_hWnd, SW_MAXIMIZE); } else if (control_name == L"restore_btn") { ::ShowWindow(m_hWnd, SW_RESTORE); } else if (control_name == L"magnify_btn") { ui_image_view_->ScalePic(UiImageView::kMagnifyPic); ShowImageScale(true); } else if (control_name == L"reduce_btn") { ui_image_view_->ScalePic(UiImageView::kReducePic); ShowImageScale(true); } else if (control_name == L"rotate_btn") { ui_image_view_->RotatePic(UiImageView::kClockWise); } else if (msg->pSender->GetName() == L"save_btn") { if (!image_path_.empty()) { std::wstring extension; nbase::FilePathExtension(image_path_, extension); if (extension.empty()) { std::wstring mime_type; GetMimeType(image_path_, mime_type, extension); } nbase::Time::TimeStruct now = nbase::Time::Now().ToTimeStruct(true); std::wstring file_name = nbase::StringPrintf(L"IMG%04d%02d%02d_%02d%02d%02d", now.year(), now.month(), now.day_of_month(), now.hour(), now.minute(), now.second()); file_name += extension; std::wstring file_type = ui::MutiLanSupport::GetInstance()->GetStringViaID(L"STRING_PIC_FILE"); std::wstring info = nbase::StringPrintf(L"%s(*%s)", file_type.c_str(), extension.c_str()); CFileDialogEx* fileDlg = new CFileDialogEx(); std::wstring filter = L"*" + extension; std::map<LPCTSTR, LPCTSTR> filters; filters[info.c_str()] = filter.c_str(); fileDlg->SetFilter(filters); fileDlg->SetFileName(file_name.c_str()); fileDlg->SetParentWnd(m_hWnd); CFileDialogEx::FileDialogCallback2 callback2 = nbase::Bind(&ImageViewForm::SaveImageDlgCallback, this, std::placeholders::_1, std::placeholders::_2); fileDlg->AyncShowSaveFileDlg(callback2); } } } return true; }
int MPSZExportController::DoExport(void) { //этот класс необходим чтобы изменять дефаултное расширение в зависимости от выбранного фильтра class CFileDialogEx : public CFileDialog { public: CFileDialogEx() : CFileDialog(FALSE,_T("mpx"), NULL, NULL, _T("MPSZ Files (*.mpx)|*.mpx|MPSZ-II Files (*.mpx)|*.mpx|MPSZ Files (*.mpz)|*.mpz|All Files (*.*)|*.*||"), NULL) {}; virtual ~CFileDialogEx() {}; protected: virtual void OnTypeChange( ) { if (m_ofn.nFilterIndex==1) m_ofn.lpstrDefExt = _T("mpx"); if (m_ofn.nFilterIndex==2) m_ofn.lpstrDefExt = _T("mpx"); if (m_ofn.nFilterIndex==3) m_ofn.lpstrDefExt = _T("mpz"); } }; CFileDialogEx save; if (save.DoModal()==IDOK) { MPSZFileDataIO::EFileTypes type; if (save.GetOFN().nFilterIndex == 1) //mpx { type = MPSZFileDataIO::FILE_TYPE_MPX; mp_mpsz_io->SetActualSetsNumber(MPSZ_NUMBER_OF_MAPS); } if (save.GetOFN().nFilterIndex == 2) //mpx MPSZ II { type = MPSZFileDataIO::FILE_TYPE_MPXv2; mp_mpsz_io->SetActualSetsNumber(MPSZ_NUMBER_OF_MAPS); } else if (save.GetOFN().nFilterIndex == 3) //mpz { type = MPSZFileDataIO::FILE_TYPE_MPZ; mp_mpsz_io->SetActualSetsNumber(MPSZ_NUMBER_OF_MAPS_IN_MPZ_FILE); } else { type = MPSZFileDataIO::FILE_TYPE_MPX; //если у файла нет расширения или оно другое то по умолчанию mpx mp_mpsz_io->SetActualSetsNumber(MPSZ_NUMBER_OF_MAPS); } //сохраняет имя файла выбранного пользователем для сохранения m_mpsz_file_name = _TSTRING(save.GetFileName()); int id = mp_view->DoModal(); if (id == IDOK) { bool result = mp_mpsz_io->Save(_TSTRING(save.GetPathName()),type); if (!result) { AfxMessageBox(MLL::LoadString(IDS_CANT_SAVE_THIS_FILE),MB_OK|MB_ICONWARNING); return IDCANCEL; } } return id; } else return IDCANCEL; //отказ пользователя }