C++ 在运行时更改对话框中的静态文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21642041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:44:36  来源:igfitidea点击:

Changing static text in dialog box at runtime

c++mfc

提问by user3126297

I have created a dialog box and linked it to the menu item. In this case the menu item is Help -> Statistics. It all works. So when I run the program, click on the menu Help, then Statistics, a dialog box pops up.

我创建了一个对话框并将其链接到菜单项。在这种情况下,菜单项是Help -> Statistics。这一切都有效。所以当我运行程序时,点击菜单Help,然后点击Statistics,弹出一个对话框。

I also have a static text box in the dialog box. How do you change the text of this static text box at runtime?

我在对话框中还有一个静态文本框。如何在运行时更改此静态文本框的文本?

P.S: Though I have a dialog box up and running, I do not have the handle for the dialog box. If any of your solutions involve knowing the handle to the dialog box, please tell me how to retrieve it. Thanks.

PS:虽然我有一个对话框正在运行,但我没有对话框的句柄。如果您的任何解决方案涉及知道对话框的句柄,请告诉我如何检索它。谢谢。

EDIT:

编辑:

class CStatisticsDlg : public CDialogEx
{
public:
        CStatisticsDlg();

// Dialog Data
    enum { IDD = IDD_STATISTICS };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
    DECLARE_MESSAGE_MAP()
public:
};

CStatisticsDlg::CStatisticsDlg() : CDialogEx(CStatisticsDlg::IDD)
{
}

void CStatisticsDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CStatisticsDlg, CDialogEx)
END_MESSAGE_MAP()

回答by Johnny Mopp

  1. In Class Wizard, create a CString member variable for the label. Note: by default, labels don't have a custom id so you have to give it one like IDC_MY_LABEL.
  2. Somewhere before showing the dialog call m_strMyLabel.SetWindowText("blah");
  1. 在类向导中,为标签创建一个 CString 成员变量。注意:默认情况下,标签没有自定义 id,所以你必须给它一个像 IDC_MY_LABEL 这样​​的。
  2. 在显示对话调用之前的某个地方 m_strMyLabel.SetWindowText("blah");

If you need to do it while the dialog is open you have to call UpdateData(FALSE)

如果您需要在对话框打开时执行此操作,则必须调用 UpdateData(FALSE)

Edit: if you don't want to create a member variable you can **corrected - typing from memory....

编辑:如果您不想创建成员变量,您可以**更正 - 从内存中输入....

// Find the label
// if called from within CStatusDlg class
CWnd *label = GetDlgItem(IDC_MY_LABEL);
label->SetWindowText("blah");

// If called from elsewhere
CStatusDlg dlg.....  // create the dialog
CWnd *label = dlg.GetDlgItem(IDC_MY_LABEL);
label->SetWindowText("blah");