#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
HINSTANCE hMod = LoadLibrary(TEXT("Riched20.dll"));
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
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) {
HWND static hwndEdit,hwndItalicButton,hwndUnderlineButton,hwndBoldButton,hwndItalicButton3,hwndStatic;
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,
NULL,
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);//click button
hwndBoldButton = CreateWindow(TEXT("button"), TEXT("Bold"),BS_AUTOCHECKBOX| BS_PUSHLIKE | WS_VISIBLE | WS_CHILD, 180, 130, 80, 30,hwnd, (HMENU) ID_BOLDBUTTON, NULL, NULL);//click button
//
CHARFORMAT cf;
cf.cbSize=sizeof(CHARFORMAT);
cf.dwEffects= 0;
cf.dwMask= CFM_FACE|CFE_BOLD;
SendMessage(hwndEdit,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);
break;
case WM_COMMAND:
//responds to button click
if (HIWORD(wParam) == BN_CLICKED)
{
CHARFORMAT cf;
int wmID=0;
wmID = LOWORD(wParam);
switch (wmID)
{
//respond to italic button click
case ID_ITALICBUTTON:
{
if(IsDlgButtonChecked(hwnd,ID_ITALICBUTTON))
{
//turn on Italic text
cf.cbSize=sizeof(CHARFORMAT);
cf.dwEffects= CFE_ITALIC;
cf.dwMask= CFE_ITALIC ;
SendMessage(hwndEdit,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);
}
else
{
//turn off Italic text
cf.cbSize=sizeof(CHARFORMAT);
cf.dwEffects= 0;
cf.dwMask= CFE_ITALIC;
SendMessage(hwndEdit,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);
}
}
break;
//respond to underline button click
case ID_UNDERLINEBUTTON:
{
//turn on underline text {
if(IsDlgButtonChecked(hwnd,ID_UNDERLINEBUTTON))
{
cf.cbSize=sizeof(CHARFORMAT);
cf.dwEffects= CFE_UNDERLINE;
cf.dwMask= CFE_UNDERLINE ;
SendMessage(hwndEdit,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);
}
else
{
//turn off underline text
cf.cbSize=sizeof(CHARFORMAT);
cf.dwEffects= 0;
cf.dwMask= CFE_UNDERLINE;
SendMessage(hwndEdit,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);
}
}
break;
//respond to bold button click
case ID_BOLDBUTTON:
{
if(IsDlgButtonChecked(hwnd,ID_BOLDBUTTON))
//turn on bold text
{
cf.cbSize=sizeof(CHARFORMAT);
cf.dwEffects= CFE_BOLD;
cf.dwMask= CFM_FACE|CFE_BOLD ;
SendMessage(hwndEdit,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);
}
else
{
//turn off bold text
cf.cbSize=sizeof(CHARFORMAT);
cf.dwEffects= 0;
cf.dwMask= CFM_FACE|CFE_BOLD;
SendMessage(hwndEdit,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);
}
break;
}
break;
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}