• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 811
  • Last Modified:

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);

}
0
lwinkenb
Asked:
lwinkenb
  • 5
  • 2
  • 2
  • +1
1 Solution
 
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 OlivaresCommented:
Make sure you are using a truetype font for presenting, could be Arial by example.
0
 
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
Jaime OlivaresCommented:
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
 
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:
That appears to be the problem.  Bold worked just fine with the Tahoma font.  Thanks for the help.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now