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

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.

Regards,,
wyatt





 
0
wyatt4u
Asked:
wyatt4u
  • 4
  • 3
1 Solution
 
_corey_Commented:
http://www.codeproject.com/editctrl/dimedit.asp

The source to this might help give you some ideas for creating your own ideas to do owner draws.
0
 
kcyCommented:
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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols.asp

kcy
0
 
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kcyCommented:
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,
kcy
0
 
kcyCommented:
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...
0
 
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.
       GetWindowTextLength();
         iEdit_GetLineCount();
         Edit_LineIndex();
         Edit_LineLength();
         SetSel();

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


Regards,
wyatt
0
 
kcyCommented:
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:
   CHARFORMAT cf;
   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.
kcy
0
 
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now