将 TForms 添加到 TPageControl,如何通知 TForm 它正在显示?
我有一个 TPageControl,其中包含许多包含 TForm(或可能是 TFrame,但目前是 TForm)的 TTabSheets。
当选项卡进入视图时,我希望 TForm 或 TFrame 收到通知,它已来到前面。我找不到任何可以做到这一点的东西。
我知道我可以在 OnChange
事件中获取 Active TTabSheet,因此我尝试将此类添加到 TForm:
struct iTab
{
virtual void DoIt( void ) = 0;
};
在 OnChange
中使用 this:
ICPTab *tab = dynamic_cast<ICPTab *>( sheet->Controls[ 0 ] );
我想我可以使用 RTTI获取 iTab
指针并调用 DoIt()
我收到警告:
[BCC32 Warning] Unit1.h(18): W8130 Interface 'IPTab' does not derive from IUnknown. (Interfaces should derive from IUnknown)
[BCC32 Warning] MainWindow.cpp(612): W8131 Casting Delphi style class 'TControl' to an interface. Use 'System::interface_cast<ICPTab>(cls)' instead
我对获取所有 IUnknown 不感兴趣,以便表单可以使用接口。
我可以使用以下方法获取 TFrame 或 TForm 指针:
TForm *tab = dynamic_cast<TForm *>( sheet->Controls[ 0 ] );
但无法使用此指针调用非 TForm 方法。调用 Activate()
方法可以吗?
那么我如何通知 TForm 或 TFrame 现在正在显示呢?
I have a TPageControl with a number of TTabSheets that contain TForms (or possibly TFrames, but TForms for now).
When a tab comes into view I would like the TForm or TFrame to be notified it has come to the front. I can't find anything that does that.
I know I can get the Active TTabSheet in the OnChange
event, so I tried to add this class to the TForm:
struct iTab
{
virtual void DoIt( void ) = 0;
};
with the this in the OnChange
:
ICPTab *tab = dynamic_cast<ICPTab *>( sheet->Controls[ 0 ] );
Thinking I could use RTTI to get the iTab
pointer and call DoIt()
from the
And I get the warning:
[BCC32 Warning] Unit1.h(18): W8130 Interface 'IPTab' does not derive from IUnknown. (Interfaces should derive from IUnknown)
[BCC32 Warning] MainWindow.cpp(612): W8131 Casting Delphi style class 'TControl' to an interface. Use 'System::interface_cast<ICPTab>(cls)' instead
I am not interested in getting all of IUnknown just so that the form can use an interface.
I can get the TFrame or TForm pointer using:
TForm *tab = dynamic_cast<TForm *>( sheet->Controls[ 0 ] );
but can't call a non TForm method with this pointer. Would it be ok to call the Activate()
method?
So how do I notify the TForm or TFrame that it is now showing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须从
IUnknown
(或IInterface
)派生iTab
或顺序将其正确用作接口。编译器警告甚至提示您可以使用interface_cast
从 Delphi 样式(又名TObject
派生)对象中提取接口。或者使用TObject::GetInterface()
代替。否则,如果您只想调用自定义 TForm 类的方法,则只需将子控件指针从 TTabSheet 类型转换为实际的 TForm 派生类:
或者:
You have to derive
iTab
fromIUnknown
(orIInterface
) or order to use it as an interface correctly. The compiler warning even hints that you can useinterface_cast
to extract an interface from a Delphi-style (akaTObject
-derived) object. Alternatively useTObject::GetInterface()
instead.Otherwise, if you just want to call methods of your custom TForm class, then just type-cast the child control pointer from your TTabSheet to your actual TForm-derived class:
Or:
我使用了以下代码:
由于选项卡上的表单从未调用其 OnActivate 方法,因此效果非常好。
注意:方法示例不完整,没有调用 OnDeactivate
I went with the following code:
Since a form on a tabsheet never has its OnActivate method called, this works quite well.
note: the method sample is not complete, there is no call to OnDeactivate