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

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

本文简介:选择自 mysine 的 blog

now: put the debugging code on, do [build all] and then [run], and set a breakpoint on the first line of this event. open up the testinput.pas. when the debugger stops in the onchange event, press <f9> to continue on, and press <f9> again, and again - do you see? we keep going back into the event again and again (and again). what’s happening?

somehow in our event we are triggering off another [onchange] event. this call to the [onchange] event code is stored in the message queue. when the event were currently in is finished, a new one is just waiting on the event queue, which executes and creates more events... a re-entrant loop.

this behaviour is not surprising - after all we are actually changing the control in the process of our code, so no wonder another [onchange] event is being triggered.

the way to fix such things is to ensure our actions do not trigger of the event. we can do this by "temporarily" storing the richedits.onchange property (which contains a reference to call our procedure tform1.richedit1change) in our own internal variable, and then setting the onchange property to nil.

we then do all the processing we want to do - if it happens to trigger an [onchange] event - there is nothing to call as onchange is nil, and so the event doesn't go onto the message queue. when we're finished however we must return the onchange property to it original value, otherwise the reprocessing want happen next time around.

if we look at the delphi helpfile we see that the onchange property is of a certain type, the same type we have to make our saveonchangein variable:

var

saveonchangein: tnotifyevent;

~~~~~rest of code

begin

myre                     := trichedit(sender);
saveonchangein  := myre.onchange;
myre.onchange  := nil;

~~~~~rest of code

myre.onchange := saveonchangein;

end;

try it out!!!

  • compile and run
  • open up unit1.pas in the "editor" we have written
  • click in the richedit in the center of the first lines, in the middle of "unit".
  • press the [space bar]
  • press the [backspace key]
  • arrow to the end of the line
  • press [enter]
  • press [backspace]
  • [backspace] away the entire line
  • re-type the entire line

result:  "functionally" the control should look that same as it did before we clicked in it. the line "unit unit1;" should highlighted properly as per your delphi 3.0 editor (save the background colour). however its slow and flickers a great deal. try opening up a new line and just type a long phrase - e.g "if (richedit = santa) then getpresents('box of choclate'); " and you'll agree with me that:

  1. good - it is highlighting properly
  2. bad - there is flickering
  3. bad - the longer the line gets, the longer it takes to do the re-highlighting
  4. bad - you get the "someone is chasing me effect"

the flickering is due to a number of components. we'll have to deal with each seperately.

the most obvious is the "selecting" of each token. visually the control is just repeating what we were able to do manually - when a piece of text is selected it becomes highlighted by the black stripe. we need to stop this from happening. back to the helpfile(s) again. have a search around, and come back after a snack break with some ideas... i'm hungry :-)
 
 

death to the black stripe
 

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

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

go top