讲述如何开发一个控件,很有价值(六)[2]

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

本文简介:选择自 mysine 的 blog

were going to use the gettoken() function to do all the hard work. we'll need some extra variables to pass to the gettoken function, so add to the var section:

mytokenstr:string;
mytokenstate:ttokenstate;
myrun:pchar;
myselstart: integer;

these are similar to the variables we used in the convertreadstream - in fact we want to do "exactly" the same thing, just one single line at a time. add this code before the last end;

strpcopy(mypbuff,myre.lines.strings[row]);
mypbuff[length(myre.lines.strings[row])] := #0;
myselstart := beginselstart;
myrun        := mypbuff;

while(myrun^ <> #0) do
begin

myrun := pascon.gettoken(myrun,mytokenstate,mytokenstr);

//
// scanforrtf;
// setrtf;
// writebuffer(prefix + tokenstr + postfix);
//

end;

end;

nb: as we will be using pascon you'll have to move it from being a local variable of tform1.button1click to be a global variable. this will  mean you'll have to move all the initialising:

pascon:=tpasconversion.create;
pascon.usedelphihighlighting(3);

to a tform1.show, and the pascon.free to tform1.close procedure. it will still work if you only move the variable definition - but not for long... :-)

i've left the code from the old convertreadstream in the example above to show what we "logically" still need to implement in the current context - that is manipulating the richedit control directly. what we have now is the ability to cut up the current line in to different tokens, and know what type they are. we now have to add these tokens to current line with the right attributes (fonts,colors,bold etc).

but wait. they are already on the line - well the text is anyway, but maybe not in the correct format (color,bold etc). so what actually could do is to select each token in its corresponding positon in the richedit control and just apply the appropriate attributes to them.

we did this back in the beginning remeber? when we set the >10 character lines to the color red. but how do we do this now? lets look at what we have in the variables at hand when we hit "// setrtf" the first time:
 


(these example uses uni1.pas as the input file as its more interesting)
 



 

variables

01234567901234567890
lines.strings[r0] unit unit1;
mypbuff unit unit1;
mytokenstate tsidentifier
mytokenstr unit
myrun  unit1;

本文关键:控件开发
  相关方案
Google
 

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

go top