The MFC Professional Stingray Software

NEWBIE TIP #3
Using a rich edit control in a dialog box


Put a control (a CStatic or CEdit will do nicely) on the dialog as a place holder, sized correctly. We'll identify it as IDC_RICH. Then in OnInitDialog, get yourself a pointer to the control's window, get the control's style bits and rectangle, destroy the control, and create the CRichEditCtrl in its place.


void CMyDialog::OnInitDialog()
{
    // Get a pointer to the place holder control.
    CWnd *pCtl = GetDlgItem(IDC_RICH);

    // If you use an edit control as place
    // holder, you can grab its styles.
    DWORD dwStyles = GetWindowLong(pCtl->m_hWnd, GWL_STYLE);

    // Get the place holder's position in the dialog.
    CRect rect;
    pCtl->GetWindowRect(&rect);
    ScreenToClient(&rect);

    // Kill the place holder control.
    pCtl->DestroyWindow();

    // Create a new rich edit control with the same
    // style flags, rectangle, and identifier.
    m_MyRichEditCtl.Create(dwStyles, &rect, this, IDC_RICH);
}


Source code is provided for instructional purposes, and is released to the public domain. No claim of ownership is made. Use it at your own risk. No warranties are expressed or implied. The resulting program may not be fully functional. No animals were used to test this code.

| Past Tips | Future Tips | Home |

Comments on this Tip? Send us mail.
(Be sure to put the Tip # in the subject.)

Got an idea for a Tip you'd like us to write? Let us know.

Solved a problem lately that would make a good Tip? PLEASE tell us about it!!!
We'll give you prominent credit, and we'll even buy you a beer next time you're in town.