Richedit control

Richedit
Richedit

This is a sample of a window with richedit control. Copy this code into your main.cpp and go. Or Download the solution(MS Visual Studio.NET)
There are probable a lot of way of putting text in a Richedit control. The function AddText does this so anyone can understand.
#define   STRICT

#include  <windows.h>
#include  <richedit.h>

void AddText(HWND hwnd, char *szTextIn, COLORREF crNewColor)
{
   char *Text = (char *)malloc(lstrlen(szTextIn) + 5);
   CHARFORMAT cf;
   int iTotalTextLength = GetWindowTextLength(hwnd);
   int iStartPos = iTotalTextLength;
   int iEndPos;

   strcpy(Text, szTextIn);
   strcat(Text, "\r\n");

   SendMessage(hwnd, EM_SETSEL, (WPARAM)(int)iTotalTextLength, (LPARAM)(int)iTotalTextLength);
   SendMessage(hwnd, EM_REPLACESEL, (WPARAM)(BOOL)FALSE, (LPARAM)(LPCSTR)Text);

   free(Text);

   cf.cbSize      = sizeof(CHARFORMAT);
   cf.dwMask      = CFM_COLOR | CFM_UNDERLINE | CFM_BOLD;
   cf.dwEffects   = (unsigned long)~(CFE_AUTOCOLOR | CFE_UNDERLINE | CFE_BOLD);
   cf.crTextColor = crNewColor;

   iEndPos = GetWindowTextLength(hwnd);

   SendMessage(hwnd, EM_SETSEL, (WPARAM)(int)iStartPos, (LPARAM)(int)iEndPos);
   SendMessage(hwnd, EM_SETCHARFORMAT, (WPARAM)(UINT)SCF_SELECTION, (LPARAM)&cf);
   SendMessage(hwnd, EM_HIDESELECTION, (WPARAM)(BOOL)TRUE, (LPARAM)(BOOL)FALSE);

   SendMessage(hwnd, EM_LINESCROLL, (WPARAM)(int)0, (LPARAM)(int)1);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
   static   HWND           hWndEdit;
   static   int            iStartPos, iEndPos;
   CHARFORMAT              cf;
   CREATESTRUCT            *cs;
   RECT                    rect;

   switch (iMsg)
   {
   case WM_CREATE :
      cs = (LPCREATESTRUCT)lParam;
      hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "RichEdit", NULL,
         WS_CHILD | WS_CLIPCHILDREN | ES_WANTRETURN | ES_MULTILINE | WS_VISIBLE | ES_LEFT,
         0, 0, 1, 1, hwnd, NULL, cs->hInstance, NULL);

      SendMessage(hWndEdit, EM_SETWORDBREAKPROCEX, (WPARAM)0, (LPARAM)NULL);

      cf.cbSize = sizeof (CHARFORMAT);  
      cf.dwMask = CFM_FACE | CFM_SIZE;
      cf.yHeight = 180;
      strcpy(cf.szFaceName, "MS Sans Serif");
      SendMessage(hWndEdit, EM_SETCHARFORMAT, (WPARAM)(UINT)0, (LPARAM)&cf);

      AddText(hWndEdit, "Hello world", RGB(164,0,0));
      break;

   case WM_SIZE :
      GetClientRect(hwnd, &rect);
      MoveWindow(hWndEdit, 3, 3, rect.right - 6, rect.bottom - 6, 1);
      break;

   case WM_CLOSE :
      DestroyWindow(hwnd);
      break;

   case WM_DESTROY :
      PostQuitMessage(0);
      break;
   }
   return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
   HWND        hwnd;
   MSG         msg;
   WNDCLASS    wndclass;
   HINSTANCE   hRichEdit;  
   char        szAppName[] = "RicheditApp";

   wndclass.style         = 0;
   wndclass.lpfnWndProc   = WndProc;
   wndclass.cbClsExtra    = 0;
   wndclass.cbWndExtra    = 0;
   wndclass.hInstance     = hInstance;
   wndclass.hIcon         = LoadIcon(hInstance, szAppName);
   wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
   wndclass.lpszMenuName  = szAppName;
   wndclass.lpszClassName = szAppName;
   RegisterClass(&wndclass);

   hRichEdit = LoadLibrary("RICHED32.DLL");

   hwnd = CreateWindow(szAppName,
      szAppName,
      WS_OVERLAPPEDWINDOW | WS_VISIBLE,
      0, 0, 400, 400,
      NULL, NULL, hInstance, szCmdLine);

   while (GetMessage(&msg, NULL, 0, 0))
   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }

   FreeLibrary(hRichEdit);

   return (int)msg.wParam;
}
Articles
info@win32apicode.com