5
votes

I need to create some controls in a MFC dialog dynamically. The creation works fine so far, but the controls created dynamically are looking different from controls created with the resource editor. Some controls even behave different. I think, that I'm missing some initializations the generated code does.

Currently I only create CStatic and CEdit controls. Both don't use the standard windows font when I create them dynamically (the font looks more like the default font used prior to Windows 95, if I remember correctly).

Also, the CEdit control behaves different from when I create it with the resource editor. The dynamically created control seems to limit the text length to the visible size. I can set a longer text with SetWindowText() and read the full text back in with GetWindowText(), but the user can't enter a text longer than the size displayed. The CEdit control created by the resource editor behaves different: If the user enters a text longer than what can be displayed, the entered text ist "scrolled" inside the control (no scrollbars, as its only a single line control).

I tried fixing that problem by calling SetLimitText() on the control, but that didn't change the behavior.

The controls are saved to arrays defined in the dialog-class:

CStatic** m_pLabels;
CEdit**   m_pEdits;

The creation of the controls happens in the OnInitDialog() method of the dialog-class:

for (int i = 0; i < max; i++)
{
  m_pLabels[i] = new CStatic();
  m_pLabels[i]->Create("key", WS_CHILD | WS_VISIBLE | SS_RIGHT,
    CRect(10, 10 + i * 30, 130, 35 + i * 30), this);

  m_pEdits[i] = new CEdit();
  m_pEdits[i]->CreateEx(WS_EX_CLIENTEDGE, "EDIT", "",
    WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_BORDER,
    CRect(133, 10 + i * 30, 350, 35 + i * 30), this, i + 100);
  m_pEdits[i]->SetLimitText(499);
  m_pEdits[i]->SetWindowText("value to be edited");
}

Thanks for your help!

3
I don't know which of the answers I should select as the "accepted answer" as each one answers a part of my question. What is the normal procedure here?Xperimental
Good question - not sure! I suggest you just pick whichever is the most informative and accept that, with some comment to explain what you did.DavidK
Another possible solution would be for me to answer the question myself, quoting your answers and then selecting my answer as the accepted one. Or I could answer with something like "Read the other answers for the final solution".Xperimental

3 Answers

5
votes

Dynamically created controls always get the stock font initially: the usual approach is to just set the control's font to the parent dialog's font: something like

  pEdits[i]->SetFont(GetFont());
4
votes

I think the best way to do is to put a control through dialog editor on a dialog, set it's visual styles to the ones of your choice and then open the .rc file in a text editor and copy the styles of that control from there. This way you will be able to create controls that are far more closer to the ones you add through dialog editor.

e.g., after putting a simple button on a dialog having OK/Cancel buttons and a text control, my dialog looks like this in the .rc file:

IDD_MFCAPP_DIALOG DIALOGEX 0, 0, 320, 200
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "MFCApp"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,263,7,50,16
    PUSHBUTTON      "Cancel",IDCANCEL,263,25,50,16
    CTEXT           "TODO: Place dialog controls here.",IDC_STATIC,10,96,300,8
    PUSHBUTTON      "Button1",IDC_BUTTON1,43,17,50,14
END

Now, since I have all the information about how the dialog was created at the back-end, I can simply replicate this behviour through code.

P.S. Off course, you would do this in a separate test project.

1
votes

You need to add the ES_AUTOHSCROLL style to the editbox. This style is responsible for scrolling the text leftwards when you enter more text than the box can display.

Opening .rc files in text editor and looking up the styles of controls can help you find out such points.