Rich Edit Bold text

I have a function which adds text to a Rich Edit control, and makes the first 5 letters added bold.  The text is added fine, but the first 5 letters aren't bold at all.  Anyone have any ideas why it's not working correctly?

void SetText(HWND hwnd, LPCTSTR lpszText)
{
      CHARFORMAT            cf;
      int                        i;

      i = GetWindowTextLength(hwnd);

      /* Fill the CHARFORMAT structure */
      memset(&cf,0,sizeof(cf));
      cf.cbSize = sizeof(cf);

      /* Append the text to the end of the rich edit box */
      SendMessage (hwnd, EM_SETSEL, (WPARAM)i, (LPARAM)i);
      SendMessage (hwnd, EM_REPLACESEL, 0, (LPARAM)lpszText);
      SendMessage (hwnd, EM_REPLACESEL, 0, (LPARAM)_T("\r\n"));

      /* Make the first 5 letters BOLD */
      cf.dwMask = CFM_BOLD;
      cf.dwEffects = CFE_BOLD;
      SendMessage (hwnd, EM_SETSEL, i, i+5);
      SendMessage(hwnd,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);

}
LVL 5
lwinkenbAsked:
Who is Participating?
 
Roshan DavisCommented:
     // try some fonts
      memset( &cf, 0, sizeof cf );
      cf.cbSize = sizeof cf;
      cf.dwMask = CFM_FACE | CFM_SIZE;
      cf.yHeight = 300;
      strcpy(cf.szFaceName, "Tahoma");
      SendMessage( hwnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
      SendMessage( hwnd, EM_REPLACESEL, FALSE, (LPARAM) "tahoma ");

~roshan
0
 
lwinkenbAuthor Commented:
Also, I'm able to change the color of the text, and make it underlined.  I just can't seem to make it bold.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Make sure you are using a truetype font for presenting, could be Arial by example.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
lwinkenbAuthor Commented:
I changed the code to:

void SetText(HWND hwnd, LPCTSTR lpszText)
{
     CHARFORMAT          cf;
     int                    i;

     i = GetWindowTextLength(hwnd);

     /* Fill the CHARFORMAT structure */
     memset(&cf,0,sizeof(cf));
     cf.cbSize = sizeof(cf);

     /* Append the text to the end of the rich edit box */
     SendMessage (hwnd, EM_SETSEL, (WPARAM)i, (LPARAM)i);
     SendMessage (hwnd, EM_REPLACESEL, 0, (LPARAM)lpszText);
     SendMessage (hwnd, EM_REPLACESEL, 0, (LPARAM)_T("\r\n"));

     /* Make the first 5 letters BOLD */
     cf.dwMask = CFM_BOLD | CFM_FACE;
     _tcscpy(cf.szFaceName,_T("Arial"));
     cf.dwEffects = CFE_BOLD;
     SendMessage (hwnd, EM_SETSEL, i, i+5);
     SendMessage(hwnd,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);
}

But it still isn't doing BOLD.
0
 
Jaime OlivaresSoftware ArchitectCommented:
appears to be you are working with Unicode or wide characters, try with:
wcsncpy( cf.szFaceName, L"Arial", sizeof(cf.szFaceName) );
Also, ensure you are using Richedit 2.0, have a look to this previous question:
http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_11065469.html
0
 
lwinkenbAuthor Commented:
>>  appears to be you are working with Unicode or wide characters
What makes you say that?  Just because I'm using the tchar.h string manipulation functions doesn't mean I'm using unicode or wide char.  In this case, I'm using regular ANSI byte strings (although I have the same problem if I use unicode strings).

>> Also, ensure you are using Richedit 2.0, have a look to this previous question:
I've tried using Richedit 1.0 and 3.0, and the problem is the same with both versions.
0
 
_corey_Commented:
lwinkenb,

  Are you able to make it italics then?  The setup seems correct, but I want to know if a font effect like italics works before writing some test code.

corey
0
 
lwinkenbAuthor Commented:
Yeah, italics work perfectly.
0
 
Roshan DavisCommented:
hi lwinkenb,

I think the font which you selected not supports bold, here I checked the same code (code which is in the question itself), itz working fine!!!

~roshan
0
 
lwinkenbAuthor Commented:
That appears to be the problem.  Bold worked just fine with the Tahoma font.  Thanks for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.