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

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

本文简介:选择自 mysine 的 blog

success - (nearly...)
 

i think you'll agree we are pretty close. there is just a little bit of flicker. this flicker is the selstart jumping the cursor position around the text. we need to hide this. this "cursor" is also known as a caret. looking throught win32.hlp again we find the lovely, and appropriately named, hidecaret() function.

lets try this then: everytime we change the value of myre.selstart lets call hidecaret(myre.handle) immediately before.

i'll be kind - that doesn't work. i tried 2 x hidecaret(myre.handle), and it still didn't work. neither did three,four or 25x. so close - but yet - so far. i think its time for another delphi rule:

delphi rule #5 - if you bother to get your way through the atrocious index of the win32.hlp file to find what you are looking for - make sure you really read what you found properly!

the key was the last paragraph in the description of not hidecaret but showcaret (which i had also read as i thought we were going to need it, especially to reverse my 25x hidecaret()). you also need another delphi rule to understand it:
 

the caret is a shared resource; there is only one caret in the system. a window should show a caret only when the window has the keyboard focus or is active. 

delphi rule #6 - everything (basically) is a window

you see the richedit is a windows control and is also.. in a weird sense.. a window. it has a handle, which is why hidecaret would accept it. so re-reading the last line again we get:
 

the caret is a shared resource; there is only one caret in the system. a [richedit] should show a caret only when the [richedit] has the keyboard focus or is active. 

so - in the end - we're back to were we started - we have to disable the richedit to stop the final bit of flickering. this also (co-incidentially) means that em_hideselection is not needed anymore (if hideselection is set properly during design time). so in the end everyone gets 10/10 for marks!

ash version 0.9b
 
 

procedure tform1.richedit1change(sender: tobject);

var

saveonchangein: tnotifyevent; 
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);

saveonchangein := myre.onchange;
myre.onchange  := nil;

myre.enabled   := false;

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);

if myre.selattributes.name <> pascon.fparsefont[mytokenstate].name then myre.selattributes.name := pascon.fparsefont[mytokenstate].name;

if myre.selattributes.color <> pascon.fparsefont[mytokenstate].color then myre.selattributes.color := pascon.fparsefont[mytokenstate].color;

if myre.selattributes.style <> pascon.fparsefont[mytokenstate].style then myre.selattributes.style := pascon.fparsefont[mytokenstate].style;

myselstart := myselstart + length(mytokenstr);

end;

myre.selstart          := wasselstart;
myre.sellength      := 0;
myre.onchange     := saveonchangein;
myre.enabled         := true;

myre.setfocus;

end;

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

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

go top