5

Is the documentation for Rich Edit Controls really as bad (wrong?) as it seems to be? Right now I'm manually calling LoadLibrary("riched20.dll") in order to get a Rich Edit Control to show up. The documentation for Rich Edit poorly demonstrates this in the first code sample for using Rich Edit controls.

It talks about calling InitCommonControlsEx() to add visual styles, but makes no mention of which flags to pass in.

Is there a better way to load a Rich Edit control?

http://msdn.microsoft.com/en-us/library/bb787877(VS.85).aspx

Here's the only code I could write to make it work:

#include "Richedit.h"
#include "commctrl.h"

INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_USEREX_CLASSES;  //Could be 0xFFFFFFFF and it still wouldn't work
InitCommonControlsEx(&icex);  //Does nothing for Rich Edit controls

LoadLibrary("riched20.dll");  //Manually?  For real?
hWndRichEdit = CreateWindowEx(
    ES_SUNKEN,
    RICHEDIT_CLASS,
    "",
    WS_BORDER | WS_VISIBLE | WS_CHILD,
    2, 2, 100, 24,
    hWnd, (HMENU) ID_RICH_EDIT, hInst, NULL);
buti-oxa
  • 11,261
  • 5
  • 35
  • 44
user16408
  • 53
  • 1
  • 5

4 Answers4

2

Using MFC, RichEdit controls just work.

Loading with InitCommonControlsEx() - ICC_USEREX_CLASSES doesn't load RichEdit AFAIK, you don't need it as it only does the 'standard' common controls, which don't include richedit. Apparently you only need to call this to enable 'visual styles' in Windows, not to get RichEdits working.

If you're using 2008, you want to include Msftedit.dll and use the MSFTEDIT_CLASS instead (MS are rubbish for backward compatibilty sometimes).

The docs do suggest you're doing it right for Win32 programming.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
  • 1
    No, you can still use it for ANSI apps, but it does support the WM_UNICHAR notification to send unicode text to/from it in an ANSI app. – gbjbaanb Sep 18 '08 at 13:49
  • MSFTEDIT_CLASS only for unicode applications: Yes. In VC2012 MFC, MSFTEDIT_CLASS is used if the program is compiled with _UNICODE. If _UNICODE not defined, RICHEDIT_CLASS is used. – linquize May 17 '13 at 11:21
  • Although MSFTEDIT_CLASS macro is only defined in the MS headers if UNICODE is defined, you can create a rich edit control using WIN32 which works if you pass in "RICHEDIT50W" as the class name in an ANSI app. The control handles the full unicode codepoint set provided you set utf-8 as the code page. Why MSFTEDIT_CLASS is only defined in UNICODE apps I don't know. You have to load "Msftedit.dll" of course. (I don't know anything about MFC.) – AndyK Mar 09 '22 at 12:49
2

Many years ago, I ran into this same issue, and yes, the answer was to load the .dll manually. The reason, as far as I can remember, is that the RichEdit window class is registered in DllMain of riched20.dll.

Brannon
  • 25,687
  • 5
  • 39
  • 44
  • That’s not the reason (other statically-linked DLLs register their classes in DllMain as well). The reason is Microsoft providing different versions of the RichEdit (in parts incompatible) in different DLLs. They want you to load the DLL manually in order to pick the exact version you need. – Krishty Apr 27 '21 at 07:19
1

Isn't there an import library (maybe riched20.lib) that you can link to. Then you won't have to load it "manually" at run time. That's how all the standard controls work. VS automatically adds a reference to user32.lib when you create a project.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • Unfortunately, the Windows SDK nowadays ships without `riched20.lib`. MS states [here](https://learn.microsoft.com/en-us/windows/win32/controls/create-rich-edit-controls) that one has to load the library dynamically. – Krishty Apr 25 '21 at 00:13
0

I think you have to call CoInitializeEx before you create any of the common controls.

The LoadLibrary is not needed. If you link with the correct .lib file the exe-loader will take care of such details for you.

Nils Pipenbrinck
  • 83,631
  • 31
  • 151
  • 221
  • 4
    CoInitializeEx() is needed to initialized COM. It's not needed for the win32 common controls. You may have been thinking of InitCommonControlsEx(). – Ferruccio Sep 17 '08 at 17:26