Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Edit control, font win32 api C programming

Posted on 2003-11-05
Medium Priority
Last Modified: 2013-12-03
I am wanting to know how would I change the color of the font in a edit control.I can change the font color through getting the DC of the edit control and using the WM_CTLCOLOREDIT and returning a HBRUSH but what I am wanting to do is change half the string that is on the line to black and the other half of the string to red.So if I had a multiline edit control each line/string will be displayed as half black and half red.Can anyone help me out on this or push me into the right direction.Thank you.


Question by:wyatt4u
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Expert Comment

ID: 9689252

The source to this might help give you some ideas for creating your own ideas to do owner draws.

Expert Comment

ID: 9689841
You might try the Rich Edit control.  I've often used it to display status with urgent items displayed in red.  You can set all fonts attributes including color and size of sections of text in the control using the EM_SETCHARFORMAT message.  Visual C++ has a wrapper class for this control called CRichEditCtrl if you are using Visual C++.

Here is a link to an MSDN web page about rich edit controls:


Author Comment

ID: 9690359
I was going to try and use the Richedit control but its seems I can't get the riched20.dll for (2.0) file to load correctly.I have tryed every thing known to man kind I think hehehe.The problem with that was the

 LoadLibrary("riched20")   would load but the
GetProcAddress()  would return null and could not figure out why, so .... that
left me  going this route.The route of using a edit control.I figure I go this route so I would not have to make my own control which would be alot of work.I figure I could do everything I need to do with the existing edit control since I can get the HDC of the edit  control.So far I can set all its font attributes using createfont or using the LOGFONT structure with CreateFontIndirect .I can set the forground color of the edit control and the background color as well.I am just stuck at this point where I need for each line index to display a string half black and half red or whatever.The artical you pointed me to _corey_  has a artical there by Jan van den Baard "BrainChild" that looks promissing.I just need to sort through all his code I think.I think what I am looking for is syntax coloring.Well off to see if this code can help me out.Thank you both for your reply.
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments


Expert Comment

ID: 9690823
Looking back at one of my old programs, I also had to do a LoadLibrary before it would work, only I was using riched32.dll (version 1.0) instead.  To create the control on the fly after the library is loaded use the CreateWindow() function specifying "RICHEDIT" as the class name.  Or if you are using a template for a dialog box, then create a user defined control and set its class name to "RICHEDIT".  There is no function that I needed to use GetProcAddress() on.  Everything was done with EM_ messages.  RICHED32.DLL and RICHED20.DLL should be located in your windows\system32 directory.

Hope this helps,

Expert Comment

ID: 9691069
I did a little digging on my PC and it appears that RICHED32.DLL is a wrapper for RICHED20.DLL.  So you might try LoadLibrary("riched32.dll") instead.  Maybe it doesn't work unless you load that library.  I'm just guessing...

Author Comment

ID: 9697187
Thanks to you both.Ok this is where I am at I have gotten the RichEdit control to work.I can use 1.0 or 2.0. Dont know what I was doing before for it  not to work but it is working now. But I am still stuck at my same problem how can I get each line to dispay half black and half red.Each time a new line is inserted.I been messing with these functions trying to do it but have not got it down yet.

Any help would be great.Also I am using the Richedit control 2.0


Accepted Solution

kcy earned 520 total points
ID: 9699186
Add a handler for the EN_CHANGE notification for your rich edit control.  In order to receive the EN_CHANGE notifications you will need to add this statement to your WM_INITDIALOG or WM_CREATE message handler:
   SendMessage(hRichEditCtrl, EM_SETEVENTMASK, 0, SendMessage(hRichEditCtrl, EM_GETEVENTMASK, 0, 0) | ENM_CHANGE);

Add this code to your EN_CHANGE handler:
   DWORD nCurrSel;
   int nSelStart;
   int nLineLen;
   memset(&cf, 0, sizeof(CHARFORMAT));
   cf.cbSize = sizeof(CHARFORMAT);
   cf.dwMask = CFM_COLOR;
   cf.crTextColor = RGB(0, 0, 0);
   // Save cursor position
   nCurrSel = Edit_GetSel(hRichEditCtrl);

   // Get line start position and length
   nSelStart = Edit_LineIndex(hRichEditCtrl, -1);
   nLineLen  = Edit_LineLength(hRichEditCtrl, nSelStart);
   // Set first half of current line to black
   Edit_SetSel(hRichEditCtrl, nSelStart, nSelStart+nLineLen/2);
   ::SendMessage(hRichEditCtrl, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
   // Set second half of current line to red
   cf.crTextColor = RGB(255, 0, 0);
   Edit_SetSel(hRichEditCtrl, nSelStart+nLineLen/2+1, nSelStart+nLineLen);
   ::SendMessage(hRichEditCtrl, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);

   // Restore cursor position
   Edit_SetSel(hRichEditCtrl, LOWORD(nCurrSel), HIWORD(nCurrSel));

I hope this works for you.

Author Comment

ID: 9700240
Thank you kcy.I was on the right track an this is almost done just like I was doing it I just could not get the SetSel to do right but you helped cleared that up.Thanks to you both.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question