谁帮我解释这个inherited的奇怪行为?(200分)

  • 主题发起人 主题发起人 滨滨
  • 开始时间 开始时间

滨滨

Unregistered / Unconfirmed
GUEST, unregistred user!
在Controls.pas中TControl的消息处理句柄
procedure TControl.WMLButtonDblClk(var Message: TWMLButtonDblClk);
begin
SendCancelMode(Self);
inherited;
~~~~~~~~~~
if csCaptureMouse in ControlStyle then MouseCapture := True;
if csClickEvents in ControlStyle then DblClick;
DoMouseDown(Message, mbLeft, [ssDouble]);
end;

中inherited;一句会调用父类的哪一个方法呢?按Delphi的帮助,单独的inherited;会调用父类中的同名方法,但我并没有发现TControl的父类的同名的过程啊?
我跟踪以后竟发现
inherited;调用的结果是:
procedure TCustomForm.DefaultHandler(var Message);
我实在是想不通!!!
除此之外,所有TControl的消息处理句柄,只要有inherited;调用,一律调用到TCustomForm.DefaultHandler去了,可是这两个类的方法之间哪有什么继承关系???
而且TControl是父,TCustomForm是子,这个不带任何参数的inherited怎么就调用到那里去了呢?
 
反正执行到这一句后函数调用就会指向虚函数表中-16的位置,正好是DefaultHandler
 
delphi帮助里面的一句话解释你的问题:
Calling inherited in a message-handling method results in a call to
the ancestor's DefaultHandler method only if that ancestor does not
specify a message method for the particular message being handled.
Otherwise calling inherited results in a call to the specific handler
for that type of message
 
这里的inherited是把message的结果返回到消息队列去了, 许多消息都需要有一个result的
对于TControl来说, 如果message不要求返回结果的话, 可以忽略inherited
 
我也找到了解释:
The inherited statement searches backward through the class hierarchy and invokes the first message handler with the same ID as the current method, automatically passing the message record to it. If no ancestor class implements a message handler for the given ID, inherited calls the DefaultHandler method originally defined in TObject.
The implementation of DefaultHandler in TObject simply returns without performing any actions. By overriding DefaultHandler, a class can implement its own default handling of messages. The DefaultHandler method for VCL controls calls the Windows DefWindowProc function.
谢谢二位!
 
知道了答案还不给分?(反正没我的份儿)[:D]
 
接受答案了.
 
后退
顶部