1

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.

Rookie
  • 3,753
  • 5
  • 33
  • 33
  • It would be helpful to know what value is being returned by `DialogBox()`. Also, what value does [`GetLastError()`](http://msdn.microsoft.com/en-us/library/ms679360%28v=vs.85%29.aspx) return if you call it after DialogBox returns? – antinome Jun 21 '11 at 18:26
  • @antinome, -1 for dialogbox(), and 0 for getlasterror(). – Rookie Jun 21 '11 at 20:21
  • Is that *all* the code? Or is your code missing *break* in one or more case statements? – Hans Passant Jun 21 '11 at 21:07
  • A multiline edit control is word wrapped if ES_AUTOHSCROLL is not specified. See ES_MULTILINE in [Edit Control Styles](http://msdn.microsoft.com/en-us/library/bb775464%28v=vs.85%29.aspx). – Sertac Akyuz Jun 21 '11 at 21:44
  • @Hans, yes its all my code. the bug isnt about the code btw: whenever i add that Rich Edit Control, the dialogbox stops from functioning as expected, and when i delete the Rich Edit Control, it works fine again! And this all happens even without referencing to this Rich Edit Control at all in my code. – Rookie Jun 22 '11 at 13:10
  • @Sertac, thanks! i could have never guessed it was ES_AUTOHSCROLL that enables wordwrapping. – Rookie Jun 22 '11 at 13:13
  • Can you try creating a Rich Edit control **not** in a dialog box? Such as in your main application window. If that fails, too, then take a look and tell us how you are linking/loading/initializing the `dll` or `lib` file where the control is coming from. (e.g. Riched20.dll or Msftedit.dll) – antinome Jun 22 '11 at 18:13
  • Also check out **[Rich edit control preventing Dialog Box from opening](http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/c325a6ab-5e58-4970-a123-cda46fb096ff/)** (although I guess you probably are happy now with the regular edit control) – antinome Jun 22 '11 at 18:15
  • @antinome, checked the link and the answer is to include `afxwin.h` and call `AfxInitRichEdit2()` at the window `WM_CREATE` message. Thanks! if you want you can add it as an answer and i can accept it. – Rookie Jun 22 '11 at 21:19
  • Have written it up as an answer. Thanks! – antinome Jun 22 '11 at 22:57

1 Answers1

1

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()
Community
  • 1
  • 1
antinome
  • 3,408
  • 28
  • 26