微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Delphi中的消息处理程序方法忽略它是“私有”的

如何解决Delphi中的消息处理程序方法忽略它是“私有”的

有两个类:TBaseClassTDerivedClass,它们都在单独的单元中。

TBaseClass

unit InhPriv.BaseClass;

interface

uses Vcl.Controls,Winapi.Windows,Winapi.Messages,InhPriv.Messages;

type
  TBaseClass = class(TWinControl)
  strict private
    procedure TheMethod(var msg: TMessage); message WM_CUSTOM;
  end;

implementation

procedure TBaseClass.TheMethod(var msg: TMessage);
begin
  MessageBox(0,'TBaseClass.TheMethod','IhheritedPrivateTest',0);
end;

end.

TDerivedClass

unit InhPriv.DerivedClass;

interface

uses InhPriv.BaseClass,InhPriv.Messages;

type
  TDerivedClass = class(TBaseClass)
  public
    procedure TheMethod(var msg: TMessage); message WM_CUSTOM;
  end;

implementation

procedure TDerivedClass.TheMethod(var msg: TMessage);
begin
  inherited; //TBaseClass.TheMethod gets called!!!
  MessageBox(0,'TDerivedClass.TheMethod',0);
end;

end.

主机表单

TForm1 = class(TForm)
//.......
private
  c: TDerivedClass;
//.......
end;

WM_CUSTOM被定义为WM_USER + 1

调用方法

变体1:发送消息:

procedure TForm1.Button1Click(Sender: TObject);
begin
  SendMessage(c.Handle,WM_CUSTOM,0);
end;

输出

TBaseClass.TheMethod
TDerivedClass.TheMethod

变体2:直接调用方法

procedure TForm1.Button1Click(Sender: TObject);
var m: TMessage;
begin
  c.TheMethod(m);
end;

输出

TBaseClass.TheMethod
TDerivedClass.TheMethod

cpu窗口

这是直接方法调用

InhPriv.DerivedClass.pas.17: inherited; //TBaseClass.TheMethod gets called!!!
005B4880 8B55F8           mov edx,[ebp-$08]
005B4883 8B45FC           mov eax,[ebp-$04]
005B4886 E87DFDFFFF       call TBaseClass.TheMethod

Delphi文档说:

继承的后没有标识符时,它是指与封闭方法同名的继承方法,或者如果封闭方法是消息处理程序,则指向继承的消息相同消息的处理程序。

因此,没有明确的说法表明将方法转换为消息处理程序会使Delphi编译器违反OOP原则。

我可以错过一些文档吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。