将click事件分配给wxPanel实例

时间:2013-10-30 11:40:59

标签: c++ events wxwidgets

我有一个wxPanel类。我需要该类的每个实例处理一个click事件。在wxWidgets中可以吗?到目前为止,我总是不得不使用窗口ID将事件分配给对象(例如按钮) 但是这个wxPanel即将在屏幕上成倍增加,所以我需要避免使用窗口ID。

班级:

panel.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);
};

panel.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中的工作原理吗?

1 个答案:

答案 0 :(得分:0)

wxEVT_COMMAND_LEFT_CLICK不是由wxPanel生成的(事实上也不是几乎所有其他内容),您正在寻找的事件称为wxEVT_LEFT_UP并且它是wxMouseEvent ,而不是wxCommandEvent

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