将单击事件分配给 wxPanel 实例

Assign click event to a wxPanel instance

本文关键字:wxPanel 实例 分配 单击 事件      更新时间:2023-10-16

我有一个wxPanel类。我需要该类的每个实例处理一个单击事件。这在wxWidgets中可能吗?到目前为止,我总是必须使用窗口 ID 将事件分配给对象(例如按钮)。
但是,这个wxPanel即将在屏幕上乘法出现,所以我需要避免使用窗口ID。

班级:

面板.h

class UVStatusPanel : public wxPanel
{
public:
    UVStatusPanel(wxFrame* parent, int pos);
    void paintEvent(wxPaintEvent& evt);
    void paintNow();
    //THIS is expected to be the event function
    void onClick(wxCommandEvent& WXUNUSED(event));
    bool State(bool state);
    bool State();
    DECLARE_EVENT_TABLE()
private:
    bool painting;
    bool state;
    void render(wxDC &dc);
};

面板.cpp

/**The constructor*/
UVStatusPanel::UVStatusPanel(wxFrame* parent, int pos) :
wxPanel(parent)
{
 /** some align and draw rect code was there**/
 //My two attepmts to assign the click event to the panel
 Connect(this->m_windowId, wxEVT_COMMAND_LEFT_CLICK, 
          wxCommandEventHandler(UVStatusPanel::onClick));
 //EVT_COMMAND_LEFT_CLICK(this->m_windowId,UVStatusPanel::onFocus);
}

目前已注册 noc 点击。谁能解释一下点击事件在 wxWidgets 中是如何工作的?

wxEVT_COMMAND_LEFT_CLICK不是由wxPanel生成的(老实说,也不是由其他任何东西生成的),您正在寻找的事件称为wxEVT_LEFT_UP,它是一个wxMouseEvent,而不是wxCommandEvent

此外,不建议在示例中同时使用事件表和Connect()。它确实有效,但可能会令人困惑,只需选择一个并坚持使用它(最好是Connect()或者,如果您使用 2.9+,Bind() )。