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

Edit control, font win32 api C programming

Posted on 2003-11-05
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
  • 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.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


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 130 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
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…

856 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