Accessing protected event of TWinControl

前端 未结 1 757
醉酒成梦
醉酒成梦 2020-11-30 15:24

imagine, you want to assign your own event procedure:

procedure TSuperObject.DoSomething(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: In         


        
相关标签:
1条回答
  • 2020-11-30 15:51

    You don't need RTTI.

    Any code has implicit access to the protected members of any class declared in the same unit. You can take advantage of this by declaring a new TWinControl descendant in the unit that needs access to that class's members. The declaration is very simple:

    type
      TProtectedWinControl = class(TWinControl);
    

    Then type-cast any other TWinControl descendant to that new type, and you'll have access to any of its protected fields, properties, and methods. Protected members of TWinControl are automatically protected members of TProtectedWinControl (through inheritance), so the current unit has access to them.

    TProtectedWinControl(AnyWinControl).OnMouseDown := SuperObject1.DoSomething;
    

    Note that this applies to protected members, but not strict protected members.

    0 讨论(0)
提交回复
热议问题