C++ Builder

Moderators: Lundin
Number of threads: 518
Number of posts: 1158

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Perform "Syntax Highlighting" in TRichEdit Posted by Skyterror on 16 Jan 2010 at 2:47 AM
Hi, guys
Currently I have a very confusing problems. Lately I am developing a small project. In my project, I use a RichEdit component. I wonder how can we perform action like in IDE programs which it is syntax highlighting. If user enter some word that program recognized, it will changes the words color to red or blue (depend on what syntax) while the others that are not the syntax are remain black.

Is there anybody could help me solve this problem?
Report
Re: Perform "Syntax Highlighting" in TRichEdit Posted by VampireFE on 20 Jan 2010 at 9:05 AM
This could be a way to do it:

void __fastcall TForm1::RichEdit1Change(TObject *Sender)
{
  ::SendMessage(RichEdit1->Handle, WM_SETREDRAW, false, 0);

  int sp = RichEdit1->SelStart;

  for(int i=0;i<RichEdit1->Text.Length();i++)
  {
    if(RichEdit1->Text[i+1] >= 'A' && RichEdit1->Text[i+1] <= 'Z') // capital letters
    {
      RichEdit1->SelStart = i;
      RichEdit1->SelLength = 1;
      RichEdit1->SelAttributes->Color = clBlue;
      RichEdit1->SelAttributes->Style = RichEdit1->SelAttributes->Style << fsBold;
    }
    else
    {
      RichEdit1->SelStart = i;
      RichEdit1->SelLength = 1;
      RichEdit1->SelAttributes->Color = clBlack;
      RichEdit1->SelAttributes->Style = RichEdit1->SelAttributes->Style >> fsBold;
    }
  }

  RichEdit1->SelStart = sp;

  ::SendMessage(RichEdit1->Handle, WM_SETREDRAW, true, 0);
  RichEdit1->Refresh();
  RichEdit1->Repaint();
}


I know that code is ... well, lame, but, meh, I don't care :D

What it does is:
1. disables drawing
2. it goes through all elements in RichEdit, changing capital letters to bold and blue
3. enables drawing

Should be pretty easy to search for particular strings in there and change them.

I hope it helps :)
Report
Re: Perform "Syntax Highlighting" in TRichEdit Posted by PiSymbol on 26 Jan 2010 at 1:17 AM
You can use RichEdit but there is already a component. Try SynEdit.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.