重写 C++ 中的事件处理程序建设者

发布于 2024-09-10 05:23:15 字数 735 浏览 9 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那请放手 2024-09-17 05:23:15

对于组件来说,将处理程序分配给它自己的事件并不是一个好主意。这可以防止外部代码使用相同的事件,或者更糟糕的是用自己的事件覆盖您的分配,这样您的自定义代码就不再运行。相反,您应该做的是覆盖虚拟 KeyDown() 方法,即:

class PACKAGE TREMemoFind : public TMemo 
{ 
protected: 
    virtual void __fastcall KeyDown(WORD &Key, TShiftState Shift); 

public: 
    __fastcall TREMemoFind(TComponent* Owner); 
}; 


__fastcall TREMemoFind::TREMemoFind(TComponent* Owner) : TMemo(Owner) 
{ 
    ScrollBars = ssVertical; 
} 

void __fastcall TREMemoFind::KeyDown(WORD &Key, TShiftState Shift) 
{ 
    ShowMessage("It worked!"); 
    TMemo::KeyDown(Key, Shift); // fire public OnKeyDown event
} 

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:

class PACKAGE TREMemoFind : public TMemo 
{ 
protected: 
    virtual void __fastcall KeyDown(WORD &Key, TShiftState Shift); 

public: 
    __fastcall TREMemoFind(TComponent* Owner); 
}; 


__fastcall TREMemoFind::TREMemoFind(TComponent* Owner) : TMemo(Owner) 
{ 
    ScrollBars = ssVertical; 
} 

void __fastcall TREMemoFind::KeyDown(WORD &Key, TShiftState Shift) 
{ 
    ShowMessage("It worked!"); 
    TMemo::KeyDown(Key, Shift); // fire public OnKeyDown event
} 
夏末的微笑 2024-09-17 05:23:15

您在为事件分配函数地址时犯了一个错误。

__fastcall TREMemoFind::TREMemoFind(TComponent* Owner) : TMemo(Owner)
{
    ScrollBars = ssVertical;
    OnKeyDown = &MemoKeyDown;
}

聚苯乙烯
另请参阅深入解释:
在运行时向组件添加事件

You made a mistake at assigning an adress of a function to the event.

__fastcall TREMemoFind::TREMemoFind(TComponent* Owner) : TMemo(Owner)
{
    ScrollBars = ssVertical;
    OnKeyDown = &MemoKeyDown;
}

P.S.
See also for in-depth explanation:
add event to component in the run time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文