MFC - 不直接创建控件

MFC - Control aren't created directly

本文关键字:创建 控件 MFC      更新时间:2023-10-16

我创建了一个类名NRGroupBox来管理自定义的GroupBox样式。我有一些辅助工具可以在组框中添加控件:

NRGroupBox
{
  ...
  NRButton   * CreateButton(std::string id, CRect position, std::string content);
  NREdit     * CreateEdit(std::string id, CRect position);
  NRStatic   * CreateStatic(std::string id, CRect position, std::string text);
  NRComboBox * CreateComboBox(std::string id, CRect position);
  ...
  std::map<std::string, NREdit     * > edits;
  std::map<std::string, NRStatic   * > labels;
  std::map<std::string, NRButton   * > buttons;
  std::map<std::string, NRComboBox * > comboBoxes;
  ...
}

下面是其中一个辅助函数的代码:(这四个函数非常相似)

NREdit * NRGroupBox::CreateEdit(std::string id, CRect position)
{
    if(!edits.count(id))
    {
        NREdit * buff = new NREdit();
        buff->Create(WS_CHILD | WS_VISIBLE, position, this, editIds++);
        buff->MoveWindow(position);
        edits[id] = buff;
    }
    return edits[id];
}

我的问题是,当我调用这个函数时,编辑框没有显示,我需要在CreateEdit函数之外调用MoveWindow。我不明白我为什么要这样做。

下面是我想如何使用NRGroupBoxCreateEdit函数的示例。

BOOL ConfigWindow::OnInitDialog()
{
    if(!CDialog::OnInitDialog())
    {
        NRthrow("Impossible de créer la fenêtre");
        return FALSE;
    }
    MoveWindow(0,0,800,800);
    ShowWindow(SW_SHOW);
    groupBox = new NRGroupBox();
    groupBox->Create("Test GroupBox", CRect(0,0,500,500), this);
    groupBox->SetNRStyle();
    bouton  = groupBox->CreateButton("bouton", CRect(230,60,100,20), "Test Bouton");
    label   = groupBox->CreateStatic("label", CRect(10,60,100,20), "Test label");
    editBox = groupBox->CreateEdit("editBox", CRect(120,60,100,20));
    // Actually I need those lines, but I don't want to need it.
    //editBox->MoveWindow(120,60,100,20);
    bouton->MoveWindow(230,60,100,20);
    label->MoveWindow(10,60,100,20);
    NRDebug::Notice("Création d'une fenêtre de Paramétrage");

    return TRUE;
}

谢谢你的帮助

CRect(120,60,100,20)

你的坐标顺序错了,正确的<左和下><前>