Edit control, font win32 api C programming

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.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


The source to this might help give you some ideas for creating your own ideas to do owner draws.
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:

wyatt4uAuthor Commented:
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.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

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,
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...
wyatt4uAuthor Commented:
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

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wyatt4uAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.