📄 fileeditctrl.cpp
字号:
}
if (IDOK == dlgOpen.DoModal()) // Start the CFECFileDialog
{ // user clicked OK, enter files selected into edit control
CString szFileSeparator;
#if defined FEC_NORESOURCESTRINGS
szFileSeparator = FEC_IDS_SEPARATOR;
#else
szFileSeparator.LoadString(FEC_IDS_SEPARATOR);
#endif
ASSERT(szFileSeparator.GetLength() == 1); // must be one character only
szFileSeparator += _T(" ");
CString szPath;
POSITION pos = dlgOpen.GetStartPosition();
if (pos) // first file has complete path
szPath = dlgOpen.GetNextPathName(pos);
TCHAR lpstrName[_MAX_FNAME];
TCHAR lpstrExt[_MAX_EXT];
CString szTempPath;
while (pos)
{ // remaining files are name and extension only
szTempPath = dlgOpen.GetNextPathName(pos);
_tsplitpath(szTempPath, NULL, NULL, lpstrName, lpstrExt);
szPath += szFileSeparator + lpstrName + lpstrExt;
}
LRESULT result = 0;
CWnd *pParent = GetParent();
if (IsWindow(pParent))
{ // notify parent window that the control text is about to be updated
FEC_NOTIFY notify(this, FEC_NM_POSTBROWSE);
notify.pNewText = &szPath;
result = pParent->SendMessage (WM_NOTIFY, (WPARAM)GetDlgCtrlID(), (LPARAM)¬ify);
}
if (0 == result)
{ // update the window text
SetWindowText(szPath);
bReturnValue = TRUE;
}
}
else
{ // CFileDialog cancelled
CWnd *pParent = GetParent();
if (IsWindow(pParent))
{ // notify parent window
FEC_NOTIFY notify(this, FEC_NM_POSTBROWSE);
pParent->SendMessage (WM_NOTIFY, (WPARAM)GetDlgCtrlID(), (LPARAM)¬ify);
}
}
SetFocus(); // ensure focus returns to this control
return bReturnValue;
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::FillBuffers (protected member function)
// Fills the m_szFolder and m_Files member variables
//
// Parameters :
// None
//
// Returns :
// Nothing
//
// Note :
// The m_szFolder and m_Files member variables are used by the GetStartPosition()
// and GetNextPathName() functions to retrieve the file names entered by the user.
//
// If the user entered a folder, m_Files will contain the complete path for the
// folder, and m_szFolder will be empty
//
// If the user entered multiple files, m_szFolder will contain the drive and folder
// path of the first file entered, and m_Files will contain all the files. The files
// may contain any complete or relative paths. Any relative paths will be evaluated
// as being relative to the path contained in m_szFolder.
//
/////////////////////////////////////////////////////////////////////////////
void CFileEditCtrl::FillBuffers()
{
ASSERT(IsWindow(this)); // Control window must exist
#if defined FEC_NORESOURCESTRINGS
m_szFolder = FEC_IDS_SEPARATOR;
#else
m_szFolder.LoadString(FEC_IDS_SEPARATOR);
#endif
TCHAR chSeparator = m_szFolder[0]; // get the character used to seperate the files
m_szFolder.Empty(); // empty the buffers of old data
m_Files.RemoveAll(); // Empty();
int len = GetWindowTextLength();
if (!len) // no files entered, leave buffers empty
return;
LPTSTR lpstrWindow = new TCHAR[len + 1]; // working buffer
GetWindowText(lpstrWindow, len + 1);
LPTSTR lpstrStart = lpstrWindow; // points to the first character in a file name
LPTSTR lpstrEnd = NULL; // points to the next separator character
LPTSTR lpstrNext = NULL;
while (*lpstrStart == chSeparator || _istspace(*lpstrStart))
lpstrStart = CharNext(lpstrStart); // skip over leading spaces and separator characters
if (!*lpstrStart)
{ // no files entered, leave buffers empty
delete lpstrWindow;
return;
}
lpstrEnd = _tcschr(lpstrStart, chSeparator);// find separator character
if (lpstrEnd)
{
lpstrNext = CharNext(lpstrEnd);
*lpstrEnd = 0; // mark end of string
}
LPTSTR temp = lpstrStart + _tcslen(lpstrStart);// - 1;
temp = CharPrev(lpstrStart, temp);
while (_istspace(*temp)) // remove trailing white spaces from string
{
*temp = 0;
temp = CharPrev(lpstrStart, temp);
}
DWORD dwFlags = GetFlags();
CString File;
if (dwFlags & FEC_FOLDER)
{
_tfullpath(File.GetBuffer(_MAX_PATH), lpstrStart, _MAX_PATH); // get absolute path
File.ReleaseBuffer();
int len = File.GetLength();
if (dwFlags & FEC_TRAILINGSLASH) // add a trailing slash if it is not already there
{
if (_T('\\') != File[len - 1])
File += _T("\\");
}
else // remove the trailing slash if it is there
{
if (3 != len && _T('\\') == File[len - 1])
File.Delete(len - 1);
}
AddFile(File);
delete lpstrWindow;
return;
}
_TCHAR drive[_MAX_DRIVE];
_TCHAR folder[_MAX_DIR];
_TCHAR file[_MAX_FNAME];
_TCHAR ext[_MAX_EXT];
_tsplitpath(lpstrStart, drive, folder, file, ext);
m_szFolder = (CString)drive + folder; // drive and directory placed in m_szFolder
File = (CString)file + ext;
ExpandWildCards(File);
if (dwFlags & FEC_MULTIPLE)
{
lpstrStart = lpstrNext; // reset to the start of the next string
while (lpstrEnd)
{ // add the rest of the files as they have been typed (include any path information)
while (*lpstrStart == chSeparator || _istspace(*lpstrStart))
lpstrStart = CharNext(lpstrStart); // remove leading spaces and separator characters
if (!*lpstrStart) // last file was followed by spaces and/or separator characters,
break; // there are no more files entered
lpstrEnd = _tcschr(lpstrStart, chSeparator); // find next separator character
if (lpstrEnd)
{
lpstrNext = CharNext(lpstrEnd);
*lpstrEnd = 0; // mark end of string
}
temp = lpstrStart + _tcslen(lpstrStart);
temp = CharPrev(lpstrStart, temp);
while (_istspace(*temp)) // remove trailing white spaces from string
{
*temp = 0;
temp = CharPrev(lpstrStart, temp);
}
ExpandWildCards(lpstrStart);
lpstrStart = lpstrNext; // reset to the start of the next string
}
}
delete lpstrWindow; // delete working buffer
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::GetBrowseInfo (public member function)
// Retrieve a pointer to the BROWSEINFO structure
//
// Parameters :
// None
//
// Returns :
// A pointer to the BROWSEINFO structure if the FEC_FOLDER flag was set
// NULL otherwise
//
// Note :
// If the default SHBrowseForFolder settings do not fit your use, Use the pointer
// returned by this function to set up the SHBrowseForFolder using your own settings
//
/////////////////////////////////////////////////////////////////////////////
BROWSEINFO* CFileEditCtrl::GetBrowseInfo() const
{
return m_pBROWSEINFO;
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::GetButtonWidth (public member function)
// Retrieves the width, in pixels, of the browse button
//
// Parameters :
// None
//
// Returns :
// The width of the browse button
//
/////////////////////////////////////////////////////////////////////////////
int CFileEditCtrl::GetButtonWidth()
{
return m_rcButtonRect.Width();
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::GetFlags (public member function)
// Retrieves the current flags
//
// Parameters :
// None
//
// Returns :
// the current flags
//
// Note :
// See the FileEditCtrl.h file for descriptions of the flags used
// Because some flags can be changed via GetOpenFileName(), always use this
// function to get the current state of the flags. Do not use m_dwFlags directly.
//
/////////////////////////////////////////////////////////////////////////////
DWORD CFileEditCtrl::GetFlags()
{
DWORD Flags = m_dwFlags;
/* if (m_pCFileDialog)
{ // coordinate the FEC_* flags with the OFN_* flags
if (m_pCFileDialog->m_ofn.Flags & OFN_NODEREFERENCELINKS)
Flags |= FEC_NODEREFERENCELINKS;
else
Flags &= ~FEC_NODEREFERENCELINKS;
if (m_pCFileDialog->m_ofn.Flags & OFN_ALLOWMULTISELECT)
Flags |= FEC_MULTIPLE;
else
Flags &= ~FEC_MULTIPLE;
}*/
return Flags;
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::GetNextPathName (public member function)
// Returns the file name at the specified position in the buffer.
//
// Parameters :
// pos [in] - The position of the file name to retrieve
// [out] - the position of the next file name
//
// Returns :
// The complete path name of the file or folder
//
// Note :
// The starting position is retrieved using the GetStartPosition() function.
// pos will be set to NULL when there are no more files
//
/////////////////////////////////////////////////////////////////////////////
CString CFileEditCtrl::GetNextPathName(POSITION &pos)
{
ASSERT(pos); // pos must not be NULL
CString ReturnString;
CString Temp = m_Files.GetNext(pos);
if (0 == Temp.GetLength())
Temp = m_szFolder;
else if ((Temp.GetLength() > 1 && _T(':') != Temp[1]) // not drive
&& _T('\\') != Temp[0]) // nor current drive nor UNC
Temp.Insert(0, m_szFolder);
_tfullpath(ReturnString.GetBuffer(_MAX_PATH), Temp, _MAX_PATH); // get absolute path from any relative paths
ReturnString.ReleaseBuffer();
DWORD Flags = GetFlags();
if (Flags & FEC_FILE)
{
Temp = ReturnString.Right(4);
Temp.MakeLower();
if ((_T(".lnk") == Temp || _T(".pif") == Temp) && !(Flags & FEC_NODEREFERENCELINKS))
// resolve shortcuts (*.lnk or *.pif files)
DereferenceLink(ReturnString);
}
return ReturnString;
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::GetOpenFileName (public member function)
// Retrieves a pointer to the OPENFILENAME structure
//
// Parameters :
// None
//
// Returns :
// A pointer to the OPENFILENAME structure if the FEC_FILE flag was set
// NULL otherwise
//
// Note :
// If the default CFileDialog settings do not fit your use, Use the pointer
// returned by this function to set up the CFileDialog using your own settings
//
/////////////////////////////////////////////////////////////////////////////
OPENFILENAME* CFileEditCtrl::GetOpenFileName() const
{
// if (m_pCFileDialog)
// return (&m_pCFileDialog->m_ofn);
return NULL;
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::GetStartPosition (public member function)
// If the control is active, calls FillBuffers() if the text has changed
// returns the position of the first file in the buffers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -