如何从CMFCEditBrowseControl获取路径并复制到CEdit控件?
Hey there! Since you're new to MFC, let's break down what's wrong with your code and how to fix it, plus cover how to save files to your target location.
What's Wrong with Your Current Code
Your code has two key issues that are causing you to get the dialog title instead of the file path:
- You're not properly storing the pointer to
CMFCEditBrowseCtrlorCEditbefore calling their methods. Lines like(CMFCEditBrowseCtrl *) GetDlgItem(IDC_MFCEDITBROWSE1)->inFileCtrl;are invalid—inFileCtrlisn't a member ofCMFCEditBrowseCtrl, and you're not assigning the control pointer to a variable. - The
GetWindowTextandSetWindowTextcalls are actually running on the parent dialog (not the controls) because you didn't target the correct control pointers, hence why you're getting the dialog title.
Correct Code to Get Path & Copy to CEdit
Here's the fixed code to retrieve the path from CMFCEditBrowseCtrl and copy it to your CEdit box:
// Step 1: Get the path from CMFCEditBrowseCtrl CString strFilePath; CMFCEditBrowseCtrl* pBrowseControl = (CMFCEditBrowseCtrl*)GetDlgItem(IDC_MFCEDITBROWSE1); if (pBrowseControl != nullptr) { // GetWindowText retrieves the path displayed in the browse control's edit field pBrowseControl->GetWindowText(strFilePath); } // Step 2: Copy the path to CEdit CEdit* pEditControl = (CEdit*)GetDlgItem(IDC_EDIT2); if (pEditControl != nullptr) { pEditControl->SetWindowText(strFilePath); }
Key Notes:
- Always check if the control pointer is
nullptrbefore using it—this prevents crashes if the control can't be found. - Make sure
IDC_MFCEDITBROWSE1andIDC_EDIT2match the exact IDs set in your dialog resource editor.
How to Save a File to the Target Location
Once you have the source file path (from CMFCEditBrowseCtrl) and your target path (either from CEdit or a save dialog), you can use MFC's CFile class to handle the copy:
Option 1: Copy to a Predefined Target Path (from CEdit)
// Get target path from your CEdit CString strTargetPath; pEditControl->GetWindowText(strTargetPath); // Copy the source file to the target location if (!CFile::Copy(strFilePath, strTargetPath, TRUE)) { // TRUE = overwrite existing files; handle failure AfxMessageBox(_T("Failed to save file!")); } else { AfxMessageBox(_T("File saved successfully!")); }
Option 2: Let the User Choose the Target (Using CFileDialog)
If you want to let the user pick where to save the file, use a save dialog:
// Create a save file dialog CFileDialog saveDialog(FALSE, _T("txt"), _T("my_file"), OFN_OVERWRITEPROMPT, _T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"), this); if (saveDialog.DoModal() == IDOK) { // Get the chosen target path from the dialog CString strTargetPath = saveDialog.GetPathName(); // Perform the file copy if (!CFile::Copy(strFilePath, strTargetPath, TRUE)) { AfxMessageBox(_T("File save failed!")); } else { AfxMessageBox(_T("File saved successfully!")); } }
Bonus: Folder vs File Browse Mode
If your CMFCEditBrowseCtrl is set to browse for folders (not files), make sure you've configured it correctly in code:
pBrowseControl->SetMode(CMFCEditBrowseCtrl::ModeFolder);
This ensures GetWindowText returns a folder path instead of a file path.
内容的提问来源于stack exchange,提问作者Rohit Binjagermath

