简体   繁体   English

在哪里初始化另一个对话框上的丰富编辑控件?

[英]Where to initialize a rich edit control on another dialog?

I have an MFC dialog based application that has 2 Dialogs: Main Dialog CMyDlg and Second dialog CMyDlg2 . 我有一个基于MFC对话框的应用程序,其中包含2个对话框:主对话框CMyDlg和第二个对话框CMyDlg2

On the main Dialog I add a Button "Go dialog 2". 在主对话框上,添加一个按钮“转到对话框2”。 So I added a handler for the button so that when clicked it pops up the second dialog. 因此,我为按钮添加了一个处理程序,以便在单击该按钮时会弹出第二个对话框。 Everything works fine But on the second Dialog I have added a Rich Edit Control from toolbox. 一切正常,但是在第二个对话框中,我从工具箱添加了Rich Edit Control。 I Added for it a variable. 我为它添加了一个变量。 I also added a class for the second dialog. 我还为第二个对话框添加了一个类。

Now If I run the Application I get the dialog one and if I pressed "Go to dialog 2" I got what I want. 现在,如果我运行该应用程序,我将看到一个对话框,如果按下“转到对话框2”,则会得到我想要的。 But I need at some point to change the font of the rich edit control but my program crashes. 但是我有时需要更改丰富的编辑控件的字体,但是我的程序崩溃了。

So I overrided OnInitDialog and inside it do some changes to the control but program crashes. 因此,我重写了OnInitDialog并在其中进行了一些控件更改,但程序崩溃了。 After debugging I found that the handle of rich edit is null?! 调试后,我发现丰富编辑的句柄为null ?!

So how and where can I change the color or do some initializations to the control? 那么如何在哪里更改颜色或对控件进行一些初始化?

(I called AfxInitRichEdit2() in OnInitInstance() ) (我在OnInitInstance()称为AfxInitRichEdit2() OnInitInstance()

BOOL CMyDlg2::OnInitDialog() {
    m_richEdit.SetWindowText("Hello there!"); // program crashes because the handle m_richEdit is null.

    return TRUE;
}

And this is the handler of button that creates the Dialog2 and that contains the rich edit control: 这是创建Dialog2并包含丰富编辑控件的按钮处理程序:

void CMyDlg::OnBnClickedButton1(){
    CMyDlg2 theDlg;
    theDlg.DoModal();
// TODO: Add your control notification handler code here
}
  • If I create the rich edit control programmatically then everything works fine because I create it at OnInitDialog and then it works fine but I need the one that is I added using the wizard toolbox. 如果我以编程方式创建丰富的编辑控件,那么一切都会很好,因为我在OnInitDialog创建了它,然后一切正常,但是我需要使用向导工具箱添加的OnInitDialog

*** The thing is that if I write: ***问题是,如果我写:

    m_richEdit.SetWindowText(""); // program crashes but if I wirte:
    GetDlgItem(IDC_RICHEDIT221).SetWindowText(""); it works fine?

You probably have the following code inserted by wizard: 您可能通过向导插入了以下代码:

void DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_RICHEDIT22, m_richEdit);
}

This tells the dialog to associate m_richEdit with the dialog control IDC_RICHEDIT22 . 这告诉对话框将m_richEdit与对话框控件IDC_RICHEDIT22 But this association is not performed until the base class method CDialog::OnInitDialog(); 但是,直到基类方法CDialog::OnInitDialog();才执行此关联CDialog::OnInitDialog(); is called. 叫做。

BOOL CMyDlg2::OnInitDialog() 
{
    //this line should work:
    GetDlgItem(IDC_RICHEDIT22)->SetWindowText("Hello");

    //this line won't work:
    //m_richEdit.SetWindowText("Hello there!"); <- richedit's handle is NULL

    //this line will subclass m_richEdit
    //plus run other initialization
    CDialog::OnInitDialog(); 

    //m_richEdit is ready
    m_richEdit.SetWindowText("Hello there!"); 
    return TRUE;
}

It's recommended to put CDialog::OnInitDialog() int the first line, to make sure the initialization is done. 建议将CDialog::OnInitDialog()放在第一行,以确保初始化完成。

GetDlgItem works because the control IDC_RICHEDIT22 exists in the dialog template and you have a valid dialog handle. GetDlgItem之所以起作用,是因为对话框模板中存在控件IDC_RICHEDIT22 ,并且您具有有效的对话框句柄。 You are basically making a simple call based on WinAPI's GetDlgItem : 您基本上是在基于WinAPI的GetDlgItem进行简单的调用:

HWND hedit = ::GetDlgItem(m_hWnd, IDC_RICHEDIT22);
::SetWindowText(hedit, "Hello world");

There is no additional initialization needed. 无需其他初始化。

But m_richEdit is just a C++ object, declared as CRichEditCtrl m_richEdit; 但是m_richEdit只是一个C ++对象,声明为CRichEditCtrl m_richEdit; The constructor for this C++ class doesn't do much besides setting m_hWnd to NULL . 除了将m_hWnd设置为NULL之外,此C ++类的构造函数没有做任何事情。

Once it's associated with a valid window handle, we can begin using its windows methods such as CRichEdit::SetWindowText 将其与有效的窗口句柄关联后,我们就可以开始使用其Windows方法,例如CRichEdit::SetWindowText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM