重写 C++ 中的事件处理程序建设者
在 Codegear C++ Builder 中,我尝试扩展 TMemo VCL 类以在 OnKeyDown 事件期间执行某些功能。我已经设置了该控件,并且能够将其添加到表单等中。问题是我无法捕获 OnKeyDown 事件(或任何其他事件)。
这是我的类:
class PACKAGE TREMemoFind : public TMemo
{
private:
TFindDialog *FindDialog;
protected:
void __fastcall MemoKeyDown(TObject *Sender, WORD &Key, TShiftState Shift);
public:
__fastcall TREMemoFind(TComponent* Owner);
__published:
};
__fastcall TREMemoFind::TREMemoFind(TComponent* Owner) : TMemo(Owner)
{
ScrollBars = ssVertical;
OnKeyDown = MemoKeyDown;
}
void __fastcall TREMemoFind::MemoKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
ShowMessage("It worked!");
}
所以,我想我的问题是:如何设置从 VCL 类派生的类,以便在某个事件触发时执行自定义函数?
In Codegear C++ Builder, I'm trying to extend the TMemo VCL class to perform some functionality during the OnKeyDown event. I've set up the control and am able to add it to forms and so forth. The problem is that I am unable to capture the OnKeyDown event (or any other event, for that matter).
Here is my class:
class PACKAGE TREMemoFind : public TMemo
{
private:
TFindDialog *FindDialog;
protected:
void __fastcall MemoKeyDown(TObject *Sender, WORD &Key, TShiftState Shift);
public:
__fastcall TREMemoFind(TComponent* Owner);
__published:
};
__fastcall TREMemoFind::TREMemoFind(TComponent* Owner) : TMemo(Owner)
{
ScrollBars = ssVertical;
OnKeyDown = MemoKeyDown;
}
void __fastcall TREMemoFind::MemoKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
ShowMessage("It worked!");
}
So, I guess my question is: how do I set up the classes that I derive from the VCL classes to perform custom functions when a certain event fires?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于组件来说,将处理程序分配给它自己的事件并不是一个好主意。这可以防止外部代码使用相同的事件,或者更糟糕的是用自己的事件覆盖您的分配,这样您的自定义代码就不再运行。相反,您应该做的是覆盖虚拟 KeyDown() 方法,即:
It is not a good idea for a component to assign a handler to its own event. That prevents outside code from using that same event, or worse overwriting your assignment with ther own so your custom code does not run anymore. Instead, what you should be doing is overriding the virtual KeyDown() method, ie:
您在为事件分配函数地址时犯了一个错误。
聚苯乙烯
另请参阅深入解释:
在运行时向组件添加事件
You made a mistake at assigning an adress of a function to the event.
P.S.
See also for in-depth explanation:
add event to component in the run time