iczelion tut35[1]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 jimgreen 的 blog

tutorial 35: richedit control: syntax hilighting

before reading this tutorial, let me warn you that it's a complicated subject: not suited for a beginner. this is the last in the richedit control tutorials.

download the example.

theory

syntax hilighting is a subject of hot debate for those writing text editors. the best method (in my opinion) is to code a custom edit control and this is the approach taken by lots of commercial softwares. however, for those of us who don't have time for coding such control, the next best thing is to adapt the existing control to make it suit our need.

let us take a look at what richedit control provides to help us in implementing syntax hilighting. i should state at this moment that the following method is not the "correct" path: i just want to show you the pitfall that many fall for. richedit control provides em_setcharformat message that you can use to change the color of the text. at first glance, this message seems to be the perfect solution (i know because i was one of the victim). however, a closer examination will show you several things that are undesirable:

  • em_setcharformat only works for a text currently in selection or all text in the control. if you want to change the text color (hilight) a certain word, you must first select it.
  • em_setcharformat is very slow
  • it has a problem with the caret position in the richedit control

with the above discussion, you can see that using em_setcharformat is a wrong choice. i'll show you the "relatively correct" choice.

the method i currently use is "syntax hilighting just-in-time". i'll hilight only the visible portion of text. thus the speed of the hilighting will not be related to the size of the file at all. no matter how large the file, only a small portion of it is visible at one time.

how to do that? the answer is simple:

  1. subclass the richedit control and handle wm_paint message within your own window procedure
  2. when it receives wm_paint message, it calls the original window procedure of the richedit control to let it update the screen as usual.
  3. after that, we overwrite the words to be hilighted with different color

of course, the road is not that easy: there are still a couple of minor things to fix but the above method works quite nicely. the display speed is very satisfactory.

now let's concentrate on the detail. the subclassing process is simple and doesn't require much attention. the really complicated part is when we have to find a fast way of searching for the words to be hilighted. this is further complicated by the need not to hilight any word within a comment block.

the method i use may not be the best but it works ok. i'm sure you can find a faster way. anyway, here it is:

本文关键:iczelion asm
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top