15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Need CRichEdit Syntax Coloring Example

Chet Murphy -- cmurphy@modelworks.com
Wednesday, October 09, 1996

[Mini-digest: 3 responses]

Karen ,

> Environment: VC++ 4.1, Win 95 
> 
> I have a CRichEditView in which I need to
> do coloring of specific key words.  I believe
> that I can do this with strictly plain text using
> character formatting and do not need to deal with rtf.
> Is there an example that someone could point me to?
> Also, the coloring must occur when the user types 
> in key words, such as in the Visual C++ editor.  
> I have MSDN.

I tried doing syntax coloring with CRichEditView.  In my approach I
created a RTF stream by parsing raw text and adding RTF encoding for
the words that needed to be syntax colored.  I also used an interactive
parser to syntax color text entered by the user.  All of this worked
reasonability well although it was a little slow even on a fast
machine.  The one problem that I could not solve was keeping syntax
colored text readable when selecting text.  In the end I gave up and
wrote my own text editor from scratch.

> Is it always necessary to pair the CRichEditView
> with a CRichEditDoc?  

No.  

Chet Murphy
ModelWorks Software - Editors for Java, JavaScript, VBScript, HTML and VRML
cmurphy@modelworks.com
http://www.modelworks.com/express
-----From: "MHENRY.UMI.COM" 

Environment: VC++ 4.0, Win 95  
 
Karen, 
 
Funny you should ask.  I spend all day writing syntax coloring views.  I have 
a project in which users can load any one of 6 different file formats which 
are all colored differently.  My needs are a bit complex, but if you only need 
to color key words it's a piece of cake. 
 
1. Trap the OnChar event and get the current line.  
2. Scan back to the start of the word and see if it's a keyword by looking it 
up in a CStringToPtr map.   
	Just store NULL for the "pointers" in the map and use it as a hash 
table.   
3. If the user types a  syntax color the current and previous words.  
If the user types  color the last word on the previous line also.  
You'll also need to handle OnPaste(), OnCut() and OnClear(). 
4. Use the standard CRichEditCtrl::SetSel() and 
CRichEditCtrl::SetSelectionCharFormat() to change the color.  Note: it seems 
to be faster if you GetSelectionCharFormat() to test to see if you want to 
change the color of the text before you actually change it. 
 
As far as examples of syntax coloring, it sounds like what I do is much more 
complex than you need.  I need to color comments and filenames other various 
annoyingly complex things.  I use a state table generated by lex and scan the 
line, using a hash table to look up keywords.   
You can search on the web for an editor called "Elvis 2.0", a vi clone with 
syntax coloring that comes with source code.  But it probably does more than 
you need to do as well.  And it doesn't use a CRichEditView. 
 
 
>in key words, such as in the Visual C++ editor.   
 
I've spent a lot of time playing with the Visual C++ editor to figure out how 
they do their syntax coloring.  I'm pretty sure they do not use a 
CRichEditCtrl.  Anyway, their method is pretty terrible; it's very easy to 
fool, especially with numbers.  I think Microsoft needs to hire me to redo it 
;-) 
 
>Is it always necessary to pair the CRichEditView 
>with a CRichEditDoc?  If not, what is the scenario 
>where one would just use a CDocument? 
 
I think a CRichEditDoc has to be paired with the CRichEditView.  Anyway, why 
wouldn't you want to? 
 
 
--matt 
/~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  Matthew Henry  -- UMI            
  mhenry@umi.com     (Work)              
  mhenry1384@aol.com (Home)  
~~~~~~~~~~~~~~~~~~~~~~~~~~/  
 
-----From: "Luke Stephens" 

Look at the Wordpad source code on the VC++ CD.
It's located in msdev\samples\mfc\ole\wordpad.

I think it has what you are looking for.

Luke Stephens
luker@tfs.net



Michael Iles -- michaeli@dra.com
Friday, October 11, 1996


I have written a CRichEditCtrl-derived class that does simple syntax   
colouring. The only difference between mine and the ones described by   
others here is that mine takes the words to be coloured from a string   
resource and I run them through my own simple finite-state-machine   
instead of using lex (or flex or whatever) to generate the state table   
for me.

It's very simple to trap keystrokes and colour the one or two affected   
words (as described by others), but the tricky part I came across is that   
with rich edit controls the user can drag text around with the mouse and   
I had to implement an OLE interface map to be notified when this had   
happened. (People in this forum helped me to figure this out... Thanks!)

I would be happy to provide my source or respond to questions. In   
retrospect (as always) it's quite easy and involves only a hundred or so   
lines of code.

Mike Iles
(michaeli@dra.com)




| Вернуться в корень Архива |