Search code examples
c++winapiricheditdialog

Rich Edit Control changes the DialogBox return value behavior?


I'm a bit puzzled: i created a dialogbox with Edit Control, then i noticed the text isn't word wrapped, so i googled and found out that i should use Rich Edit Control instead. So i did. Now, when ever there is a Rich Edit Control in my dialog box, the functionality changes: without Rich Edit Control the dialogbox returned either IDOK or IDCANCEL, which i handle outside of the message handler code. BUT, if there is a Rich Edit Control anywhere in the dialogbox, it always returns something else than IDOK, before i even click any buttons in the dialog box: the dialogbox seems to not even be created at all.

Here is the message handler:

INT_PTR CALLBACK MyDialogBox(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){
    switch(message){
        case WM_INITDIALOG: {
            SetDlgItemText(hDlg, IDC_EDIT1, (LPCTSTR)some_string.c_str());
            return (INT_PTR)TRUE;
        }
        case WM_COMMAND:
            switch(LOWORD(wParam)){
                case IDOK: case IDCANCEL: {
                    EndDialog(hDlg, LOWORD(wParam));
                    return (INT_PTR)TRUE;
                }
            }
        break;
    }
    return (INT_PTR)FALSE;
}

And here is the code where i use the dialogbox:

if(DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, MyDialogBox) == IDOK){
    // without rich edit control it goes here or below depending on the user choice.
}else{
    // with rich edit it always goes here.
}

So, the ultimate question here is: how do i get this thing work like it works with normal Edit Control ?

Edit: when it fails, the values are: -1 for DialogBox(), and 0 for GetLastError(), if that helps ?

Edit2: Problem solved by antinome's link: include afxwin.h and call AfxInitRichEdit2() at the window WM_CREATE message.


Solution

  • This thread has some good tips for resolving this problem. To summarize:

    If using pure WinAPI:

    • Make sure to call LoadLibrary("RichEd20.dll"); or LoadLibrary("Msftedit.dll");. The latter is the newer version of the control.
    • According to Rich Edit Control in raw Win32, you can also call InitCommonControlsEx() with the appropriate class constant (MSFTEDIT_CLASS apparently) — but it's only needed if you want windows visual styles to work.

    If using MFC:

    • Make sure to call AfxInitRichEdit2() at initialization stage, for example in InitInstance()