Rich Text Edit Control Example

Most common controls (such as TreeView, ListView, Toolbar, Status Bar, TrackBar, Up-Down, Tab Control, and Progress Bar) are built into COMCTL32.DLL, so you simply call InitCommonControls() or InitCommonControlsEx() with the appropriate ICC_* class flag before creating them. A Rich Edit control is different because it is not part of COMCTL32.DLL; it is implemented in a separate DLL (RICHED32.DLL or RICHED20.DLL). Therefore, before creating a Rich Edit control, you must first load the Rich Edit DLL using LoadLibrary(). Once the DLL has been loaded, create the control by specifying RICHEDIT_CLASS (or RICHEDIT_CLASSW) as the window class name. Unlike the other common controls, the Rich Edit control does not require an ICC_* initialization flag because loading the Rich Edit DLL automatically registers the Rich Edit window class.

#define _WIN32_WINNT 0x0501 #include <windows.h> #include <commctrl.h> #include <richedit.h> #pragma comment(lib, "comctl32.lib") #define ID_EDIT 1 #define ID_ITALICBUTTON 2 #define ID_UNDERLINEBUTTON 3 #define ID_BOLDBUTTON 4 HMODULE hMod; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { hMod = LoadLibrary(TEXT("Riched20.dll")); WNDCLASSEX wc; MSG msg; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT("myWindowClass"); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&wc); //Creating the Window CreateWindowEx( WS_EX_CLIENTEDGE, TEXT("myWindowClass"), TEXT("Rich Edit Control Demo"), WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 300, 220, NULL, NULL, hInstance, NULL); //The Message Loop while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND hwndEdit; static HWND hwndItalicButton; static HWND hwndUnderlineButton; static HWND hwndBoldButton; switch (msg) { case WM_CREATE: { //load common control class ICC_USEREX_CLASSES from the dynamic-link library (DLL) INITCOMMONCONTROLSEX icex; icex.dwSize = sizeof(INITCOMMONCONTROLSEX); icex.dwICC = ICC_USEREX_CLASSES; InitCommonControlsEx(&icex); hwndEdit = CreateWindowEx( WS_EX_CLIENTEDGE, RICHEDIT_CLASS, NULL, WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_HSCROLL | WS_VSCROLL, 10, 10, 250, 100, hwnd, (HMENU)ID_EDIT, GetModuleHandle(NULL), NULL); //create buttons hwndItalicButton = CreateWindowEx( 0, TEXT("BUTTON"), TEXT("Italic"), BS_AUTOCHECKBOX | BS_NOTIFY | BS_PUSHLIKE | WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 10, 130, 80, 30, hwnd, (HMENU)ID_ITALICBUTTON, GetModuleHandle(NULL), NULL); hwndUnderlineButton = CreateWindow( TEXT("BUTTON"), TEXT("Underline"), BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_VISIBLE | WS_CHILD, 95, 130, 80, 30, hwnd, (HMENU)ID_UNDERLINEBUTTON, NULL, NULL); hwndBoldButton = CreateWindow( TEXT("BUTTON"), TEXT("Bold"), BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_VISIBLE | WS_CHILD, 180, 130, 80, 30, hwnd, (HMENU)ID_BOLDBUTTON, NULL, NULL); CHARFORMAT cf = {0}; cf.cbSize = sizeof(CHARFORMAT); cf.dwEffects = 0; cf.dwMask = CFM_BOLD; SendMessage(hwndEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf); } break; case WM_COMMAND: if (HIWORD(wParam) == BN_CLICKED) { CHARFORMAT cf = {0}; cf.cbSize = sizeof(CHARFORMAT); switch (LOWORD(wParam)) { case ID_ITALICBUTTON: if (IsDlgButtonChecked(hwnd, ID_ITALICBUTTON)) { cf.dwEffects = CFE_ITALIC; cf.dwMask = CFM_ITALIC; } else { cf.dwEffects = 0; cf.dwMask = CFM_ITALIC; } SendMessage(hwndEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf); break; case ID_UNDERLINEBUTTON: if (IsDlgButtonChecked(hwnd, ID_UNDERLINEBUTTON)) { cf.dwEffects = CFE_UNDERLINE; cf.dwMask = CFM_UNDERLINE; } else { cf.dwEffects = 0; cf.dwMask = CFM_UNDERLINE; } SendMessage(hwndEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf); break; case ID_BOLDBUTTON: if (IsDlgButtonChecked(hwnd, ID_BOLDBUTTON)) { cf.dwEffects = CFE_BOLD; cf.dwMask = CFM_BOLD; } else { cf.dwEffects = 0; cf.dwMask = CFM_BOLD; } SendMessage(hwndEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf); break; } } break; case WM_DESTROY: if (hMod) FreeLibrary(hMod); PostQuitMessage(0); break; } return DefWindowProc(hwnd, msg, wParam, lParam); }