Change VCL Component CODE

I need to change the functionality in the component. What do you do when you get the message "you cannot override this", or do you need to change the code in a private method (the message "the method does not exist in the base class"), which makes it impossible to launch the component?

+4
source share
2 answers

If I run into this problem,

  • First I try to inherit from the component or its ancestor CustomXXX and see if this fixes the problem. If this is not so,
  • I go deeper, i.e. trying to intercept incoming messages. This can be done dynamically. If this turns out to be too deep, because the code that needs to be built on it is too extensive, or I still need to access elements that I cannot access,
  • I'm trying to hack. One hack is copying a component and dependent code to a new block with a different name, renaming the component and changing what needs to be changed.
  • Sometimes I only need to repeat one or two methods to make my new behavior possible.

Never forget to give the device a different name, and the component a different name (possibly inherited from the original component or one of its ancestors, so they remain in the same hierarchy). Never change the source, then recompile the VCL. This is a nightmare to service.

I am not a fan of intermediary classes, that is, classes that receive the same name, but differ from the original classes, inheriting from the original. Their functionality depends on the order of inclusion in the uses clause, and this seems to me superfluous. I can not recommend this.

But what I do is highly dependent on the problem. I do not think that it is possible (or should) give full advice that covers all situations.

But my main advice: do not change the original units , always put the new code in a new block and use the new class name. Thus, the original and modified versions can coexist peacefully, also in the IDE.

+4
source

There are some (mostly hacker) options when it comes to modifying private methods or behavior in it:

  • change the source, recompile the device and use the modified dcu as suggested here ; never did this, but I think it can cause you a good headache when your code uses the new dcu, but the other VCL code is not
  • often the behavior of components is controlled by numerous messages in the window - see if you can achieve your change by changing the reaction to some of these messages; you can override message processing methods (those that have the message keyword), even if they are declared private, and you can also replace WndProc
  • you can use hacks like this one that messes with casting
  • you can use some kind of workaround as described in the answers here

Or you can get another component.

+4
source

Source: https://habr.com/ru/post/987940/


All Articles