title: change text color in win32 richedit
body: <p>I want to show different text colors in win32 rich edit control, here' my test</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;richedit.h&gt;
  #include &lt;commctrl.h&gt;
  
  HWND console;
  
  // util function for rich edit
  namespace rich_edit {
      CHARFORMAT get_char_fmt(HWND hwnd, DWORD range = SCF_DEFAULT) {
          CHARFORMAT cf;
          SendMessage(hwnd, EM_GETCHARFORMAT, range, (LPARAM)&amp;cf);
          return cf;
      }
      void set_char_fmt(HWND hwnd, const CHARFORMAT&amp; cf, DWORD range = SCF_DEFAULT) {
          SendMessage(hwnd, EM_SETCHARFORMAT, range, (LPARAM)&amp;cf);
      }
      void replace_sel(HWND hwnd, const char* str) {
          SendMessage(hwnd, EM_REPLACESEL, 0, (LPARAM)str);
      }
      void cursor_to_bottom(HWND hwnd) {
          SendMessage(hwnd, EM_SETSEL, -2, -1);
      }
      void scroll_to(HWND hwnd, DWORD pos) {
          SendMessage(hwnd, WM_VSCROLL, pos, 0);
      }
      void scroll_to_bottom(HWND hwnd) {
          scroll_to(hwnd, SB_BOTTOM);
      }
      // this function is used to output text in different color
      void append(HWND hwnd, COLORREF clr, const char* str) {
          cursor_to_bottom(hwnd); // move cursor to bottom
  
          CHARFORMAT cf = get_char_fmt(hwnd); // get default char format
          cf.cbSize = sizeof(cf);
          cf.dwMask = CFM_COLOR; // change color
          cf.crTextColor = clr; 
  
          set_char_fmt(hwnd, cf); // set default char format
  
          replace_sel(hwnd, str); // code from google
          scroll_to_bottom(hwnd); // scroll to bottom
      }
  }
  
  LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  
  int main() {
  
      LoadLibrary("riched20.dll"); // for using rich edit
  
  
      static char szAppName[] = "winhello";
      HWND        hwnd;
      MSG         msg;
      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(&amp;wndclass);
  
      hwnd = CreateWindow(szAppName, "Hello, world!",
              WS_OVERLAPPEDWINDOW,
              CW_USEDEFAULT, CW_USEDEFAULT,
              400, 300,
              NULL, NULL, GetModuleHandle(0), NULL);
  
      ShowWindow(hwnd, SW_SHOW);
      UpdateWindow(hwnd);
  
      while ( GetMessage(&amp;msg, NULL, 0, 0) ) {
          TranslateMessage(&amp;msg);
          DispatchMessage(&amp;msg);
      } 
      return msg.wParam;
  }
  LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {
      switch ( iMsg ) {
      case WM_CREATE:
          console = CreateWindow(RICHEDIT_CLASS, "",
                                 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);
  
          // output a red string
          rich_edit::append(console, RGB(255, 0, 0), "aaaa\n");
  
          return 0;
  
      case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
      }
      return DefWindowProc(hwnd, iMsg, wParam, lParam);
  }
  </code></pre>
  
  <p>The line:</p>
  
  <p><strong>rich_edit::append(console, RGB(255, 0, 0), "aaaa\n");</strong></p>
  
  <p>is supposed to output a red string, but actually it's black, why is that?</p>
  
  <p>Thanks.</p>