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

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

本文简介:选择自 mysine 的 blog

so what we need to do is select the word 'unit' in the richedit control, and set its attributes. we do this by setting selstart to the position of 'unit' in the richedit control, and sellength to the length of the word 'unit'. and since 'unit' is at the beginning of the current line - thats position is beginselstart (which i conveninently have stored in myselstart - you'll see why). lets replace the "pseudo" comment code with the following:

myre.selstart       := myselstart;
myre.sellength    := length(mytokenstr);
myre.selattributes.assign(pascon.fparsefont[mytokenstate]);

end;

but remember we are in a loop - when we go around again we'll have the next token in the line, and the variables will look like this:
 

variables

01234567901234567890
lines.strings[r0] unit unit1;
mypbuff unit unit1;
mytokenstate tsspace
mytokenstr (space character)
myrun unit1;

but (space character) isn't at beginselstart (#0) in the richedit control. its further along (at position #4). which just happens to be beginselstart + length('unit'). we need to update myselstart after we process the preceeding token, but before we go around the loop again:

myselstart := myselstart + length(mytokenstr);

end;

okay - this is where we are standing at the moment:
 
 

procedure tform1.richedit1change(sender: tobject);

var

wasselstart,wasrow,row,beginselstart,endselstart: integer;
myre : trichedit;
mypbuff: array[0..255] of char;
mytokenstr:string;
mytokenstate:ttokenstate;
myrun:pchar;
myselstart: integer;

begin

myre := trichedit(sender);

wasselstart    := myre.selstart;
wasrow         := myre.perform(em_linefromchar, myre.selstart, 0);
row            := wasrow;
beginselstart  := myre.perform(em_lineindex, row, 0);
endselstart    := beginselstart + length(myre.lines.strings[row]);

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);
myre.selstart   := myselstart;
myre.sellength  := length(mytokenstr);

myre.selattributes.assign(pascon.fparsefont[mytokenstate]);

myselstart := myselstart + length(mytokenstr);

end;

myre.selstart    := wasselstart;
myre.sellength   := 0;

end;

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

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

go top