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

MFC跨文件获取编辑框资源ID及调用控件内容的问题求助

How to Access an Edit Control's Resource ID and Text in a Separate .cpp File (MFC)

Hey there! Let's work through this MFC snag you're hitting—totally normal for new folks, so no stress. Let's break down what's off with your current code and walk through the fix step by step.

First, Let's Diagnose Your Code Issues

You've got two main missteps right now:

  1. You're misusing the GetDlgItemTextW function with your control variable
  2. You're trying to access the resource ID like it's a member of the dialog class (it's not!)

Step 1: Include the Required Headers

First up, in your MyNew.cpp file, you need to include the headers that define your main dialog class and the resource IDs. Add these at the top:

#include "MainMFCDlg.h"
#include "Resource.h" // This defines IDC_MYEDIT_EDIT; skip if MainMFCDlg.h already includes it

Without these, the compiler won't recognize CMainMFCDlg or IDC_MYEDIT_EDIT at all.

Step 2: Fix the Control Access Code

You have two simple, valid ways to get the edit control's text—both way cleaner than what you tried:

Method 1: Use the Associated Control Variable

Since you already linked m_myEdit_Edit to IDC_MYEDIT_EDIT, you can call GetWindowTextW directly on this variable—no need to pass the resource ID at all:

// Get main dialog pointer with a safety check (critical to avoid crashes!)
CMainMFCDlg* pMainDlg = dynamic_cast<CMainMFCDlg*>(AfxGetMainWnd());
if (pMainDlg == nullptr)
{
    // Handle the case where we can't access the main window (e.g., log an error)
    return;
}

CString myString;
// Grab text directly from the linked control variable
pMainDlg->m_myEdit_Edit.GetWindowTextW(myString);

Method 2: Use the Dialog's GetDlgItemTextW

If you want to use the resource ID directly (without the control variable), call the function on the dialog object instead of the control variable:

CMainMFCDlg* pMainDlg = dynamic_cast<CMainMFCDlg*>(AfxGetMainWnd());
if (pMainDlg == nullptr)
{
    return;
}

CString myString;
// Use the dialog's own GetDlgItemText with the resource ID
pMainDlg->GetDlgItemTextW(IDC_MYEDIT_EDIT, myString);

Why Your Original Code Failed

  • MainMFCDlg->m_myEdit_Edit.GetDlgItemTextW(*IDC_MYEDIT_EDIT*, myString): The GetDlgItemText function on a control variable is meant to fetch text from a child control of that control, not the control itself. Also, IDC_MYEDIT_EDIT is a numeric macro—you don't need to dereference it with *.
  • MainDlg->IDC_MYEDIT_EDIT: IDC_MYEDIT_EDIT is a global resource ID defined in Resource.h, not a member of the CMainMFCDlg class. You can't access it through the dialog object like that.

Bonus: Always Check for Valid Pointers

Adding the nullptr check for pMainDlg prevents crashes if the main window isn't available for some reason—this is a good habit to get into with MFC!

内容的提问来源于stack exchange,提问作者ngmany

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 13:22:45