tags:

views:

431

answers:

4

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  A: 

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.

Aamir
Yeah, normally I would do that too.
djeidot
+2  A: 

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());
DavidK
Thanks for that quick fix.
Xperimental
+1  A: 

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.

sharptooth
Thanks, that fixed it. I must have somehow missed the style flag, as I had tried that approach with opening the .rc file before, not sure why though.
Xperimental
A: 

Please see the answers by DavidK and sharptooth for the solution to my problems.

Xperimental
As stated in the main comment area, I didn't know which of the answers to select as the "accepted" one. If posting my own answer with a reference to the original ones isn't a valid solution to that problem, please post a comment.
Xperimental