开发者

Modifying VCL Component CODE

I need to change the functionality in a component. What do you do when you get a "you can't override 开发者_开发百科this" message, or you need to change the code in a private method ("method does not exist in base class" message), rendering it impossible to descend the component?


If I face that problem,

  • I first try to inherit from the component, or its CustomXXX ancestor and see if that fixes the problem. If that doesn't,
  • I go deeper, i.e. try to intercept the messages that come in. That can be done dynamically. If that turns out to be too deep, because the code that has to be built on that is too extensive, or if I still have to access items I can't access,
  • I try hacks. One hack is to copy the component and the dependent code to a new unit with a different name, rename the component and modify what needs to be modified.
  • Sometimes I only need to redo one or two methods to make my new behaviour possible.

Never forget to give the unit a different name and the component a different name too (possibly inheriting from the original component or one of its ancestors, so they remain in the same hierarchy). Do never modify the original sources and then recompile the VCL. That is a maintenance nightmare.

I am no fan of interposer classes, i.e. classes that get the same name but different behaviour than the original classes, inheriting from the original. Their functionality depends on the order of inclusion in the uses clause, and that seems rahter flaky to me. I can't recommend that.

But what I do greatly depends on the problem. I don't think one can (or should) give a blanket advice that covers all situations.

But my main advice: do not modify the original units, always put the new code in a new unit and use a new class name. That way the original and the modified versions can peacefully co-exist, also in the IDE.


There are some (mainly hacky) options when it comes to modifying private methods or behavior therein:

  • modify the original source, recompile the unit and use the changed dcu as suggested here; never did this but I think this can cause you a good headache when your code uses the new dcu but other VCL code don't
  • often component behavior is controlled by numerous window messages - look if you can achieve your change by modifying the reaction on some of these messages; you can override the message handling methods (the ones with the message keyword) even if they are declared private and you can also replace the WndProc
  • you can use hacks like this one which is tinkering with casting
  • you could use some detour mechanism as described in the answers here

Or you can get another component.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜