修改VCL组件代码

发布于 2024-11-30 01:59:23 字数 81 浏览 3 评论 0 原文

我需要更改组件中的功能。当您收到“您无法覆盖此”消息,或者您需要更改私有方法中的代码(“基类中不存在该方法”消息),导致组件无法下降时,您该怎么办?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2024-12-07 01:59:23

如果我遇到这个问题,

  • 我首先尝试从组件或其 CustomXXX 祖先继承,看看是否可以解决问题。如果没有,
  • 我会更深入,即尝试拦截传入的消息。这可以动态完成。如果事实证明这太深了,因为必须构建的代码太广泛,或者如果我仍然必须访问我无法访问的项目,
  • 我会尝试 hack。一种技巧是将组件和依赖代码复制到具有不同名称的新单元,重命名组件并修改需要修改的内容。
  • 有时我只需要重做一两个方法就可以使我的新行为成为可能。

永远不要忘记给单元一个不同的名称,也给组件一个不同的名称(可能继承自原始组件或其祖先之一,因此它们保留在同一层次结构中)。 切勿修改原始源代码然后重新编译 VCL。这是维护的噩梦。

我不喜欢插入器类,即与原始类具有相同名称但行为不同、继承自原始类的类。它们的功能取决于 use 子句中包含的顺序,这对我来说似乎很不稳定。我不能推荐这样做。

但我做什么很大程度上取决于问题。我认为人们不能(或应该)给出涵盖所有情况的一揽子建议。

但我的主要建议是:不要修改原始单位,始终将新代码放入新单位中,并且使用新类名。这样原始版本和修改版本就可以和平共存,在 IDE 中也是如此。

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.

懒的傷心 2024-12-07 01:59:23

在修改私有方法或其中的行为时,有一些(主要是 hacky)选项:

  • 修改原始源,重新编译单元并按照建议使用更改后的 dcu 此处;从来没有这样做过,但我认为当您的代码使用新的 dcu 但其他 VCL 代码
  • 通常不会由大量窗口消息控制组件行为时,这可能会让您很头痛 - 看看您是否可以通过修改某些内容的反应来实现更改这些消息;您可以覆盖消息处理方法(带有 message 关键字的方法),即使它们被声明为私有,您也可以替换 WndProc
  • 您可以使用像 this 正在修改转换,
  • 您可以使用一些绕行机制,如答案此处

或者您可以获得另一个组件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文