Document directory
- 1. Load richedw.dll
- Ii. Common Methods
1. Load richedw.dll
If the RichEdit control is included in the dialog box, you must load the corresponding dynamic link library before creating this dialog box (or before dynamically creating RichEdit). Otherwise, the creation will fail. Different Libraries support different RichEdit versions. The relationship is as follows:
Control version |
Dynamic Link Library name |
1.0 |
Riched32.dll |
2.0 |
Richedw.dll |
3.0 |
Richedw.dll |
4.1 |
Msftedit. dll |
The RichEdit libraries of different Windows versions vary according to the following:
Windows XP SP1 |
Supported des rich edit 4.1, rich edit 3.0, and a rich EDIT 1.0 emulator. |
Windows XP |
Using DES rich edit 3.0 with a rich EDIT 1.0 emulator. |
Windows ME |
Supported des rich EDIT 1.0 and 3.0. |
Windows 2000 |
Using DES rich edit 3.0 with a rich EDIT 1.0 emulator. |
Windows NT 4.0 |
Supported des rich EDIT 1.0 and 2.0. |
Windows 98 |
Supported des rich EDIT 1.0 and 2.0. |
Windows 95 |
Except des only rich EDIT 1.0. However, richedw.dll is compatible with Windows 95 and may be installed by an application that requires it. |
Example:
class RichEditLib {
HMODULE h_;
public:
RichEditLib()
: h_(LoadLibrary(_T("riched20.dll")))
{
if (!h_)
throw std::runtime_error("Cannot load /"riched20.dll/".");
}
~RichEditLib() {
FreeLibrary(h_);
}
};
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) {
try {
....
RichEditLib rel;
} catch (std::exception & ex) {
::MessageBoxA(::GetActiveWindow(), ex.what(), NULL, MB_OK | MB_ICONSTOP);
return -1;
}
....
return 0;
}
Ii. Common Methods
1. Obtain the number of lines containing text in the control.
Send the message em_getlinecount to the control to obtain the total number of lines of text contained in the control,Note that when the text is empty, it returns 1In other words, the total number of rows it returns will never be less than 1. So to really know how many rows are there, you must do some small processing:
int getRichEditLineCount(HWND richedit) {
int result = SendMessage(richedit, EM_GETLINECOUNT, 0, 0);
const int firstCharPosOfLastLine = (int)SendMessage(richedit, EM_LINEINDEX, result-1, 0);
if (!SendMessage(richedit, EM_LINELENGTH, firstCharPosOfLastLine, 0))
--result;
return result;
}
2. Get the position of the last character
int getRichEditTail(HWND richedit) {
int const lines = getRichEditLineCount(richedit);
int result = (int)SendMessage(richedit, EM_LINEINDEX, lines-1, 0);
result += (int)SendMessage(richedit, EM_LINELENGTH, result, 0);
return result;
}
3. scroll to the last line
If you write code to add a line of text to RichEdit, you may need to make it automatically scroll to the last line (such as logs and chat rooms) and send the wm_vscroll message to it:
SendMessage(richedit, WM_VSCROLL, SB_BOTTOM, 0)