#include <windows.h>
#include <richedit.h>
#include <commctrl.h>
static LRESULT CALLBACK WndProc
(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
switch (message)
{
case WM_CREATE:
{
// Must load rich edit library
LoadLibrary(L"riched20.dll");
HWND hWndRichEdit;
hWndRichEdit = CreateWindow(RICHEDIT_CLASS, NULL,
WS_CHILD | ES_SAVESEL | ES_NOHIDESEL | WS_CHILDWINDOW | WS_BORDER\
| WS_VISIBLE | ES_MULTILINE | WS_VSCROLL | WS_EX_STATICEDGE,
0, 0, 300, 200, hWnd, 0, GetModuleHandle(0), 0);
CHARFORMAT cf;
ZeroMemory(&cf, sizeof(CHARFORMAT));
// Get the charformat from richedit
SendMessage(hWndRichEdit, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cf);
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_COLOR | CFM_ITALIC | CFM_SIZE | CFM_FACE; // Change color and italic
cf.dwEffects = CFE_ITALIC; // Italic
cf.yHeight = 18 * 18; // Size
cf.crTextColor = RGB(0, 200, 100); // Color
wcscpy_s(cf.szFaceName, L"宋体"); // Font
// Set default char format
SendMessage(hWndRichEdit, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cf);
// Show the text
SendMessage(hWndRichEdit, EM_REPLACESEL, 0, (LPARAM)L"hello");
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
int APIENTRY wWinMain
(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow
)
{
wchar_t szAppName[] = L"TestRicheditCharFormat";
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = GetModuleHandle(0);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;
RegisterClassEx(&wndclass);
HWND hWnd = CreateWindow(szAppName, L"Hello, world!", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, GetModuleHandle(0), NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}