You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从CMFCEditBrowseControl获取路径并复制到CEdit控件?

Fixing CMFCEditBrowseControl Path Retrieval & File Saving in MFC

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 CMFCEditBrowseCtrl or CEdit before calling their methods. Lines like (CMFCEditBrowseCtrl *) GetDlgItem(IDC_MFCEDITBROWSE1)->inFileCtrl; are invalid—inFileCtrl isn't a member of CMFCEditBrowseCtrl, and you're not assigning the control pointer to a variable.
  • The GetWindowText and SetWindowText calls 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 nullptr before using it—this prevents crashes if the control can't be found.
  • Make sure IDC_MFCEDITBROWSE1 and IDC_EDIT2 match 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:35:44