www.pudn.com > 多线程文件分割软件.rar > Win2KFileDialog.cpp
#include "stdafx.h" #include "Win2KFileDialog.h" #ifdef AFX_AUX_SEG #pragma code_seg(AFX_AUX_SEG) #endif #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include#include #include // common dialog APIs #include ///////////////////////////////////////////////////////////////////////////// // CWin2KFileDialog #define _countof(array) (sizeof(array)/sizeof(array[0])) IMPLEMENT_DYNAMIC(CWin2KFileDialog, CFileDialog) CWin2KFileDialog::CWin2KFileDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName, DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) : CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd) { m_ofn.Flags = NULL; m_ofn.Flags = dwFlags;//|OFN_ENABLEHOOK; m_pApp=NULL; m_pDoc=NULL; } BEGIN_MESSAGE_MAP(CWin2KFileDialog, CFileDialog) //{{AFX_MSG_MAP(CWin2KFileDialog) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP() int CWin2KFileDialog::DoModal() { ASSERT_VALID(this); DWORD nOffset = lstrlen(m_ofn.lpstrFile)+1; memset(m_ofn.lpstrFile+nOffset, 0, (m_ofn.nMaxFile-nOffset)*sizeof(TCHAR)); HWND hWndFocus = ::GetFocus(); BOOL bEnableParent = FALSE; m_ofn.hwndOwner = PreModal(); if((m_ofn.hwndOwner != NULL) && ::IsWindowEnabled(m_ofn.hwndOwner)) { bEnableParent = TRUE; ::EnableWindow(m_ofn.hwndOwner, FALSE); } _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); ASSERT(pThreadState->m_pAlternateWndInit == NULL); m_ofn.lpfnHook = NULL; m_ofn.lpTemplateName = NULL; int nResult; if(m_bOpenFileDialog) { nResult = ::GetOpenFileName(&m_ofn); } else { nResult = ::GetSaveFileName(&m_ofn); } if(nResult) { ASSERT(pThreadState->m_pAlternateWndInit == NULL); } pThreadState->m_pAlternateWndInit = NULL; // WINBUG: Second part of special case for file open/save dialog. if(bEnableParent) { ::EnableWindow(m_ofn.hwndOwner, TRUE); } if(::IsWindow(hWndFocus)) { ::SetFocus(hWndFocus); } PostModal(); return nResult ? nResult : IDCANCEL; } BOOL CWin2KFileDialog::DoPromptFileName(CString &szFileName, UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate) { CString szTitle; VERIFY(szTitle.LoadString(nIDSTitle)); if(m_pApp == NULL) { ASSERT(FALSE); } //set the m_pApp app point before you call this function ASSERT(m_pApp->m_pDocManager != NULL); m_ofn.Flags |= lFlags; CString szFilter(""),szDefault(""); if(nIDSTitle==AFX_IDS_SAVEFILE || nIDSTitle==AFX_IDS_SAVEFILECOPY) { POSITION pos = m_pApp->GetFirstDocTemplatePosition(); BOOL bFirst = TRUE; while (pos != NULL) { CDocTemplate* pTemplate = (CDocTemplate*)m_pApp->GetNextDocTemplate(pos); AppendFilterSuffix(szFilter, m_ofn, pTemplate, bFirst ? &szDefault : NULL); bFirst = FALSE; } } else { szFilter = "My File Format (*.mff)|*.mff|My File Format Files (*.mff)"; } m_ofn.nMaxCustFilter++; LPTSTR pch = szFilter.GetBuffer(0); // modify the buffer in place // MFC delimits with '|' not '\0' while ((pch = _tcschr(pch, '|')) != NULL) { *pch++ = '\0'; } m_ofn.lpstrFilter = szFilter; m_ofn.lpstrTitle = szTitle; m_ofn.lpstrFile = szFileName.GetBuffer(_MAX_PATH); m_ofn.hwndOwner = m_pApp->m_pMainWnd->GetSafeHwnd(); m_ofn.Flags &= OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;//|OFN_ENABLEHOOK; int nResult = DoModal(); szFileName.ReleaseBuffer(); return nResult == IDOK; } void CWin2KFileDialog::AppendFilterSuffix(CString& filter, OPENFILENAME& ofn, CDocTemplate* pTemplate, CString* pstrDefaultExt) { ASSERT_VALID(pTemplate); ASSERT_KINDOF(CDocTemplate, pTemplate); CString szFilterExt, szFilterName; if (pTemplate->GetDocString(szFilterExt, CDocTemplate::filterExt) && !szFilterExt.IsEmpty() && pTemplate->GetDocString(szFilterName, CDocTemplate::filterName) && !szFilterName.IsEmpty()) { // a file based document template - add to filter list ASSERT(szFilterExt[0] == '.'); if(pstrDefaultExt != NULL) { // set the default extension *pstrDefaultExt = ((LPCTSTR)szFilterExt) + 1; // skip the '.' ofn.lpstrDefExt = (LPTSTR)(LPCTSTR)(*pstrDefaultExt); ofn.nFilterIndex = ofn.nMaxCustFilter + 1; // 1 based number } // add to filter filter += szFilterName; ASSERT(!filter.IsEmpty()); // must have a file type name filter += (TCHAR)'\0'; // next string please filter += (TCHAR)'*'; filter += szFilterExt; filter += (TCHAR)'\0'; // next string please ofn.nMaxCustFilter++; } } void CWin2KFileDialog::SetAppPointer(CWinApp *pApp) { m_pApp=pApp; } BOOL CWin2KFileDialog::DoSave(LPCTSTR lpszPathName, BOOL bReplace) { if(m_pDoc == NULL) { ASSERT (FALSE); return FALSE; } CString szNewName = lpszPathName; if(szNewName.IsEmpty()) { CDocTemplate* pTemplate = m_pDoc->GetDocTemplate(); ASSERT(pTemplate != NULL); szNewName = m_pDoc->GetPathName(); if(bReplace && szNewName.IsEmpty()) { szNewName = m_pDoc->GetTitle(); // check for dubious filename int iBad = szNewName.FindOneOf(_T(" #%;/\\")); if(iBad != -1) { szNewName.ReleaseBuffer(iBad); } // append the default suffix if there is one CString strExt; if(pTemplate->GetDocString(strExt, CDocTemplate::filterExt) && !strExt.IsEmpty()) { ASSERT(strExt[0] == '.'); szNewName += strExt; } } if (DoPromptFileName(szNewName,bReplace ? AFX_IDS_SAVEFILE : AFX_IDS_SAVEFILECOPY, OFN_HIDEREADONLY | OFN_PATHMUSTEXIST, FALSE, pTemplate)) return FALSE; // don't even attempt to save } CWaitCursor wait; if(!m_pDoc->OnSaveDocument(szNewName)) { if(lpszPathName == NULL) { // be sure to delete the file TRY { CFile::Remove(szNewName); } CATCH_ALL(e) { TRACE0("Warning: failed to delete file after failed SaveAs.\n"); do { e->Delete(); }while(0); } END_CATCH_ALL } return FALSE; } // reset the szTitle and change the document name if(bReplace) { m_pDoc->SetPathName(szNewName); } return TRUE; // success } void CWin2KFileDialog::SetDocumentPointer(CDocument *pDoc) { m_pDoc = pDoc; }